Sub topic 11
Giving bigger nodes more of the ring
This chapter assumes you have read the earlier chapters on the hash ring and virtual nodes. You should know that each virtual node is a point on the ring and that a key belongs to the first point clockwise from its hash.
Why equal nodes are not always right
Plain virtual nodes give every machine the same number of points, so every machine receives about the same share of keys. Real clusters are rarely uniform. A cache fleet may mix 16 GB and 64 GB machines, or a new node type may be added while old ones are still running. If every node gets an equal share, the small machines fill up while the large ones sit idle.
The idea: weight sets the virtual node count
A node's share of the ring is proportional to the number of virtual nodes it owns. To give a node twice the share, give it twice as many virtual nodes. No other part of the lookup changes.
Suppose you use 100 virtual nodes per unit of weight.
| Node | Weight | Virtual nodes | Expected share |
|---|---|---|---|
| a | 1 | 100 | 25% |
| b | 1 | 100 | 25% |
| c | 2 | 200 | 50% |
The expected share is the node's weight divided by the sum of all weights.
Implementation
The ring below stores each node's virtual node count. remove_node needs that count to know which positions to delete.
import bisect
import hashlib
from collections import Counter
def _hash(value):
return int(hashlib.md5(value.encode()).hexdigest(), 16)
class WeightedHashRing:
def __init__(self, vnodes_per_weight=100):
self.vnodes_per_weight = vnodes_per_weight
self._hashes = []
self._owners = {}
self._counts = {}
def add_node(self, node, weight=1):
count = round(weight * self.vnodes_per_weight)
self._counts[node] = count
for i in range(count):
h = _hash(f"{node}#{i}")
bisect.insort(self._hashes, h)
self._owners[h] = node
def remove_node(self, node):
count = self._counts.pop(node)
for i in range(count):
h = _hash(f"{node}#{i}")
self._hashes.remove(h)
del self._owners[h]
def get_node(self, key):
if not self._hashes:
raise LookupError("ring is empty")
idx = bisect.bisect_right(self._hashes, _hash(key)) % len(self._hashes)
return self._owners[self._hashes[idx]]
ring = WeightedHashRing()
ring.add_node("a", weight=1)
ring.add_node("b", weight=1)
ring.add_node("c", weight=2)
counts = Counter(ring.get_node(f"key{i}") for i in range(10000))
for node in sorted(counts):
print(node, counts[node] / 10000)The printed shares should be close to 0.25, 0.25 and 0.50. They will not match exactly, because virtual node positions are random.
Changing a weight later
Virtual node names such as c#0, c#1 and so on are stable. If you raise node c from 200 to 300 virtual nodes, you only add c#200 through c#299. Every existing position stays where it was. Keys move only to c, and only from the nodes that previously owned those new positions. Lowering the weight removes the highest-numbered positions, and those keys move away from c only.
This is why you should name virtual nodes by index and keep that naming scheme fixed. If you rebuilt all positions with a different naming scheme on each change, you would lose the small-movement guarantee.
Gotchas
- Rounding hides small weights. With few virtual nodes per unit of weight, a weight of 0.5 may round to a count too small to balance well. Relative variance in load grows as the virtual node count shrinks, so keep counts in the hundreds at least.
- Weight describes key share, not load. If a few keys are hot, the node that owns them is overloaded however many virtual nodes it has. Weights fix capacity differences, not skewed access patterns.
- Replication caps the useful weight. With replica selection that skips repeated physical nodes, a heavy node appears in the replica list of most keys. With 3 replicas, no node can hold more than one copy of each key. A node whose weight is much more than a third of the total cannot receive its full proportional share. It ends up holding a copy of nearly every key but never more than one.
- Weights are policy. The ring does not measure capacity. Someone or something must set weights and update them when hardware changes.
Exercise
A ring has nodes with weights 1, 2 and 5, and uses 100 virtual nodes per unit of weight. How many virtual nodes does the weight-2 node have, and about what fraction of keys should it own?
Show answer
It has 200 virtual nodes. The weights sum to 8, so its expected share is 2/8 = 25% of the keys.
Exercise
Why is it better to name virtual nodes as node#0, node#1, ... when you plan to change weights over time?
Show answer
The names are stable, so raising a weight only adds new positions and lowering it only removes the highest-numbered ones. Existing positions do not move, so only the keys tied to the added or removed positions change owner.