Notesfoundations🌱 seedling

Cache Invalidation

Also: Invalidation · TTL · Time to Live · Cache Lease

Invalidation is how a cache learns its copy is out of date: expire it after a TTL, delete it when the source changes, or derive deletes from the source's change log. Races mean you need a TTL as the backstop.

Why it exists

A cache is a copy, and copies go stale. Without invalidation, users read old data indefinitely.

How it works

  • TTL: entries expire after a fixed lifetime. Self-healing, with staleness bounded by the TTL.
  • Explicit: the writer deletes or updates the key after writing the source. Fresh, but every write path must comply.
  • Change-driven: tail the database's commit log (Change Data Capture) and fan out deletes. Durable, replayable, and covers writes from any client.
  • Leases: on a miss, the cache issues a token for the key; a delete voids it; a later fill with a voided token is rejected. This stops a slow reader writing back a value that's already been superseded.

Trade-offs

Approach Gains Costs
TTL only trivial, self-healing up to one TTL of staleness; misses on expiry
explicit delete fresh easy to miss a write path; races
log-driven complete, replayable pipeline lag; more infrastructure

In practice

  • Facebook: mcsqueal daemons read the MySQL commit log and broadcast deletes to every cache cluster. Leases (64-bit tokens per key) prevent stale sets.

Pitfalls

  • No TTL: one lost invalidation or race leaves a stale entry forever.
  • Invalidate-then-write order: deleting before the database commit lets a concurrent read refill the old value. Delete after the commit.

Recall

These cards are also in your review deck, scheduled with spaced repetition.