03. Data & Storage / Lesson 6 of 11
Consistent Hashing
Add or remove a server and move only the keys that must move.
- 10 min
- Intermediate
- 10 steps
- Servers4
- Keys moved80% (bad)
- Biggest share25%
- Hit rate—
- Request
- Cache hit
- Cache miss
Step 1 of 10: hash(key) % N falls apart when N changes
Read every step as text
Step 1: hash(key) % N falls apart when N changes
We keep data on 4 servers. To find a key's server, the simple rule is
hash(key) % 4: hash the key to a big number and take the remainder.It works until we add a fifth server. The rule becomes
hash(key) % 5, and for most keys the remainder changes: 80% of all keys now belong to a different server and must be copied across the network.Ideally only about 1/5 of the keys would move: the share the new server should take.
Step 2: Bend the hash space into a ring
Hash values run from 0 to 2³². Bend that line into a circle, so the largest value sits next to 0.
Now hash each server's name too, and put the server at that point on the ring. The servers no longer depend on how many of them there are: each one simply has a position.
Servers and keys are hashed into the same space, so they can be compared directly.
Step 3: A key belongs to the next server clockwise
Hash a key, find its point on the ring, then walk clockwise until you meet a server. That server owns the key.
So each server owns the arc just before it. Here the biggest arc is 28% of the ring. Every client that knows the ring finds the same owner without asking anyone.
Looking up the owner is a binary search over the sorted server positions: O(log n).
Step 4: A new server takes over one arc
Server E hashes to a point between B and C. Keys in the arc from B up to E used to walk on to C; now they meet E first.
Only those keys move, from C to E: 13% of the key space (5 of our 24 keys). With % N it was 80%.
Adding a server only touches its clockwise neighbour. Every other server keeps all of its keys.
Step 5: A server leaves: its keys go to the next one
Server C crashes. Its keys simply keep walking clockwise and land on D.
Again only one arc moves: 10% of the key space. A, B and E do not notice anything.
D now holds its own keys and C's. If C was busy, D can be overloaded: the next steps fix this.
Step 6: Random points make uneven arcs
Server positions come from a hash, so they fall wherever they fall. With only a few servers the gaps between them are very different in size.
With A, B and D left, D owns the long arc after B: 47% of all keys, while an even split would give each server 33%.
The busiest server sets the limit for the whole system, however idle the others are.
Step 7: Give every server many small points
Instead of one point, hash each server several times (
A#1,A#2, …) and place it at all of those points. These are virtual nodes.Many small arcs average out: with 32 points each, the biggest share drops to 35%. Real systems use 100 or more per server, which gets very close to an even split.
A stronger machine can get more virtual nodes, and so a bigger share: weights for free.
Step 8: Copies go to the next servers clockwise
To survive a crash, keep each key on more than one server. The ring already gives an order: store the key on its owner and on the next distinct servers clockwise.
With 3 copies, any one server can fail and every key is still on two others. The dashed lines show where two of the keys are kept.
Skip virtual nodes of a server you already chose, or two copies could end up on the same machine.
Step 9: A cache fleet loses only one slice
Large sites spread a cache over many servers with exactly this ring: the app hashes the key and asks the owning cache server.
When cache C dies, only the keys in C's arcs (31% of them) miss and go to the database, until they are cached again on the next server. With % N, almost every key would miss at once.
Memcached clients, DynamoDB, Cassandra and many CDNs place data with a hash ring.
Step 10: Consistent hashing in one picture
Servers and keys share one hash ring; a key belongs to the next server clockwise. Adding or removing a server moves one arc, not the whole data set.
Virtual nodes even out the arcs, the next servers clockwise hold the copies, and a cache fleet loses only one slice when a server dies.