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.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.