> For the complete documentation index, see [llms.txt](https://wiki.ainzzorl.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.ainzzorl.lol/courses/mit-6.824-distributed-systems/cache-consistency-memcached-at-facebook.md).

# Cache Consistency: Memcached at Facebook

[Paper](http://nil.csail.mit.edu/6.824/2020/papers/memcache-fb.pdf)

* We provision hundreds of memcached servers in a cluster to reduce load on databases and other services. Items are distributed across the memcached servers through consistent hashing

* Front-end servers, database, pool of memcache servers.

* Multiple regions, full DB primary-secondary DB replication.

* Multiple clusters within a region. Cluster: only FE + DB.

* Regional memcache pool for non-popular keys.

* DB is sharded.

* MC is also sharded, with consistent hashing.

* Starting a new cluster is a problem because it temporarily increases the DB load. They "cold start" new clusters by making them read from other cluster's cache first before the new cache is warm.

* One cluster can't be too big, will overload popular keys within the cluster.

* "Thundering heard". Very popular key in MC, many FEs are reading it. Someone deletes the key, and the cache is invalidated. Now everyone tries to read the DB at the same time, not good.
  * They use "Leases". When you miss a cache, it gives you a "lease". Other callers are asked to wait.
  * The owner of the lease will be allowed to put.

* If MC instance fails, the DB is exposed. The MC can be auto replaced, but it takes a while.
  * There's a small set of GUTTER servers, idle unless a MC server fails.
  * If a MC fails, the request is sent to a GUTTER instead.

* Consistency problem - there are many copies of the same data. Master DB, each DB replica, many MCs... When a write comes in, something must happen on all these copies.

* There can be races, ending up writing stale data to cache. It stays there indefinitely. Also solved with leases. When you read, you get a lease to write. When someone deletes, it invalidates the lease.

* Caching is not so much about reducing latency, but about hiding a relatively slow DB from the very high load,


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wiki.ainzzorl.lol/courses/mit-6.824-distributed-systems/cache-consistency-memcached-at-facebook.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
