A guide to Memcached: Caching Discipline at Scale

How to work through this course

Seven modules, about forty-five hours. It is the shortest course in the programme, and it is the one whose lessons transfer the furthest.

A word on why Memcached is here at all. It is not a database, and nothing in this course suggests treating it as one. What it is, is the cleanest possible environment for learning caching discipline: because Memcached has no data structures, no persistence, no replication and no clustering, every problem you encounter is a caching problem rather than a feature of the tool. The stampede, the invalidation race, the cold start, the key distribution question - these are identical in Redis, in a CDN, in an application-level cache and in a browser, and this is the best place to meet them without anything else in the way.

Use a genuinely slow backing store. A cache in front of a fast query teaches you nothing: the stampede does not hurt, the cold start recovers instantly, and every defence looks unnecessary. Put a deliberately expensive query behind it - 200 milliseconds or more - and every measurement in this course becomes meaningful.

Two experiments are worth more than the rest of the course combined.

The first is the stampede, caused on purpose, in Module 3. Warm one popular key, run 500 concurrent clients, let it expire, and watch your database receive a thousand identical queries in the same instant. Make it fall over if you can. Then fix it with each defence in turn and watch the query count go to one. Nobody who has done this forgets to jitter a TTL again.

The second is flush_all under full load, in Module 5. It takes thirty seconds to run and it answers the only question that matters about a cache: does your system survive without it? Most systems that have never been tested this way do not, and the discovery is normally made during an incident rather than during a laboratory.

Watch evicted_time and let it change how you think about TTLs. It is the age of the last evicted item, which is to say your actual cache lifetime. When it sits well below your configured TTLs, your TTLs are a wish - items are being pushed out long before they expire, and raising the TTL does nothing at all. Most people tune TTLs for years without knowing this number.

Check your client's hashing scheme, explicitly, today. Modulo hashing turns the loss of one node in five into the loss of 80% of your cache. Consistent hashing makes it 20%. It is a single configuration option, some clients still default to modulo, and the difference is the difference between an incident and an outage. This is the single highest-value five minutes in the course.

Write the capacity statement for something real. Module 5 asks for database queries per second at steady state, with one node lost, and cold, against your database's measured ceiling. Do it for a system you actually work on. Most people have never calculated it, and a surprising proportion discover that their cold cache exceeds their database by a factor they would not have guessed - which means their cache is not an accelerator, it is a dependency.

What to skip on a first pass. The binary protocol details, unless SASL requires them. The meta commands (mg, ms, md), which are the modern protocol and worth knowing later. External flash storage (extstore). Proxy layers such as mcrouter or the built-in proxy, which are genuinely useful at large scale and are a separate subject. UDP entirely - it is off by default and should stay off. And stats cachedump, which is unsupported, truncated and misleading.

Where to look things up. The official wiki is the reference, and its ConfiguringServer and Programming pages are the two to read. Brad Fitzpatrick's original LiveJournal notes are a short and clarifying read on why it is the way it is. And the Facebook paper Scaling Memcache at Facebook is the best engineering writing on caching at scale that exists - read it after Module 5, when you have the vocabulary for it, and it will reward the time.

One closing thought. Of all the courses in this programme, this is the one whose content you are most likely to use next week, whatever database you work with. Stampedes, invalidation, staleness windows, cold starts and key distribution are not Memcached problems - they are the problems of having a fast layer in front of a slow one, which is nearly every system of any size. Memcached simply removes everything else, so you can see them clearly.

memcached database