Ankit Patel
WritingPapersLabsAboutContact
Paper Reviews··Updated 2025-05·2 min read·224 views

Memcache Leases and Thundering Herds at Facebook

Facebook Memcache · Nishtala et al., NSDI 2013

Peak database queries from misses on herd-prone keys: 17,000 a second without leases, 1,300 with

Deleting a stale cache entry after updating a database does not stop an older in-flight read from writing the stale value right back.

Walk through the four steps when readers populate a look-aside cache on a miss:

Order Reader Writer
1 Reads the old description from the database
2 Saves the new description
3 Deletes the cached description
4 Puts its earlier result into the cache

The writer followed textbook advice: commit to the database first, then invalidate the cache key. Yet step four leaves the cache permanently serving stale data until the key expires. The slow reader started before the write and finished after the delete.

In Nishtala et al.'s NSDI 2013 paper, Scaling Memcache at Facebook, Section 3.2.1 solves this stale-set race with a lease token. When a reader misses on a key, memcached hands back a 64-bit token bound to that key. Writing the database result into the cache requires presenting the token, and any intervening delete invalidates outstanding tokens for that key. The delayed reader in step four fails validation cleanly instead of overwriting fresh state.

# NSDI 2013 Sec. 3.2.1: two-client stale-set race prevented by 64-bit lease_token
val, lease_token = cache.get_lease("profile:42")   # Client A misses; receives 64-bit token
old_row = db.read("profile:42")                    # Client A reads v1 before Client B writes

db.write("profile:42", "v2")                       # Client B commits newer value v2
cache.delete("profile:42")                         # Invalidates active lease_token on key

ok = cache.set_if_lease_valid("profile:42", old_row, lease_token)
# ok is False: stale v1 write rejected; next miss loads v2

The cache never inspects application payloads or database timestamps. It only checks whether permission to refill that key is still valid. Where Facebook's TAO graph store moved social-graph caching behind a write-through tier that understood objects and associations, general look-aside memcached stayed payload-agnostic and relied on leases to keep client-driven refills safe.

That same token throttles thundering herds when a hot key expires under heavy traffic. By issuing only one lease per key within a short window, such as once every ten seconds, memcached tells concurrent misses to wait briefly and retry the cache rather than hammering the database in parallel.

On a production workload of keys prone to stampedes, Nishtala and colleagues measured peak database queries falling from 17,000 per second without leases to 1,300 per second with leases enabled.

One lightweight piece of state handles two classic failure modes: who gets to query the backing store now, and whether their answer is still safe to install when they return.

When I sit in a cache design review, projected hit rates matter less to me than tracing two failure paths: a slow reader crossing an invalidation, and a lease holder crashing mid-fetch while waiting callers sit behind a bounded timeout. And when correctness requires strict serialization, such as reserving the final unit of inventory, neither a warm cache nor a direct read-before-write replaces an atomic check against the authoritative store.

Continue in Paper Reviews