Memcached: Caching Discipline at Scale

A deliberately simple cache - and the stampede, eviction and cold-start problems that simplicity hands to your application.

Taught by Noshad Alam 7 lessons 47 min Published 30 Nov 2026

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

Including it in a database curriculum needs a word of explanation. Memcached is not a database and should never be treated as one: **anything stored only in Memcached is already lost**. What it teaches is **caching discipline**, which is a genuinely separate engineering skill and one that transfers directly to Redis, to CDN caching, to application-level caches and to any system with a fast layer in front of a slow one. The problems are the same everywhere - stampedes, staleness, invalidation, cold starts, key distribution - and Memcached is the best place to learn them because it removes everything else, leaving the caching problem on its own.

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

This course assumes the programme's shared foundations. It is the shortest and most approachable course in the programme, and the concepts in Modules 3 and 5 are the ones that make experienced engineers nod.

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

  1. 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 →
  2. 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 →
  3. 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 →
  4. 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 →
  5. 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.

Course guide How to work through this course Why this short course is worth the time, the two experiments that matter, what transfers, and what to skip. 3 min read

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.