Systemsfor Aravinda
0 / 78 core
Week 12 · Production stories

Facebook's memcache

1 hours · core path

Read first: Caching

In one sitting

The 2013 memcache paper describes a demand-filled lookaside cache in front of many databases. Leases stop stale fills and stampedes, a gutter pool absorbs a dead server's misses, and invalidation on write is the correctness path. The later router mcrouter is a separate design and is not part of this paper.

A single user request can touch a large number of keys. The databases behind those keys cannot absorb that read rate on every view, and they should not absorb the thundering herd when a cache server dies or when a popular key is missing. The 2013 memcache paper, by Nishtala and others at NSDI, is Facebook’s design for that gap: a demand-filled lookaside cache in front of a fleet of databases. Four ideas carry it. Lookaside fills. Leases on a miss. A gutter pool for a dead cache server. Invalidation when you write. A later router, mcrouter, is a different story. This page does not fold it into the paper.

The problem

The read path is a fan-in of small items. A page is not one query. It is many keys, and the request waits on the slow ones. Put that on the latency page’s rule: the user waits on the maximum, and a fanout of independent keys makes a “rare” slow key ordinary. The cache exists so most of those keys never become a database read.

The cache cannot be a copy you loaded in advance of the whole database. The working set is whatever people are actually viewing. A demand-filled cache stores a key because a request missed and someone filled it. Keys nobody reads do not occupy RAM. The cost shows up at the misses: the first reader of a key, the moment after an invalidation, and the moment a server that held a slice of the key space goes away.

Two bugs sit in that miss path, and they look similar until you separate them. The first is a stampede. A popular key is missing, and every waiter reads the database and then writes the cache. The database sees a spike, and the cache writes race each other. The second is a stale fill. A slow reader fetched the old value, a write happened, and the slow reader stores the old value on top of the new one. You have just undone the write in the cache. Throughput fixes, such as a bigger database, do not address a race that puts the wrong bytes back.

A third bug is a dead process rather than a missing key. One cache server held a slice of the keys. It dies. Every key it held is a miss at once. If each miss goes to the database, the database takes the whole working set of that server for as long as the server is gone. That is the outage you built the cache to prevent, arriving as a failure of the cache itself.

The 2013 answer is a lookaside cache with a lease for the race and the stampede, a small spare pool for the dead server, and invalidation on the write path so a hit means “nobody has written since we filled.” mcrouter, a later router in front of cache fleets, is how you might talk to a huge number of servers in a design that came after this paper. It is not the mechanism behind leases, gutter, or invalidation. Leave it out of the explanation of those three.

The design

Lookaside, demand-filled. The application asks the cache. On a hit, it uses the value. On a miss, it reads the database and fills the cache. This course calls that shape cache-aside. The paper’s word is lookaside. Same path. Demand-filled means you did not try to preload the database into RAM. A key appears because a request needed it. A restart of the cache starts empty, and the databases see the misses until the working set is warm. That cold start is the bill for not holding a full copy.

Pools. The key space is spread across servers. One machine holds a slice, not the universe. That is partitioning of the cache. Clusters are pooled and replicated within a region, so inside the region a key is not entrusted to a single copy. Replication here is a cache replica, not the database’s durability story. You still invalidate. A second copy that never hears about a write will serve the old value and feel like a hit.

Leases. A miss returns a token. The filler reads the database and sets the value back, and the set must present the token. A stale token is rejected. That single rule does two jobs.

The first job is the stampede. One filler holds the lease and populates the key. Other requests for the same key wait instead of each reading the database and each calling set. You turned N misses into one database read, which is the stampede control the caching page wants from a lock, a lease, or single-flight.

The second job is the stale fill. Walk it as a teaching trace. The token names are labels, not a format from the paper.

key K is missing
request A receives token T1 and starts a database read

other requests for K wait on that fill

a write commits to the database and invalidates K
token T1 is now stale

A finishes the slow read and sets K with T1
the cache rejects the set

a later reader receives a fresh token, reads the database,
and fills the post-write value

Without the token check, A’s set would store the pre-write value after the invalidation, and the next hits would serve bytes the database no longer holds. The lease makes that set lose. The write’s invalidation is what made the token stale. The two mechanisms are a pair.

Gutter. A small spare pool absorbs traffic for a cache server that has died, so the database does not take the whole miss. The servers that remain should not each discover the hole by missing, and the database should not become the cache for that slice. Traffic for the dead server goes to the gutter pool instead. The pool is a spare, not a second full copy of the fleet. It exists for the failure “this one server is gone.” It does not exist to fix a stale token on a server that is still alive.

Invalidation on write. The correctness path is invalidation when you write. You update the database, you invalidate the key, and the next read misses, reads the new value, and fills. Hits after that fill see the new value. If the invalidation never arrives, the cache keeps serving the old one, and it looks healthy while it does it. A lease does not run, because nobody is filling. A gutter does not run, because no server died. The caching lesson’s backstop, for a system you build yourself, is a TTL that bounds how long a lost invalidation can live. That TTL is a bound for your own cache. The 2013 correctness path is the invalidation.

Put a fanout number on the “large number of keys” only as an assumption, so the max is visible. This is not a count from the paper.

assumed keys on one request     40
assumed independent chance a key is slow   1 percent
chance every key is fast        0.99^40 ≈ 0.67
chance the request waits on at least one slow key ≈ 0.33

About one request in three, under those assumptions, feels a slow key even though each key looks fine 99 percent of the time. A cache that turns most of the 40 into hits is how you shrink the fanout that actually reaches a database. A cache that misses all 40 at once, because a server died and you had no spare, hands the database a stampede shaped like your most expensive page.

