Consistent Hashing
Consistent hashing is a technique for assigning keys to nodes (servers, cache instances, shards) in a way that survives adding or removing nodes without reshuffling almost everything. It shows up constantly in system design interviews because it solves a very concrete, very common problem: how do you distribute data or requests across a changing set of machines, without a full rebalance every time the set changes?
The problem with the naive approach​
The obvious way to assign a key to one of N servers is hash(key) % N. It's simple and spreads keys evenly — right up until N changes. If you add or remove even one server, N changes, and hash(key) % N returns a completely different answer for almost every key. In a cache, that means almost every cache entry suddenly maps to the "wrong" server and is effectively lost (a cache miss storm). In a sharded database, it means moving almost all your data around just because you added one more shard — clearly not something you want happening every time you scale.
The consistent hashing idea​
Instead of hashing keys onto a number line of size N, consistent hashing hashes both keys and nodes onto the same fixed, large circular space (usually visualized as a ring, e.g. hash values from 0 to 2³²−1 wrapping back to 0). To find which node owns a key:
- Hash the key to get a position on the ring.
- Walk clockwise from that position until you hit the first node.
- That node owns the key.
Because nodes are placed on the same ring as keys, adding a new node only affects the keys that fall between the new node and the next node counter-clockwise from it — everything else on the ring is untouched. Removing a node only affects the keys that were assigned to it (they move to the next node clockwise). Roughly speaking, adding or removing one of N nodes only remaps about 1/N of the keys, instead of nearly all of them.
Virtual nodes: fixing uneven distribution​
A ring with just one point per physical node can be lumpy — by chance, one node might end up "owning" a much longer arc of the ring than another, especially with a small number of nodes. The standard fix is virtual nodes: each physical node is hashed onto the ring multiple times (e.g., 100–200 times, at different hash positions, often computed as hash(nodeName + "-" + replicaIndex)). More points on the ring per physical node means the law of large numbers smooths out the arc lengths, so load ends up much closer to evenly distributed — and a real machine can also be given proportionally more virtual nodes if it has more capacity than its peers.
Try it yourself​
The demo below builds a small hash ring with 3 nodes (each with several virtual replicas), assigns 20 sample keys, then adds a 4th node and recomputes. Run it and compare how many keys moved under consistent hashing versus how many would have moved under plain hash(key) % N (essentially all of them). Try changing VIRTUAL_NODES_PER_NODE to 1 and re-running to see the distribution get lumpier.
Where it's actually used​
Consistent hashing was popularized by Akamai's content distribution network and later by Amazon's Dynamo paper, and today shows up in:
- Distributed caches (e.g., Memcached client libraries) — so adding a cache node doesn't invalidate almost the entire cache.
- Database sharding — so adding a shard doesn't require moving nearly all existing data (see Database Sharding).
- Load balancers that want to route the same client to the same backend for session affinity, even as backends scale up or down.
- Distributed hash tables (DHTs) in peer-to-peer systems.
Consistent hashing vs. plain hash(key) % N​
Pros
- Adding/removing a node only remaps ~1/N of keys, not almost all of them
- Virtual nodes smooth out load distribution across physical nodes
- No coordinated full-rebalance step needed when the node count changes
Cons
- More complex to implement and reason about than a plain modulo
- Uneven load is still possible with too few virtual nodes per physical node
- Ring lookup adds a small amount of overhead vs. a single modulo operation
Further Reading​
- Amazon Dynamo Paper (2007), Section 4.2 — the paper that brought consistent hashing (with virtual nodes) into mainstream distributed systems practice.
- Toptal — A Guide to Consistent Hashing — an accessible, diagram-heavy walkthrough of the ring and virtual node concepts.
Saved locally in your browser — visible in the sidebar as you go.