Infrastructure primitives
Distributed cache - the ring, and what a node leaving costs
Why a hash ring moves a sliver of keys when a node leaves, and why the hot key and the miss storm remain.
The first act plays here. Owned once with credits, never a subscription.
What this board gets wrong on purpose
The tension
Consistent hashing balances how many KEYS each node holds. It has nothing to say about how many REQUESTS each key gets, so one celebrity key is one slot on one node and no rebalancing can split it - the only fixes are to replicate that key to every node or to cache it in the application, and the near-cache drawn here has no invalidation at all, so it trades a hot spot for stale reads nobody can clear. The second cost is worse because it looks like success: removing one node of sixteen moves only 6.25 percent of the keyspace, and every one of those keys misses 100 percent of the time until it is refilled, so a routine rolling restart doubles the load arriving at the origin database while the dashboard reports a 94 percent hit rate and looks fine.
Practise this as an interview
Take Distributed cache - the ring, and what a node leaving costs as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.
Start here
Why this design
A distributed cache is the most common component put in front of a database and the one most often misunderstood, because the hard part is not storing bytes in RAM - it is deciding which machine owns a key with no coordinator, and surviving the moment that answer changes. Interviewers use it to see whether you know why consistent hashing beats modulo, whether you notice that it balances keys rather than traffic, and whether you treat the hit rate as a capacity multiplier on the origin rather than as a performance nicety. Teams running one in production meet the same three failures this board draws: a celebrity key, a miss storm when a node leaves, and a cache that has quietly become load bearing.
What you will learn
- Why hash(key) mod N moves 94 percent of keys when N goes from 16 to 17, and why a ring with 160 virtual nodes per member moves only 1/N and spreads it evenly.
- How the client library computes placement locally in microseconds, and why the membership view - not any server - is the only shared state that matters.
- Why one point of hit rate is about 20 percent more origin load, and why the real availability requirement is the origin surviving a 0 percent hit rate.
- Why a hot key cannot be fixed by rebalancing, and what the two real fixes - replicate-everywhere and an in-process near cache - each cost.
- How single-flight loading and warming a joining node turn a miss cliff into a curve during routine rolling restarts.
- Why the only durable schema a cache has is its control plane: topology versions, members, namespace policy and hot-key samples.
How to read this board
- Diagram, left to right along the read path: CALLER · AND ITS OWN CACHE, then PLACEMENT (the client library and the ring hex), then THE RING · FOUR OF SIXTEEN (the members, node D about to leave).
- Then the right column: WHAT THE RING DOES NOT SOLVE (hot key, eviction, single flight) and ORIGIN · SIZED FOR 5% OF READS. Then MEMBERSHIP along the bottom, off the request path. Press play for the eight-step walkthrough.
- Page 1 · Requirements - the questions that decide whether this is a cache at all, and the hit-rate and latency targets.
- Pages 2 and 3 · Capacity and Storage - traffic, the ring size (sized by RAM, not throughput), bandwidth, the hot key, and why durable storage is zero.
- Page 4 · Availability - the two targets, and what one, four or all sixteen nodes going away does to the origin.
- Page 5 · How it works - one get told end to end, then Page 6 · Deployment plan - regions, warming, and why a hash change is not rollable.
- Finally the DATA MODEL below the band: the four control-plane tables, because a cache has no schema of its own.
1 · Requirements
Ask these before drawing anything
- What is being cached, and what does a stale answer cost? A stale product description is nothing; a stale permission check is a security incident. This answer decides whether TTL alone is acceptable or whether explicit invalidation is mandatory.
- Is this a cache or a store? A cache may lose everything and the system still works, slowly. If losing it loses data, it is not a cache and consistent hashing is the wrong tool - use the key-value store board instead.
- What is the working set, and what is the total key space? Caching is only economic when a small fraction of keys serves a large fraction of reads. If access is uniform, a cache is just expensive RAM.
- How big is the largest value, and how hot is the hottest key? Both have answers that break the design, and both are usually unknown until somebody measures.
- Who invalidates - the writer, a TTL, or nobody? Nobody is a valid answer and should be said out loud rather than discovered.
- Can the origin survive a total cache loss? If not, the cache is load bearing, and the availability requirement is really the origin capacity requirement.
Functional
- get, set and delete on an opaque byte value, keyed by an opaque string.
- A per-key TTL, set by the caller, defaulting per namespace.
- Read-through on a miss, with exactly one fill per key regardless of how many callers missed at once.
- Add and remove nodes without restarting clients and without moving more than one node share of the keyspace.
- Report per-key request counts well enough to identify a hot key while it is hot, not the next morning.
Non-functional
- p99 of 1 ms for a hit, measured at the client including the network. If a cache is not a great deal faster than the origin it is a liability rather than an optimisation.
- Hit rate at or above 95 percent in steady state. Everything below that arrives at a database sized for 5 percent.
- 99.9 percent availability for the cache, and a system that stays UP at 0 percent cache availability. Those two numbers are different requirements and the second is the important one.
- Adding or removing a node moves at most 1/N of keys and needs no client restart.
- The cache never becomes the source of truth. Any value in it can be recomputed from the origin.
Out of scope
- Durability. Nothing here is written to disk, and that is a feature - it is why a node restart is quick and why the capacity maths is only about RAM.
- Cross-region replication of cache contents. Each region caches for itself; see the deployment page.
- Transactions across keys. A cache with transactions is a database wearing a disguise.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 4-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 4tables24 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan