High-Performance Redis Caching Patterns & Invalidation Architect
Design low-latency Redis caching layers with Cache-Aside, Write-Through, TTL strategies, and atomic distributed locking.
System Directive • Copy and Paste Below
Act as a distributed systems architect and Redis caching specialist.
Design a high-throughput, low-latency Redis caching strategy and cache invalidation framework for our database-heavy application.
### WORKLOAD PARAMETERS:
- Application Data Model: [DATA_MODEL_E_G_USER_PROFILES_PRODUCT_CATALOG_SESSIONS]
- Read vs Write Ratio (e.g., 95% reads, 5% writes): [READ_WRITE_RATIO]
- Data Freshness SLA (Strict real-time vs Tolerance for 5-minute staleness): [DATA_FRESHNESS_REQUIREMENTS]
- Target Database: [PRIMARY_DATABASE_ENGINE]
### CACHING BLUEPRINT:
1. **Caching Pattern Selection & Rationale**:
- Evaluation of Cache-Aside (Lazy Loading) vs Write-Through vs Write-Behind for this specific workload.
2. **Key Naming Convention & Data Structure Design**:
- Consistent, hierarchical key format (e.g. `org:{org_id}:user:{user_id}:profile`).
- Selection of optimal Redis data structures (Strings, Hashes, Sorted Sets for leaderboards/pagination) to minimize memory usage.
3. **Cache Invalidation Architecture (Solving the Hardest Problem)**:
- Event-driven cache eviction on database mutations (via webhooks or CDC / Debezium).
- TTL with dynamic jitter to prevent the 'Cache Stampede / Thundering Herd' problem.
4. **Atomic Distributed Locking (Redlock / Lua Script)**:
- Lua script implementation of an atomic lock (`SET resource_name my_random_value NX PX 30000`) ensuring only one worker queries the database on cache miss.
5. **Memory Sizing & Eviction Policy**:
- Recommended `maxmemory-policy` (e.g. `allkeys-lru` vs `volatile-lru`) and memory sizing calculation.
Customization & Placeholders
Specify your read/write ratio and data freshness needs. The architecture includes TTL jitter to prevent cache stampedes.
Example Input Values
Model: Multi-tenant SaaS dashboard metrics, Ratio: 98% reads / 2% writes, Freshness: Can be 60 seconds stale, DB: PostgreSQL.
Expected Output Structure
Cache-Aside pattern implementation in TypeScript/Python, Redis Hash data structure, TTL jitter function, and Lua distributed lock.
High-Performance Cloud Architectures
Qubenix builds blazing-fast cloud architectures using optimized Redis caching, CDN edges, and database clustering.
More Prompts in Coding & Development
React & TypeScript Clean Component Architecture & State Management
Architect clean, maintainable React 19 / TypeScript components using compound patterns, custom hooks, and zero re-render bloat.
PostgreSQL / MySQL Query Optimization, Indexing & EXPLAIN ANALYZE
Diagnose slow database queries, optimize table schemas, and design composite B-Tree/GIN indexes to drop execution time by 90%+.
Enterprise REST & GraphQL API Schema Designer with OpenAPI Specs
Design clean, pragmatic RESTful endpoints or GraphQL schemas adhering to OpenAPI 3.1 standards, pagination, and status codes.