Vivcre Learn learn it · write it · retain it

← All posts

System Design #consistent-hashing#caching#sharding#virtual-nodes#system-design

Consistent Hashing: Resize Without a Stampede

9 Jul 2026

You run 4 cache servers and route each key with hash(key) % 4. You add a fifth server. What just happened to your database?

hash(key) % N is coordinator-free and even, but changing N remaps ~80% of keys — a cache stampede on a routine resize. Consistent hashing places servers and keys on a ring so a join or leave moves only ~1/N of keys and leaves the rest warm.

The landmine: hash % N

Modulo routing is tempting: no coordinator, keys spread evenly. But watch what one added server does. Going from N=4 to N=5, hash(key) % 4 and hash(key) % 5 agree only occasionally — for a good hash, roughly 80% of all keys now map to a different server, not the 25% you’d hope.

Those 80% are looked up on servers that don’t have them → 80% instant miss rate → every miss falls through to the database → a cache stampede. A cache meant to shield the database becomes the thing that knocks it over — triggered by a single routine event: a node dying, or an autoscaler adding capacity. hash % N is a landmine wired to N.

The fix: a hash ring

Stop thinking of a flat list you take modulo over. Picture a ring of hash values, 0 to 2³², wrapping back to 0. Hash the servers onto points on the ring (by name/IP). To place a key, hash it to a point on the same ring and walk clockwise to the first server you meet — that server owns the key. Same formula for everyone, no coordinator: every client computes the same owner from the ring alone.

S1 S2 S3 S4 key walk clockwise →
The key hashes between S1 and S2; walking clockwise, S2 owns it. Each server owns the arc just before it.

Why this survives a resize

A server owns exactly the arc of the ring immediately before it (counter-clockwise to the previous server). So:

Either way, ~1/N of keys move, not ~all of them — a small, survivable trickle of misses instead of a database-killing stampede. Note it stays coordinator-free: the owner is a formula over the ring, not a decision by a leader (unlike Raft) — because here you want determinism, not an authority.

Refinement — virtual nodes. With only a few servers at a few random points, arcs come out uneven: one server owns a huge arc and overloads, and when a node dies its whole arc dumps on one unlucky neighbor. Fix: hash each physical server to many ring points (~100–200 "vnodes"). Ownership scatters into many small arcs, evening load, and a death redistributes across many neighbors. This is what Cassandra, DynamoDB, and memcached clients actually deploy.
Recall checkpoint

1. Why does going 4→5 with `hash % N` move ~80% of keys, not 25%? `hash%4` and `hash%5` rarely agree, so most keys land on a different server — the remap is ~(N−1)/N, not 1/N.

2. What two problems do virtual nodes fix? Uneven arcs (one server overloaded) and a death dumping its whole arc onto a single neighbor.

TL;DR


Practise these questions →

Spaced-repetition MCQs for this post, on practise.vivcre.com.