flowchart TD
  app[Application]
  cache[Lookaside cache]
  fill[Database fill on a miss]
  gutter[Gutter pool]
  app --> cache
  cache --> fill
  fill --> cache
  cache --> gutter

The ordinary miss is the fill arrow: one lease holder reads the database and sets the key. The gutter is the alternate place traffic goes when the cache server that should have answered is dead. Invalidation is not a separate box. It is what the writer does to the cache so the next read takes the fill path and gets the new value.

What they traded

Choice What you gain What you spend
Lookaside, demand-filled RAM holds keys people actually read Misses hit the database, including a cold cache after a restart
Lease token on a miss One filler, and a slow set cannot overwrite a newer value Fillers must present the token, and a rejected set has to read again
Others wait on the lease holder A popular miss does not become a database stampede Waiters depend on that filler finishing or being abandoned
Invalidate on write The next read can see the new value A lost invalidation leaves a confident stale hit
Gutter pool for a dead server The database does not take that server’s whole miss A spare pool to run, and it starts without the dead server’s keys
Pooled and replicated in the region One server is not the entire key space or the only copy Invalidation has to reach the copies, and you bought more memory
Many keys per request The page is assembled from small cached items The request waits on the slowest key in the fanout

The stale-fill failure is the one leases are for. A read of K starts, the database then commits a write, the invalidation deletes the cached key, and the in-flight read finally calls set with the value it fetched at the beginning. If that set succeeds, every subsequent hit is the old value, and your write path looks correct in the database. The token is stale, the set is rejected, and the next reader fills from the post-write row. Skip the token check and the race comes back the first time a fill is slower than a write.

The dead-server failure is the one gutter is for. Server S owned a slice. S is gone. Every key on S misses together. That is not a popular key. That is a popular server. Sending the slice’s traffic to a small spare pool keeps the database from becoming the replacement cache. The lease on a live server would not have helped, because the process that issued tokens is the process that died. Gutter does not make the spare warm. It gives the misses a place to land that is not the database for every request of every key S held.

The lost-invalidation failure is the one neither lease nor gutter sees. The database has the new value. One cache replica was told to drop the key. Another was not, or the only message was dropped. Readers of the stale replica take the hit path and never open the database. Your error rate stays quiet. The user sees yesterday’s value. A TTL, in a system you run yourself, is how you bound that window when an invalidation is lost. It is a backstop from the caching lesson, not a substitute you can describe as the 2013 correctness path. The correctness path is still “invalidate on write,” and it has to be true of the copies you actually read.

What to steal

Steal lookaside, invalidation on write, and a single-flight or a lease on the miss. Those three are the design of a cache in front of a database at any size where a stampede or a stale set would hurt. The application reads the cache, fills on a miss, and invalidates the key when it writes the database. One filler populates a missing key while everyone else waits. A filler that lost the race with a newer write is not allowed to store what it read. You can build that in one process before you own a fleet.

Steal the gutter as an idea, not as a tier you must deploy on day one. The idea is that a dead cache server’s traffic needs a spare, or the database takes the whole miss. A team of five can implement the spare as a second cache process, or accept that one server’s misses hit the database if the database can take that slice and you have measured the slice. Standing up a regional replica mesh and a dedicated gutter pool before you have a fleet is how you operate Facebook’s diagram with none of Facebook’s reads. Clusters pooled and replicated within a region are the paper’s scale. Your first cache is one pool of one, with invalidation you can prove runs.

Do not import mcrouter into this design. The 2013 paper is leases, gutter, lookaside, and invalidation. The router is a later way to direct traffic at a large fleet. Mixing them produces a story where a routing layer gets credit for a race it does not solve. A stale set is a token problem. A dead server is a spare-pool problem. A stale hit after a write is an invalidation problem. A router is none of those.

When you draw this in a design round, say who fills, who is allowed to set, and what a write does to the key. Then say what happens when the cache process is gone. Those four sentences are the paper. The rest is fleet size you do not have yet.

Check yourself

A miss returns token T1, and during the database read a write invalidates the key. The slow filler then sets the value with T1. What should the cache do, and what bug does that stop?

Reject the set. T1 is stale because a newer update won before the fill landed. Accepting the set would store the pre-write value on top of the invalidation, and later hits would serve bytes the database no longer holds. The lease is also why other readers waited on one filler instead of stampeding the database. The token names here are a teaching trace, not a format from the paper.

One cache server dies, and it held a slice of the key space. What is the gutter pool for?

It is a small spare that absorbs traffic for the dead server so the database does not take the whole miss. Every key that lived on that server would otherwise miss at once. A lease cannot help, because the server that would issue tokens is gone. The gutter is a spare for that failure, not a second full copy of the cache and not the fix for a stale fill on a live server.

You update the database and the invalidation never reaches a cache replica that still holds the key. Why do leases and the gutter not correct the next read?

The next read is a hit on the stale replica. A lease runs on a miss, when someone is filling. A gutter runs when a cache server has died. Neither is this case. Invalidation on write is the correctness path, and it has to reach the copies that can serve the key. A TTL can bound a lost invalidation in a system you build yourself. That backstop is the caching lesson, not the 2013 paper’s correctness path.

A teammate explains the 2013 design as mcrouter plus a cache. Which pieces did they skip, and where does the router actually belong?

The paper is a demand-filled lookaside cache: a lease token on a miss, a gutter pool for a dead server, and invalidation on write, with clusters pooled and replicated within a region. mcrouter is a later router in front of cache fleets. It is a separate story. It does not replace the lease, the gutter, or the invalidation, and it should not be merged into the 2013 paper.

Based on: Facebook memcache, NSDI 2013, Nishtala and others.