|
| 1 | +# Caching Migration: From In-Memory to Redis |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the migration from problematic in-memory caching to a Redis-based distributed caching solution for the opensensor-api running in Kubernetes. |
| 6 | + |
| 7 | +## Problem Statement |
| 8 | + |
| 9 | +The original implementation used simple in-memory caching with global dictionaries: |
| 10 | + |
| 11 | +```python |
| 12 | +# Problematic in-memory cache |
| 13 | +_cache = {} |
| 14 | +_cache_timestamps = {} |
| 15 | +``` |
| 16 | + |
| 17 | +### Issues with In-Memory Caching in Kubernetes: |
| 18 | + |
| 19 | +1. **Pod Isolation**: Each of the 4 replicas has its own memory space, so cached data isn't shared |
| 20 | +2. **Cache Inconsistency**: Different pods may have different cached values for the same data |
| 21 | +3. **Memory Waste**: Each pod duplicates the same cached data |
| 22 | +4. **Pod Restarts**: Cache is lost when pods restart (common in K8s) |
| 23 | +5. **Scaling Issues**: Adding more replicas multiplies memory usage and cache inconsistency |
| 24 | + |
| 25 | +## Solution: Redis-Based Distributed Caching |
| 26 | + |
| 27 | +### Architecture Changes |
| 28 | + |
| 29 | +1. **New Cache Module**: `opensensor/cache.py` |
| 30 | + - Redis connection management with connection pooling |
| 31 | + - Graceful fallback when Redis is unavailable |
| 32 | + - Comprehensive error handling and logging |
| 33 | + |
| 34 | +2. **Updated Collection APIs**: `opensensor/collection_apis.py` |
| 35 | + - Replaced `simple_cache` decorator with `redis_cache` |
| 36 | + - Updated `get_device_info_cached` function to use Redis |
| 37 | + |
| 38 | +3. **Cache Management Endpoints**: Added to `opensensor/app.py` |
| 39 | + - `/cache/stats` - Get cache statistics |
| 40 | + - `/cache/clear` - Clear all cache entries |
| 41 | + - `/cache/invalidate` - Invalidate specific cache patterns |
| 42 | + |
| 43 | +### Key Features |
| 44 | + |
| 45 | +#### Redis Cache Decorator |
| 46 | +```python |
| 47 | +@redis_cache(ttl_seconds=300) |
| 48 | +def get_device_info_cached(device_id: str): |
| 49 | + """Cached device information lookup using Redis""" |
| 50 | + api_keys, _ = get_api_keys_by_device_id(device_id) |
| 51 | + return reduce_api_keys_to_device_ids(api_keys, device_id) |
| 52 | +``` |
| 53 | + |
| 54 | +#### Graceful Fallback |
| 55 | +- If Redis is unavailable, functions execute without caching |
| 56 | +- No service disruption when Redis is down |
| 57 | +- Automatic reconnection attempts |
| 58 | + |
| 59 | +#### Connection Management |
| 60 | +- Uses existing `REDIS_URL` environment variable |
| 61 | +- Connection pooling for optimal performance |
| 62 | +- Health checks and timeout handling |
| 63 | + |
| 64 | +## Configuration |
| 65 | + |
| 66 | +### Environment Variables |
| 67 | +- `REDIS_URL`: Redis connection string (already available in deployment) |
| 68 | + |
| 69 | +### Dependencies |
| 70 | +Added to `Pipfile`: |
| 71 | +```toml |
| 72 | +redis = "*" |
| 73 | +``` |
| 74 | + |
| 75 | +## Cache Management |
| 76 | + |
| 77 | +### Monitoring |
| 78 | +```bash |
| 79 | +# Get cache statistics |
| 80 | +GET /cache/stats |
| 81 | +``` |
| 82 | + |
| 83 | +Response includes: |
| 84 | +- Redis connection status |
| 85 | +- Number of opensensor cache keys |
| 86 | +- Redis version and memory usage |
| 87 | +- Cache hit/miss ratios |
| 88 | + |
| 89 | +### Maintenance |
| 90 | +```bash |
| 91 | +# Clear all cache |
| 92 | +POST /cache/clear |
| 93 | + |
| 94 | +# Invalidate specific patterns |
| 95 | +POST /cache/invalidate |
| 96 | +{ |
| 97 | + "pattern": "get_device_info_cached:*" |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Benefits |
| 102 | + |
| 103 | +1. **Shared Cache**: All pods share the same cache, ensuring consistency |
| 104 | +2. **Persistence**: Cache survives pod restarts |
| 105 | +3. **Scalability**: Adding more API pods doesn't duplicate cache data |
| 106 | +4. **Performance**: Redis is optimized for caching workloads |
| 107 | +5. **Monitoring**: Built-in metrics and monitoring capabilities |
| 108 | +6. **Reliability**: Graceful degradation when Redis is unavailable |
| 109 | + |
| 110 | +## Deployment Notes |
| 111 | + |
| 112 | +### Kubernetes Deployment |
| 113 | +- No changes required to existing deployment YAML |
| 114 | +- Uses existing `REDIS_URL` environment variable |
| 115 | +- Backward compatible - works with or without Redis |
| 116 | + |
| 117 | +### Rolling Update Strategy |
| 118 | +1. Deploy new image with Redis caching |
| 119 | +2. Old in-memory cache will be gradually replaced |
| 120 | +3. No downtime or service interruption |
| 121 | + |
| 122 | +### Monitoring |
| 123 | +- Check `/cache/stats` endpoint for Redis connectivity |
| 124 | +- Monitor Redis metrics through existing infrastructure |
| 125 | +- Log analysis for cache hit/miss ratios |
| 126 | + |
| 127 | +## Testing |
| 128 | + |
| 129 | +### Local Development |
| 130 | +```bash |
| 131 | +# Install dependencies |
| 132 | +pipenv install |
| 133 | + |
| 134 | +# Set Redis URL (if testing locally) |
| 135 | +export REDIS_URL="redis://localhost:6379" |
| 136 | + |
| 137 | +# Run the application |
| 138 | +uvicorn opensensor.app:app --reload |
| 139 | +``` |
| 140 | + |
| 141 | +### Cache Verification |
| 142 | +```bash |
| 143 | +# Check cache stats |
| 144 | +curl -X GET "http://localhost:8000/cache/stats" \ |
| 145 | + -H "Authorization: Bearer <token>" |
| 146 | + |
| 147 | +# Test cache invalidation |
| 148 | +curl -X POST "http://localhost:8000/cache/invalidate" \ |
| 149 | + -H "Authorization: Bearer <token>" \ |
| 150 | + -H "Content-Type: application/json" \ |
| 151 | + -d '{"pattern": "*"}' |
| 152 | +``` |
| 153 | + |
| 154 | +## Migration Checklist |
| 155 | + |
| 156 | +- [x] Add Redis dependency to Pipfile |
| 157 | +- [x] Create Redis cache utility module |
| 158 | +- [x] Replace in-memory caching in collection_apis.py |
| 159 | +- [x] Add cache management endpoints |
| 160 | +- [x] Update main app to include collection router |
| 161 | +- [x] Document migration process |
| 162 | +- [ ] Deploy to staging environment |
| 163 | +- [ ] Verify Redis connectivity |
| 164 | +- [ ] Monitor cache performance |
| 165 | +- [ ] Deploy to production |
| 166 | + |
| 167 | +## Rollback Plan |
| 168 | + |
| 169 | +If issues arise, the system gracefully falls back to no caching when Redis is unavailable. For complete rollback: |
| 170 | + |
| 171 | +1. Revert to previous image version |
| 172 | +2. In-memory caching will resume automatically |
| 173 | +3. No data loss or service interruption |
| 174 | + |
| 175 | +## Performance Expectations |
| 176 | + |
| 177 | +- **Cache Hit Ratio**: Expected 80-90% for device info lookups |
| 178 | +- **Response Time**: 10-50ms improvement for cached requests |
| 179 | +- **Memory Usage**: Reduced per-pod memory usage |
| 180 | +- **Consistency**: 100% cache consistency across all pods |
0 commit comments