Sub topic 6
What happens when nodes join or leave
This chapter assumes you know how the hash ring maps keys to nodes by clockwise successor, and that you can read short Python. Here you work out which keys move when the cluster changes, and which nodes are involved.
The rule: only one arc changes
On the ring, each node owns the arc of hash space that ends at its own position and starts just after the previous node's position. A key belongs to the first node at or after its hash, moving clockwise.
When the node set changes, only the arcs next to the change are affected. Every other key keeps its owner.
A node joins
Suppose the ring holds a, b, c and d in clockwise order. A new node e hashes to a point between b and c.
Before the join, c owned every key between b and c. After the join, e owns the keys between b and e, and c keeps the keys between e and c.
Two facts follow:
- Every moved key moves to the new node. No key is shuffled between old nodes.
- The expected share of moved keys equals the share of the ring that e takes. With N nodes of equal arc length, that is about 1/N of the keys. Going from 4 to 5 nodes moves about one fifth, compared with about 80% under modulo hashing.
The node that loses keys is the clockwise successor of e (here c). It must hand them over, or the cache must refill them.
A node leaves
Removal is the reverse. If c leaves, its arc is absorbed by its clockwise successor, d. The keys that were on c move to d. Keys on a, b and d stay where they are.
The same fraction applies: about 1/N of the keys move. This holds whether c left cleanly or crashed. A crash differs only in that c cannot hand over its data.
Checking it in code
This sketch uses one ring position per node, which is the simplest case. It measures how many keys move and where they go.
import bisect
import hashlib
def h(value):
return int(hashlib.md5(value.encode()).hexdigest(), 16)
def build(nodes):
return sorted((h(n), n) for n in nodes)
def owner(ring, key):
points = [p for p, _ in ring]
idx = bisect.bisect_right(points, h(key)) % len(ring)
return ring[idx][1]
keys = [f"key{i}" for i in range(10000)]
ring = build(["a", "b", "c", "d"])
before = {k: owner(ring, k) for k in keys}
ring = build(["a", "b", "c", "d", "e"])
after = {k: owner(ring, k) for k in keys}
moved = [k for k in keys if before[k] != after[k]]
print(len(moved) / len(keys))
print({after[k] for k in moved})The second line prints {'e'}, because every moved key lands on the new node. The first line prints the moved fraction. It depends on where e happens to land, so it can be well above or below 0.2.
That spread is a weakness of this version. It is the subject of the next chapter.
Common errors
- Assuming the share is exactly 1/N. It is only the average. With few ring positions, one join can move far more or far fewer keys.
- Forgetting the data. The ring tells you where a key should live. It does not copy data. After a join, keys now owned by e are still stored on c until you migrate them. For a cache, those lookups simply miss and refill.
- Treating a crash like a clean leave. If c vanishes, d becomes the owner at once, but d holds none of c's data. Replication, covered later, addresses this.
- Adding several nodes at once and computing each move separately. Compute the final ring and compare it with the old one. Arcs can overlap in ways that make the per-node counts misleading.
Exercise
A ring has 5 nodes with roughly equal arcs. One node crashes. About what fraction of keys changes owner, and which node receives them?
Show answer
About 1/5 of the keys change owner. All of them go to the crashed node's clockwise successor, because that node absorbs the crashed node's arc. Keys owned by the other three nodes do not move.