Servers & keys hash onto a ring: which server holds each key? Add/remove a server, then count how many keys move (vs hash%N). Virtual nodes: what do they do…
Why it matters
Distributed caches, database sharding and load balancers must know which server owns a key. With hash % n, adding one server changes the owner of nearly every key: every cache goes cold at once. The ring moves only about 1/n. Dynamo, Cassandra and many Memcached clients use it.
The key idea
Servers and keys on one ring
Common mistakes
Adding one server makes nearly every cache miss at once That is the sign of hash % N. Move to a ring (consistent hashing), so a server change only moves one arc of keys.
One server gets far more load than the others Random positions on the ring can be uneven when there are few servers. Add virtual points (many points per server): the spread improves.
Wondering how every node knows a key’s owner without a central server The hashing is deterministic: every node computes the key’s owner itself from the server list. No central lookup needed.
Try it in this playground
Ask a packet’s owner Follow its walk clockwise.
Add a helper on the ring Fewer than half the packets move.
Add or remove a helper with hash % n Half or more change hands.
Give helpers virtual points Watch the arcs split up and the load even out.