System Design
5. Advanced Database Architecture8. Load Balancing and ScalabilityAnimated
Consistent Hashing
Hashing onto a ring so few keys move when nodes change.
The idea
Nodes and keys share a hash ring. A key belongs to the next node clockwise, so adding a node moves only a small share of keys.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
- outlined = moved key
Nodes and keys are hashed onto the same ring. Each key belongs to the first node clockwise from it.
- nodes:
- 4
- keys:
- 30
- moved:
- 0
1/2
Dots on the ring are keys; larger circles are servers.
Step by step
- 1Hash nodes onto a ring
- 2Hash each key onto it
- 3Walk clockwise to the owner
- 4Adding a node moves only nearby keys
Where it is used
Caches and distributed stores.
Watch out for
Uneven spread without virtual nodes.