Design a Redis Caching Strategy with Eviction and Invalidation Patterns
Implement a production Redis caching strategy with cache-aside pattern, smart TTLs, invalidation, and monitoring for your application.
๐ The Prompt
Design and implement a Redis caching strategy for a [APPLICATION_TYPE] application built with [LANGUAGE/FRAMEWORK]. The application has [ESTIMATED_USERS] concurrent users and the primary bottleneck is [BOTTLENECK_DESCRIPTION] (e.g., expensive database queries, third-party API calls, computed aggregations).
**Implement the following components:**
1. **Cache Layer Architecture**:
- Create a cache service/module with methods: `get`, `set`, `delete`, `getOrSet` (cache-aside pattern)
- Implement connection pooling with [POOL_SIZE] connections and automatic reconnection
- Add a circuit breaker so the application degrades gracefully if Redis is unavailable (fallback to direct source)
2. **Key Design Strategy**:
- Define a namespaced key schema: `[APP_PREFIX]:[ENTITY]:[IDENTIFIER]:[VARIANT]`
- Implement key generation helpers that prevent collisions and enable pattern-based invalidation
- Document keys for these specific use cases: [USE_CASE_LIST] (e.g., user sessions, product listings, search results, rate limiting counters)
3. **TTL and Eviction**:
- Assign appropriate TTLs: [SHORT_TTL] for frequently changing data, [MEDIUM_TTL] for semi-static data, [LONG_TTL] for rarely changing reference data
- Implement jitter (random ยฑ[JITTER_PERCENT]%) on TTLs to prevent thundering herd / cache stampede
- Configure `maxmemory-policy` recommendation based on the use case (explain why `allkeys-lru` vs `volatile-ttl` vs others)
4. **Cache Invalidation**:
- Implement event-driven invalidation using [PATTERN] (e.g., pub/sub, write-through, write-behind)
- Handle these invalidation scenarios: single entity update, bulk updates for [BULK_SCENARIO], and full cache flush for deployments
- Add cache versioning via key prefix `v[N]:` for schema migration scenarios
5. **Monitoring & Observability**:
- Log cache hit/miss ratios with [LOGGING_FRAMEWORK]
- Include helper commands to inspect cache health: memory usage, key count by namespace, and TTL distribution
Provide complete, production-ready code with error handling, type annotations, and inline comments. Include a Redis configuration snippet (`redis.conf` or equivalent) with recommended settings for this workload.
๐ก Tips for Better Results
Be specific about your data access patterns in [BOTTLENECK_DESCRIPTION] โ read-heavy vs write-heavy workloads need fundamentally different caching approaches. Define realistic TTL values based on how stale your data can be rather than using arbitrary numbers. Always implement the circuit breaker pattern so your app survives Redis outages.
๐ฏ Use Cases
Backend and platform engineers optimizing application performance by introducing or improving a Redis caching layer for high-traffic applications with expensive data retrieval operations.