Consistent hashing
A way to decide which shard each piece of data goes to that, when a shard is added or removed, moves only part of the data.
Example
With 4 shards, adding a fifth moves about 1/5 of the data. With a plain hash (id % 4 → id % 5) almost all of it would move.
Test yourself: Consistent hashing
Consistent hashing
You split with hash % N. What happens if you add a node?
From 4 to 5 nodes, a key stays put only if hash % 4 = hash % 5: barely 1 in 5.
Consistent hashing
What does consistent hashing solve?
With 1 million keys and 5 nodes, it moves about 200,000 (K/N) instead of 800,000.
Consistent hashing
With consistent hashing, when a node is removed its keys are spread among all the others.
They go only to the next node on the ring: the other 3 of 5 don't notice.
0/3
â–¶ Play