Caching

Cache Invalidation

"There are only two hard problems in computer science: cache invalidation and naming things." Getting a cache to stop serving stale data — without either thrashing the source of truth or serving it forever — is genuinely hard.
  • TTL-based expiry: simple and bounded, but the bound is arbitrary — too short thrashes the database, too long serves stale data for longer than acceptable
  • Explicit invalidation on write: correct in principle, but every code path that writes the source of truth must remember to invalidate the cache — one missed path reintroduces staleness silently
  • Write-through sidesteps invalidation by keeping the cache updated at write time instead of correcting it after the fact
  • Versioned or tagged keys (embedding a content hash or version number in the cache key) avoid invalidation entirely — an old version simply ages out on its own, never invalidated because it is never looked up again
  • Invalidating a distributed cache cluster requires a broadcast mechanism (pub/sub to every node) — and there is always a real, non-zero propagation window before every node has processed it
Invalidation broadcast across cache nodes
Every node must receive and process the message before the invalidation is fully effective — a slow or disconnected node keeps serving the stale value until it does
Invalidation strategies compared
StrategyCorrectnessOperational cost
TTL onlyBounded staleness, not zeroVery low — no coordination needed
Explicit invalidationCorrect if every write path remembersModerate — broadcast + race handling
Versioned keysCorrect by constructionHigher memory (old versions linger until TTL)
Sources
  • Designing Data-Intensive ApplicationsCh. 11 — Derived Data (cache maintenance)
  • System Design Interview – An Insider's Guide, Volume 2Ch. 1 — Cache Invalidation