Facebook's front page is built from thousands of small facts: your friends' names, their latest posts, the counts under each photo. Fetching all of that from a database on every page view would melt any database. So Facebook put memcached, a simple in-memory cache, in front of it.
But a cache that works for one server fails in surprising ways at their scale. This paper is a tour of those failures and their fixes. Each fix is small, clever, and easy to reuse in your own designs: leases, a gutter pool, invalidation from the database log, and regional pools.
Use a look-aside cache, and treat the database as the source of truth. When you write, delete the cached key. Add leases so a burst of misses becomes one database query, and so a slow reader cannot overwrite fresh data with old data.
The basic pattern: a look-aside cache
In a look-aside cache, the application does the work. On a read it asks the cache first. If the key is missing, it reads the database and then puts the value in the cache. On a write it updates the database, then deletes the cached key, so the next read will load a fresh copy.
Why delete instead of update? Deletes are idempotent: doing one twice does no harm. An update that arrives out of order can leave an old value in the cache. Deleting is simpler to reason about, and the next reader repairs the key.
