System design paper · 2013

Memcache at Facebook

Nishtala, Fugal, Grimm, Kwiatkowski, Lee, Li, McElroy, Paleczny, Peek, Saab, Stafford, Tung and Venkataramani · NSDI 2013

How Facebook grew a plain in-memory cache into a worldwide system handling over a billion requests a second, and what it gave up to do it.

A web server asks the cache first. On a miss it asks the database and stores the answer. On a write it changes the database and deletes the cached copy. One popular Facebook page fetched 521 values this way, on average.

You’ll learn to

  • Explain how a cache that sits beside a database can end up holding old data, and how a lease stops both that and a flood of repeat reads.
  • Work out why asking hundreds of servers for data at once floods the network, and how limiting requests in flight fixes it.
  • Trace how one database keeps many caches, in many data centres, from serving old data, including the marker that sends a read to the primary database.
  • Name what Facebook and others later changed in this design, and why.

Read first: Consistent hashing

The idea in short

Facebook builds each page from hundreds of small pieces of data. Reading them all from a database would crush it, so the pieces are kept in a cache: fast memory that holds copies of recent answers. The paper describes how that cache grew from one cluster of servers to many data centres, handling over a billion requests a second.

The same three problems return at every size. Copies go out of date when the database changes. One popular item that drops out of the cache can send a flood of requests to the database. And a page that needs hundreds of pieces at once makes many servers answer together and swamp the network. The authors fix each with a small rule, and accept that a copy may be briefly out of date as the price of keeping the database alive.

The problem

By 2013 Facebook had over a billion users, and nearly everything they did was reading. The authors say users consume an order of magnitude more content than they create, so the workload is mostly fetching data.

Every read is expensive to answer from scratch. Loading one of Facebook’s popular pages fetched 521 separate pieces of data on average (friends, photos, comments, settings), and 1,740 at the 95th percentile. That means the slowest 5% of loads of that page needed more than 1,740. The data lives in several places, including MySQL databases and other back-end services.

The fix is a : a store that keeps copies of answers already fetched, so repeat requests never reach the database. Facebook used memcached, an open-source program that keeps key-value pairs in the memory of one machine. A key is a name, such as the name of one user’s profile, and the value is the data stored under it. The program offers three actions: set, get and delete. The servers never talk to each other.

The paper uses two spellings on purpose. Memcached is the program on one machine. Memcache is the distributed system Facebook built from thousands of those machines.

Memory is fast but small, and it forgets when a machine restarts. So the cache is never the owner of the data. The database is. The cache may drop any item at any time, and the system has to cope.

The scale was the hard part. The paper says memcache handled over a billion requests a second, counting every kind of request, and held trillions of items. At that size, a plain cache fails in three ways. Its copies can be old, because the database changes under it. A burst of requests for the same missing item can reach the database all together. And when each page asks hundreds of servers at once, the network itself becomes the bottleneck.

So the question the paper answers is: how do you grow a one-machine cache into a worldwide system without letting the database fall over or the data go badly out of date?

The design choices

Three decisions shape everything else.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

Scaling Memcache at Facebook by Nishtala, Fugal, Grimm, Kwiatkowski, Lee, Li, McElroy, Paleczny, Peek, Saab, Stafford, Tung and Venkataramani, NSDI 2013.

Read the original paper

More System design papers

All papers