System design · interactive
Consistent hashing
Spread keys over cache or database servers. With hash(key) % n, adding one server moves almost every key. Put servers and keys on a ring instead, and only one slice moves.
- Server A36
- Server B16
- Server C0
- Server D8
60 keys on 4 servers. Busiest server holds 36 keys; a perfect split is 15.
Placement
Servers (tap to add or remove)
Virtual nodes per server
1 A hash turns a name into a number from 0 to 2³²−1. Bend that range into a circle, and each server sits at its hash.
A#0 → 275° · B#0 → 112° · C#0 → 284° · D#0 → 350°
2 3 4 5
In production
Tools and technologies
Distributed caches, databases and load balancers all use rings or similar schemes to move as little data as possible.
Not tied to one language — use these names when you search docs or design a system.
- Apache CassandraSoftware
Token ring with virtual nodes for data placement.
GitHub ↗ - Amazon DynamoDBService
Partitioning descended from the Dynamo paper's consistent hashing.
Official site ↗ - Envoy ring hash / MaglevSoftware
Load balancing that keeps a client on the same backend.
GitHub ↗ - Memcached clientsSoftware
Ketama consistent hashing spreads keys over cache servers.
GitHub ↗