CachingCache Invalidation StrategiesEasy⏱️ ~2 min

What is Cache Invalidation and Why Does It Matter?

Definition
Cache invalidation is the discipline of keeping cached data acceptably fresh while preserving the performance benefits of caching. When you cache data, you create a copy that can become stale when the source of truth changes.

The Freshness vs Performance Trade off

Caching speeds reads by avoiding origin hits but creates stale copies. The core challenge: deciding how and when to mark entries invalid or refresh them. Fast responses favor keeping data cached longer; current data favors frequent invalidation. Production systems handle 1 billion+ lookups per second at sub millisecond latency, with global purges completing in 150ms-1s.

Three Decision Axes

Timing: invalidate based on time (TTL, where Time To Live defines when data expires), when data changes (event driven), or by checking freshness on every access. Data flow: update cache synchronously with writes (write through), asynchronously after writes (write behind), or only on reads after invalidation (cache aside). Granularity: invalidate individual keys, groups using tags, or bump a generation counter invalidating everything in a namespace.

Hybrid Approaches in Production

Most systems use hybrids: cache aside reads with TTL as safety net (5-60 min for static content, 1-10s for dynamic), plus explicit event driven invalidations for correctness critical keys (permissions, inventory), and generational namespaces to avoid massive fan out when one change affects many entries.

Key Trade-off: Getting invalidation wrong causes stale data (correctness bugs), thundering herds overloading origin, or inconsistent views across cache tiers. No single strategy handles all cases.
💡 Key Takeaways
Cache invalidation balances freshness vs performance: caching speeds reads but creates stale copies that must be managed when source changes.
Three axes: timing (TTL vs event driven vs validate on access), data flow (write through vs write behind vs cache aside), granularity (per key vs tags vs generation counters).
Production hybrids: cache aside with TTL safety net, event driven for correctness critical updates, generational keys to avoid delete storms.
Scale targets: 1 billion+ lookups/second at sub millisecond latency, global purges in 150ms-1s.
📌 Interview Tips
1Explain the three axes: timing (when to invalidate), data flow (how writes update cache), granularity (how many keys per invalidation).
2Describe hybrid approach: cache aside with TTL for baseline, event driven for critical updates, generation bumps for bulk invalidation.
3Know the scale numbers: billion+ QPS, sub millisecond latency, sub second global purges.
← Back to Cache Invalidation Strategies Overview