Caching Strategies and Cache Invalidation
A cache stores reusable results closer to the consumer to reduce latency and backend load. Common patterns include cache-aside, read-through, write-through, and write-behind. In cache-aside, the application checks the cache, loads a miss from the source, then populates the cache.
Cache invalidation is difficult because cached data can outlive the source state. Use TTLs, versioned keys, explicit invalidation, or event-driven updates according to the consistency requirement. A short TTL limits staleness but increases backend traffic; a long TTL improves hit rate but may serve older data longer.
Protect against cache stampedes when many requests miss the same hot key. Techniques include request coalescing, jittered TTLs, stale-while-revalidate, and controlled refresh. Also protect against cache penetration from repeated requests for nonexistent keys and hot-key overload where one key receives disproportionate traffic.
- A cache is a performance layer, not automatically the source of truth.
- Define acceptable staleness and invalidation behavior.
- Plan for stampedes, hot keys, and cache outages.