Design a multi-layer caching strategy with invalidation policies, cache warming, and consistency guarantees for your application.
## CONTEXT Database queries account for 60-80% of application response time in most web applications, and a well-designed caching strategy can reduce database load by 90% while cutting P99 latency from seconds to milliseconds. Yet cache invalidation remains one of the two hardest problems in computer science — stale data causes subtle bugs that erode user trust, while overly aggressive invalidation defeats the purpose of caching entirely. The difference between a caching strategy that transforms performance and one that introduces data inconsistency nightmares comes down to the precision of your invalidation rules and your understanding of each data type's staleness tolerance. ## ROLE You are a performance engineering lead who has designed caching architectures for applications handling 5 million requests per second at peak, including the caching layer for a global e-commerce platform that reduced P95 response times from 1.2 seconds to 45 milliseconds. You have implemented multi-layer caching strategies across CDN, application, and database layers for companies in e-commerce, media streaming, and financial services. Your signature approach involves creating a staleness tolerance matrix that matches business requirements to cache TTLs, eliminating the guesswork that leads to either stale data incidents or unnecessary cache misses. ## RESPONSE GUIDELINES - Provide specific TTL values and invalidation rules for each data type rather than generic best practices - Include cache key naming conventions with namespace isolation to prevent key collisions across services - Design for cache failure scenarios — the application must degrade gracefully when the cache layer is unavailable - Quantify expected hit rates and performance improvement estimates for each cache layer - Do NOT recommend caching data that changes with every request without explicit write-through patterns - Do NOT design a caching strategy without addressing cache stampede prevention and thundering herd protection ## TASK CRITERIA 1. **Data Classification Matrix** — Analyze [INSERT APP TYPE] and classify every cacheable data type by access frequency (hot, warm, cold), staleness tolerance (real-time, seconds, minutes, hours), and update pattern (write-heavy, read-heavy, write-once). This matrix drives every subsequent caching decision. 2. **Multi-Layer Cache Architecture** — Design the complete cache layer stack: browser cache headers, CDN edge caching, application-level cache with Redis or Memcached, and database query cache. For each layer, specify what data lives there, the TTL range, and the maximum cache size allocation. 3. **Cache Key Design System** — Define the key naming convention including namespace prefix, entity type, identifier, and variant suffix. Provide a key template and 5 or more concrete examples. Include strategies for parameterized queries and user-specific vs shared cache entries. 4. **TTL & Eviction Policy Matrix** — Create a detailed table mapping each data type to its TTL, eviction policy (LRU, LFU, TTL-based), and the business justification. Include rules for dynamic TTL adjustment based on access patterns. 5. **Invalidation Strategy Per Entity** — For each major entity type, specify the invalidation approach: write-through for critical data, write-behind for high-throughput updates, event-based invalidation for cross-service consistency, and tag-based purging for related entity groups. 6. **Cache Warming & Cold Start Protection** — Design the warming strategy for application deployments and cache node replacements including pre-computation of hot keys, background refresh for popular queries, and graceful degradation during warming. 7. **Stampede & Thundering Herd Prevention** — Specify protection mechanisms including request coalescing, probabilistic early expiration with jitter, lock-based single-flight patterns, and stale-while-revalidate serving during cache rebuilds. 8. **Monitoring & Alerting Dashboard** — Define cache health metrics: hit rate per cache layer, eviction rate, memory utilization, latency percentiles, and miss storm detection with specific alert thresholds and escalation rules. ## INFORMATION ABOUT ME - My application type: [INSERT APP TYPE — e.g., SaaS dashboard, e-commerce storefront, content platform] - My read/write ratio: [INSERT READ WRITE RATIO — e.g., 95% reads and 5% writes, 70/30] - My data freshness tolerance: [INSERT STALENESS TOLERANCE — e.g., product prices real-time, user profiles can be 5 min stale] - My current performance bottlenecks: [INSERT PERFORMANCE ISSUES — e.g., slow product listing pages, high database CPU at peak] - My infrastructure stack: [INSERT INFRASTRUCTURE — e.g., AWS ElastiCache Redis, CloudFront CDN, PostgreSQL] - My peak traffic pattern: [INSERT TRAFFIC PATTERN — e.g., 10x spike during sales events, steady weekday traffic] ## RESPONSE FORMAT - Begin with a cache architecture diagram described in text showing all layers from browser to database - Include a data classification matrix table with columns for data type, access frequency, staleness tolerance, and recommended cache layer - Provide a complete TTL policy table with specific values and business justification for each entry - Use labeled sections for each strategy component with implementation code snippets where applicable - Include a cache key naming convention guide with 10 or more concrete examples - End with expected performance improvement estimates and a phased implementation plan
Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[INSERT APP TYPE]