Sub topic 7
Why a plain ring distributes load unevenly
This chapter assumes you know how the hash ring maps keys and nodes to one hash space, and that a key goes to the first node position clockwise from its hash. It also assumes you can read short Python.
The problem: random positions make uneven arcs
On a plain ring, each node is hashed once, so it gets one position. A node owns the arc between its predecessor's position and its own. The size of that arc decides how many keys the node receives.
Hash functions place points uniformly at random, and random points do not space themselves evenly. Throw four points onto a circle and some gaps come out big and some small. Four nodes could each own 25% of the keys, but that is the average, not what you usually get.
Measuring the arcs
This script computes the share of the hash space that each node owns. It needs no earlier class, only the hash function.
import hashlib
SPACE = 2 ** 128
def h(value):
return int(hashlib.md5(value.encode()).hexdigest(), 16)
def arc_shares(nodes):
points = sorted((h(n), n) for n in nodes)
if len(points) == 1:
return {points[0][1]: 1.0}
shares = {}
for i, (pos, node) in enumerate(points):
prev = points[i - 1][0]
shares[node] = ((pos - prev) % SPACE) / SPACE
return shares
for name, share in sorted(arc_shares(["a", "b", "c", "d"]).items()):
print(f"{name}: {share:.1%}")The loop uses points[i - 1]. For i = 0 that is the last point, so the first node's arc wraps around the end of the ring. The % SPACE handles that wrap.
Run it with different node names, such as "cache-1" through "cache-4". The shares change each time, and they rarely come out near 25% each. The result is fixed for a given set of names, because the hash is deterministic.
How uneven is it?
For N nodes placed uniformly at random, the expected sizes of the extreme arcs are known:
- The largest arc is about H(N) / N of the ring, where H(N) is the Nth harmonic number (1 + 1/2 + ... + 1/N).
- The smallest arc is about 1 / N² of the ring.
The ideal share is 1 / N, so the largest node carries H(N) times its fair load.
| Nodes | Ideal share | Expected largest share, relative to ideal | Expected smallest share, relative to ideal |
|---|---|---|---|
| 4 | 25% | about 2.1x | about 0.25x |
| 10 | 10% | about 2.9x | about 0.1x |
| 100 | 1% | about 5.2x | about 0.01x |
Adding nodes does not fix this. The gap between the busiest node and the average grows slowly, like ln N. At 100 nodes, the busiest node typically carries about five times the average load, and the quietest sits nearly idle.
Why it hurts in practice
- Capacity. You size every node for the busiest one, or that node becomes a hot spot that fails first.
- Uneven cache hit rates. In a cache, a node with a tiny arc stores little. A node with a huge arc evicts constantly.
- Cascading removal. When a node leaves, all its keys go to its single clockwise successor. That successor's load jumps by the departed node's share. If the departed node was large, the successor may overload and fail, which pushes its load to the next node.
Key skew is a separate issue
Arc size is only one source of imbalance. Even with perfectly equal arcs, some keys are requested far more often than others. A single popular key lands on one node whatever the ring looks like. The fix for that is replication or caching in front of the ring, not a change to the ring. The next chapter addresses only the arc problem.
The direction of the fix
The problem is that each node has one random sample of the hash space. Averaging many samples smooths out variance. If each node were placed at many positions instead of one, its total share would be the sum of many small arcs, and sums of many random pieces cluster near their mean. That idea is called virtual nodes, and it is the subject of the next chapter.
Exercise
A ring has 10 nodes, each with one position. A node owns 25% of the key space. Its predecessor on the ring is removed. What share does the node own afterwards, and which node gets the keys of the removed one?
Show answer
The removed node's arc is merged into the arc of its clockwise successor, which is this node. The node's share becomes 25% plus the removed node's former share. Only that one node gains load, which is why a leaving node can overload its neighbor.
Exercise
Using the table, roughly how many times the ideal share does the busiest of 100 single-position nodes carry, and does adding more nodes remove the imbalance?
Show answer
About 5.2 times the ideal share, since H(100) is about 5.19. More nodes do not remove it. The ratio grows slowly, like ln N, so it gets worse, not better.