Memcached: Caching Discipline at Scale
A deliberately simple cache - and the stampede, eviction and cold-start problems that simplicity hands to your application.
Memcached does one thing: it stores bytes under a key, in memory, with an optional expiry, and gives them back fast. It has no persistence, no replication, no data structures, no transactions, no queries and no clustering. **That is the design, not a gap in it** - and it is why Memcached has been running in front of some of the largest sites in the world for twenty years with very little drama.
Overview
Three things carry the course. **Cache-aside correctness**: what to delete and when, and why updating a cache entry is more dangerous than deleting it. **Stampede protection**: what happens when a popular key expires and a thousand requests hit the database in the same instant, and the three defences against it. And **cold cache recovery**: what your system does when the cache is empty, because node replacement, a restart or a deployment makes it empty, and a system that cannot survive a cold cache is a system that depends on its cache - which is a different architecture from one that is accelerated by it.
Seven modules, about 45 hours. The capstone is a high-traffic catalogue cache with stampede protection and a node-loss benchmark.
Introduction
For a sandbox, `docker run -p 11211:11211 memcached:1.6 -m 64 -vv` gives you a server with verbose logging, which is instructive. You need **several** instances for Modules 4 and 5, which Docker Compose handles. You also need a slow backing store to cache - a small PostgreSQL or SQLite with a deliberately expensive query is ideal, because a cache in front of something fast teaches you nothing.
Learn the **text protocol with `telnet` or `nc` first**. It is a handful of commands, it is readable, and seeing `STORED`, `END` and `EXISTS` come back teaches you what your client library abstracts away - which matters here more than usual, because the two most consequential behaviours in Memcached (client-side key hashing and the CAS token) live in the client, not the server.
One framing to carry through. Every cache decision is a trade between **freshness, load and complexity**, and you cannot optimise all three. A short TTL is fresh and loads the database; a long TTL is cheap and serves stale data; explicit invalidation is both fresh and cheap and is the most complex and easiest to get wrong. The right answer is per-key and depends on what the data is - and being able to say which you chose and why is what this course is for.
What you will be able to do
- Use the Memcached text protocol directly and read its responses
- State precisely what Memcached does not do, and why that is the design
- Design a key naming scheme within the 250-byte key limit
- Manage TTLs deliberately, including the 30-day boundary behaviour
- Implement cache-aside correctly, and explain why you delete rather than update
- Serialise values appropriately and account for the 1 MB item limit
- Use multi-get effectively and explain why it matters more than it looks
- Use CAS for safe read-modify-write, and atomic counters
- Implement namespace invalidation for group expiry without key enumeration
- Diagnose and defend against a cache stampede, three ways
- Implement request coalescing and negative caching
- Explain the slab allocator and diagnose slab calcification
- Read eviction and LRU metrics, and distinguish eviction from expiry
- Explain client-side consistent hashing and what it saves on node loss
- Plan and measure a cold cache recovery
- Secure a deployment with private networking, SASL and connection limits
- Produce a deployment, sizing and cost checklist
What each level covers
5 levels · 7 lessons
-
Beginner - Foundations
The protocol and the limits; then the cache-aside patterns that do the real work.
2 lessons 12 min of reading
Start with The Protocol, Keys, Values and TTLs → -
Intermediate - Application Behaviour
The stampede, request coalescing, negative caching and the honest handling of stale data.
1 lesson 7 min of reading
Start with Stampedes, Coalescing, Negative Caching and Staleness → -
Advanced - Performance and Reliability
The slab allocator and eviction metrics; then node loss, consistent hashing and the cold cache.
2 lessons 15 min of reading
Start with Slabs, LRU, Eviction Metrics and Client Hashing → -
Expert - Security and Production Architecture
The networking rule that is the whole security story, SASL where available, and the managed trade-off.
1 lesson 7 min of reading
Start with Networking, SASL, Limits, Monitoring and Managed Caches → -
Capstone
One catalogue cache, defended on stampede protection, a measured cold start and a stated capacity ceiling.
1 lesson 6 min of reading
Start with Capstone - High-Traffic Catalogue Cache →
Guides and articles
Longer reading about this subject, written alongside the course.
Before you start
- Memcached 1.6 or later, and Docker Compose for the multi-node modules
- telnet or netcat, and one programming language with a Memcached client
- A slow backing store to cache - PostgreSQL or SQLite with an expensive query
- The programme's shared foundations
Common questions
- What does the Memcached: Caching Discipline at Scale course cover?
- It is 5 levels and 7 lessons: Beginner - Foundations; Intermediate - Application Behaviour; Advanced - Performance and Reliability; Expert - Security and Production Architecture; Capstone. Each level ends with a quiz, and the last one is an exam over the whole course.
- How long does it take?
- About 47 minutes of reading and exercises - roughly 0.8 hours. It is self-paced with no cohort and no start date, so you can leave and come back and the site remembers where you were.
- What do I need to know first?
- Memcached 1.6 or later, and Docker Compose for the multi-node modules. telnet or netcat, and one programming language with a Memcached client. A slow backing store to cache - PostgreSQL or SQLite with an expensive query. The programme's shared foundations.
- Is it free?
- Yes. Every lesson, example and quiz in this course is free to read, and you do not need an account to read them. Signing in only adds progress tracking and quiz results.
- What will I be able to do afterwards?
- Use the Memcached text protocol directly and read its responses. State precisely what Memcached does not do, and why that is the design. Design a key naming scheme within the 250-byte key limit. Manage TTLs deliberately, including the 30-day boundary behaviour. Implement cache-aside correctly, and explain why you delete rather than update. Serialise values appropriately and account for the 1 MB item limit. Use multi-get effectively and explain why it matters more than it looks. Use CAS for safe read-modify-write, and atomic counters. Implement namespace invalidation for group expiry without key enumeration. Diagnose and defend against a cache stampede, three ways. Implement request coalescing and negative caching. Explain the slab allocator and diagnose slab calcification. Read eviction and LRU metrics, and distinguish eviction from expiry. Explain client-side consistent hashing and what it saves on node loss. Plan and measure a cold cache recovery. Secure a deployment with private networking, SASL and connection limits. Produce a deployment, sizing and cost checklist.