Skip to content

Case study: Distributed cache

Design a Memcached or Redis cache tier: partitioning, hot keys, invalidation races, and surviving a node that restarts empty.

35 min9 steps

By the end you can
  • Size a cache tier from the database's capacity and predict the load when a node is lost
  • Partition keys across cache nodes and handle hot keys
  • Keep cached data correct with delete-on-write and leases
  • Predict what a cold restart, a slow cache and write-heavy hot keys do to the database

Requirements

Operations

  • get, set with TTL, delete; keys <=250 B; values <=1 MB

Guarantees

  • Sub-ms p99; database remains below capacity after node loss

What number should you ask for first when designing a cache tier?

Answer this step to continue.

What this lesson simplifies

  • The simulated cache tier is one component; per-node failure and hot-shard overload are discussed but not simulated.
  • Coalescing is modelled as per-key waiting for the first fill; leases' stale-set protection is discussed only.
  • The runnable cell runs 4,000 requests/s over 10,000 keys with an 8-worker database.

Sources

  1. R. Nishtala et al.. Scaling Memcache at Facebook, 2013.
  2. D. Karger et al.. Consistent hashing and random trees, 1997.
  3. N. Bronson, A. Aghayev, A. Charapko, T. Zhu. Metastable Failures in Distributed Systems, 2021.

Reviewed by Content lead on . Revision 1.