TSTier SlateAll templates
  1. Home
  2. Templates
  3. Distributed cache

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.

  • System design
  • core
  • 8 step walkthrough
  • 4 tables
Own this board 400 creditsPractise as an interview

The first act plays here. Owned once with credits, never a subscription.

Share

  • Facebook
  • X

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.

Practise this as an interview

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

  1. 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).
  2. 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.
  3. Page 1 · Requirements - the questions that decide whether this is a cache at all, and the hit-rate and latency targets.
  4. 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.
  5. Page 4 · Availability - the two targets, and what one, four or all sixteen nodes going away does to the origin.
  6. 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.
  7. 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

Preview of the Distributed cache system design board: 15 components joined by 16 connections. The labelled board opens in Tierslate.
Board preview

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
Open this board in TierslateBrowse every board

Related system design templates

The building blocks an interview asks you to build from scratch. 18 more infrastructure primitives templates:

  • URL shortener system design - Why one tiny write and a hundred cacheable reads make two systems sharing a single table.
  • Key-value store system design - How a key finds its replicas, and why choosing how many to wait for buys overlap rather than order.
  • Rate limiter system design - Why the hard part of limiting requests is where the shared counter lives, not the algorithm.
  • Unique ID generator system design - How 64 bits buy sortable ids with no central counter, and what happens when the clock goes backwards.
  • Web crawler system design - Why a crawler that feeds on its own output needs politeness, deduplication and loop brakes more than speed.
  • Pastebin system design - Why storing text is the easy part and expiry, abuse and the read path are the design.
  • Load balancer system design - How requests are spread across servers, and why the health check matters more than the algorithm.
  • API gateway system design - Why auth, routing and limits move to one front door, and what that door costs when it falls over.
  • Login and sessions system design - How a password becomes a session, and what changes when the session is a signed token nobody can revoke.
  • Feature flags system design - Why a flag is evaluated locally from a pushed ruleset, and what a stale ruleset does during an incident.
  • Secrets manager system design - How secrets are encrypted under a key nobody holds directly, and why rotation is the feature, not storage.
  • DNS system design - How a name becomes an address through layers of caches, and why the TTL decides how fast a mistake spreads.
  • Distributed cron system design - Why a scheduled job needs a lease so it runs once, not once per server, and what a missed tick should do.
  • Background job queue system design - Why a job queue delivers at least once, and how idempotent workers and dead letters keep retries honest.
  • Code execution sandbox system design - How untrusted code is run in isolated, time-limited sandboxes, and why the queue in front of them is the real system.
  • Distributed log system design - Why an append-only partitioned log gives ordering and replay, and what replication and consumer offsets cost.
  • Consensus system design - How Raft keeps replicas agreeing through elections and a replicated log, and what a network partition really does.
  • Object storage system design - How objects are split, erasure-coded and placed across failure domains so losing disks never loses data.
Open the Distributed cache board in TierslateAll infrastructure primitives templatesAll 100 system design templates
Tierslate

tierslate.com

HomeTemplatesGuidesCompareAboutUI mockupsPrivacyTerms