Quant Memo
Core

Consistent Hashing

A way to map keys to servers so that adding or removing a server only reshuffles a small fraction of keys, instead of nearly all of them.

Prerequisites: Hash Maps and Sets

A trading firm shards its market-data cache across 8 machines by ordinary hashing: server = hash(symbol) mod 8. One machine dies, so it moves to 7. Nearly every symbol's hash(symbol) mod 7 lands on a different machine than hash(symbol) mod 8 did — almost the entire cache is now a miss, and every downstream system has to refetch nearly everything at once. Consistent hashing exists to make that kind of resizing cheap instead of catastrophic.

Ordinary mod N hashing ties every key's destination to the total server count, so changing that count reshuffles almost everything. Consistent hashing places both servers and keys on a fixed ring by hash value; a key belongs to the next server clockwise from it, so adding or removing one server only moves the keys between it and its nearest neighbor.

How it works

Imagine a circle of hash values from 0 to some large maximum, then wrapping back to 0. Hash each server's identifier onto a point on this circle. To find which server owns a given key, hash the key onto the same circle and walk clockwise to the first server point you hit.

server A server B server C key 1 key 2 → B new server D only keys between C and D's clockwise position move to D
Adding server D only steals keys from the arc between C and D — every other key's clockwise server is unchanged, unlike mod-N hashing where nearly all keys move.

Worked example

Three servers A, B, C hash to positions 10, 40, 75 on a ring of size 100. Keys hash to: key1 → 15, key2 → 50, key3 → 90. Walking clockwise: key1 (15) lands on B (next point after 15 is 40); key2 (50) lands on C (next point after 50 is 75); key3 (90) wraps around to A (next point after 90, wrapping past 100, is 10).

Now add server D at position 60. Re-check ownership: key1 still lands on B (40 is still its next point going clockwise from 15). key2, previously landing on C at 75, now lands on D at 60 — it moves. key3 is unaffected, still landing on A. Only key2, the one sitting between D's new position and its old owner, ever moved; keys owned by A and unaffected parts of B and C stayed put.

What this means in practice

Consistent hashing is the backbone of distributed caches (Memcached client libraries), distributed databases (DynamoDB, Cassandra), and CDN routing, anywhere a set of servers can grow or shrink and a full reshuffle on every change would be too expensive. In practice, each physical server is usually hashed to many points on the ring ("virtual nodes"), not just one — a single point risks one server inheriting a wildly uneven, oversized arc of the ring, while dozens of virtual nodes per server smooth that out to a roughly even split of keys.

Consistent hashing bounds how many keys move on a resize, not which keys — a lookup table or client cache still has to be told about the new server layout, or it will keep routing to the old positions. The algorithm minimizes churn; it does not eliminate the need to propagate ring changes to every client.

Related concepts

Practice in interviews

Further reading

  • Karger et al., 'Consistent Hashing and Random Trees' (STOC 1997)
  • Kleppmann, Designing Data-Intensive Applications (partitioning chapter)
ShareTwitterLinkedIn