Real-World Case Studies: Cache Eviction and Invalidation

✅ Source-checked. Each deep dive names its source, and we kept only what that source says. Found an error, or something the source doesn't support? Report it on the tracking issue.

How Facebook keeps memcache consistent with its databases (leases, an invalidation pipeline and a standby "Gutter" pool), and how the Caffeine library decides which entries to keep with W-TinyLFU.


👥 Deep Dive 1: Facebook Memcache, Keeping Caches Fresh

Source: Scaling Memcache at Facebook (Nishtala et al., NSDI 2013). Everything in this section comes from the paper.

Facebook's memcache deployment "handles billions of requests per second and holds trillions of items".

Leases: stale sets and thundering herds

Leases solve two problems. A stale set happens when a slow client writes an outdated value back into the cache after a newer one was invalidated. A thundering herd happens when a popular key is invalidated and many clients hit the database at once.

Invalidation from the database's commit log

"We deploy invalidation daemons (named mcsqueal) on every database. Each daemon inspects the SQL statements that its database commits, extracts any deletes, and broadcasts these deletes" to the memcache deployment. Because invalidations come from what the database actually committed, a web server crashing mid-request can't leave stale data behind. The web server that made a change also invalidates its own cluster directly, so the user sees their own write.

Gutter: a standby pool for failed servers

When a memcached server stops responding, its traffic goes to a small pool called Gutter, rather than straight to the database. "Gutter accounts for approximately 1% of the memcached servers in a cluster", and "entries in Gutter expire quickly". Requests are routed by mcrouter, a proxy that "presents a memcached server interface and routes the requests/replies to/from other servers".


☕ Deep Dive 2: Caffeine, Choosing What to Keep with W-TinyLFU

Sources: Einziger, Friedman and Manes, "TinyLFU: A Highly Efficient Cache Admission Policy" (2015), and the Caffeine project's README, Efficiency and Design pages. Everything in this section comes from those.

Caffeine is an open-source Java cache "using a Google Guava inspired API". Its README lists projects it powers, including Cassandra, Kafka, HBase and Solr.


ℹ️ An earlier version of this page credited Caffeine and W-TinyLFU to Google and said Caffeine "powers Google Guava". Caffeine is an independent project whose API was inspired by Guava's. It also called Facebook's invalidation pipeline "Wormhole", but the memcache paper describes the mcsqueal daemons. And it described Netflix's cross-region invalidation through Kafka with TTL jitter, which we couldn't source. We removed all three.

🎯 System Design Interview Takeaways

  1. Draw the stale-set race. A reader misses, reads the old value, a writer updates and invalidates, then the reader writes the old value back. Leases (or version checks) stop the last step.
  2. Invalidate from the source of truth. Tailing what the database committed, as mcsqueal does, can't miss an update the way a crashed web server can.
  3. Protect the database when a cache node dies. A small standby pool with short TTLs, like Gutter, absorbs the misses.
  4. Eviction is also an admission decision. W-TinyLFU only admits a new entry if it's likely to be used more than the one it would replace.