Caching • Cache Invalidation StrategiesEasy⏱️ ~2 min
What is Cache Invalidation and Why Does It Matter?
Cache invalidation is the discipline of keeping cached data acceptably fresh while preserving the performance benefits of avoiding origin reads and writes. When you cache data, you create a copy that can become stale when the underlying source of truth changes. The core challenge is deciding how and when to mark a cache entry as invalid or refresh it, balancing the competing goals of serving fast responses (which favors keeping data cached longer) against showing current data (which favors frequent invalidation).
You make invalidation choices along three key axes. First, timing: do you invalidate based on time passing (Time To Live or TTL), or when data changes (event driven), or by checking freshness on every access (validate on access)? Second, data flow: do you update the cache synchronously with writes (write through), asynchronously after writes (write behind), or only on reads after invalidation (cache aside)? Third, granularity: do you invalidate individual keys, groups of related keys using tags, or bump a generation counter that invalidates everything in a namespace? Most production systems use a hybrid approach: cache aside reads with TTL as a safety net, plus explicit invalidations for hot or correctness critical keys, and generational namespaces to avoid massive fan out when one change affects many cached entries.
The numbers that matter: Meta's Memcache handles over 1 billion cache lookups per second at peak with sub millisecond median latencies within a region. Modern Content Delivery Network (CDN) providers like Fastly can propagate global invalidations in approximately 150 milliseconds median, while Cloudflare achieves sub second global purges. These concrete performance targets frame what good looks like at internet scale.
💡 Key Takeaways
•Cache invalidation solves the freshness versus performance trade off: caching speeds up reads by avoiding origin hits, but creates stale copies that must be managed when source data changes
•Three decision axes define your strategy: timing (TTL versus event driven versus validate on access), data flow (write through versus write behind versus cache aside), and granularity (per key versus tag based versus generation counters)
•Production systems typically use hybrids rather than pure strategies: cache aside with TTL for baseline safety, event driven invalidation for correctness critical updates, and generational keys to avoid delete storms
•Real scale benchmarks: Meta handles 1 billion plus cache queries per second with sub millisecond latency, CDN providers achieve global purges in 150 milliseconds to 1 second, establishing concrete performance targets
•The cost of getting it wrong includes serving stale or private data (correctness bugs), thundering herds that overload origin systems, and partial failures across cache tiers causing inconsistent user views
📌 Examples
Meta's social graph uses cache aside reads with short TTLs on many entities, plus delete on write invalidations published after durable database commits, accepting sub second staleness cross region while maintaining sub millisecond read latency within regions
Pinterest uses multi tier caching with default TTL for static assets, but switches to event driven purges for privacy sensitive changes like pin visibility updates, targeting sub second to low seconds invalidation for correctness flows
A typical e commerce product page might use 5 minute TTL for general browsing traffic (avoiding origin load), but immediately invalidate when price or inventory changes (correctness requirement), demonstrating hybrid timing strategies