Consistent Hashing
Overview
Consistent hashing is a way to decide which server owns which key so that adding or removing a server moves only a small share of the data. It is behind many caches, sharded databases and CDNs.
Detailed Notes
What is consistent hashing
A method of mapping keys to servers using a ring. Both servers and keys are hashed onto the same circle, and a key belongs to the first server found moving clockwise from its position.
The problem it solves
The simple way to spread keys is server = hash(key) mod N, where N is the number of servers. It works until N changes. Going from 4 servers to 5 changes the answer for nearly every key, so almost all cached data is suddenly "on the wrong server": a flood of cache misses that hammers the database. Consistent hashing keeps most keys where they are.
How it works
- Pick a hash function and imagine its output range as a circle (for example 0 to 2^32 - 1).
- Hash each server (by name or IP) to a point on the ring.
- Hash each key to a point on the ring.
- The key belongs to the first server clockwise from its point.
- Adding a server takes over only the keys between it and its predecessor.
- Removing a server hands its keys to the next server clockwise.
- On average only about K/N keys move (K keys, N servers), instead of nearly all.
Virtual nodes. With few servers, the ring can be uneven: one server may own a huge arc. So each physical server is hashed to many points (for example 100-256 virtual nodes). This evens out the load, lets stronger machines take more points, and spreads a failed server's keys across many neighbours instead of dumping them on one.
Replication. To keep copies, store each key on the next R distinct servers clockwise. Dynamo-style stores combine this with quorums.
| Method | Keys moved when N changes | Notes |
|---|---|---|
| hash mod N | Nearly all | Fine only for a fixed number of servers |
| Consistent hashing + virtual nodes | About K/N | Even load, used by Cassandra, DynamoDB-style stores, CDNs |
Limits: one extremely popular (hot) key still overloads its server, and range queries are harder because hashing scatters neighbouring keys.
A simple example
Think of a circular street with four mailboxes (servers) and houses (keys) along it. Each house uses the next mailbox clockwise. Build a fifth mailbox and only the houses just before it switch to it; everyone else keeps their old one.
System design example
You run a cache cluster of 4 nodes serving millions of keys. Traffic grows and you add a fifth. With mod N, about 80 percent of keys would miss at once. With consistent hashing, only about 20 percent move, so the database stays calm. The same idea places data in sharded databases (see the Data Partitioning chapter).
Summary
- Servers and keys share one hash ring; a key goes to the next server clockwise.
- Changing the number of servers moves only about K/N keys.
- Virtual nodes balance the load and soften failures.
- Hot keys and range queries are still hard.
Architecture Diagram
Keys map to the next server clockwise on the ring.
- 1The client hashes the key to a point on the ring.
- 2It walks clockwise to the first server.
- 3Adding or removing a server moves only the neighbouring keys.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

