Sub topic 5
Virtual Nodes and Balanced Load
This chapter assumes you know how a hash ring maps keys to nodes, as covered in the previous chapter, and that you can read short Python snippets.
The problem with one point per node
With plain consistent hashing, each physical node gets one position on the ring. A node owns the arc between its predecessor and itself. With only a few random points, arc lengths vary a lot. One node may own 40% of the ring while another owns 10%.
Removing a node makes this worse. Its entire arc moves to a single successor, which then carries double load.
What a virtual node is
A virtual node (vnode) is one of many ring positions assigned to a physical node. Instead of hashing node-a once, you hash node-a#0, node-a#1, and so on. Each physical node owns many small arcs scattered around the ring.
Two effects follow from this:
- Balance improves. By the law of large numbers, the sum of many small random arcs is close to the average. With around 100 to 256 vnodes per node, load typically varies by a few percent.
- Failures spread out. When a node leaves, each of its small arcs goes to a different successor. Many nodes each take a small share, not one node taking all of it.
Building the ring
The ring from the topic's reference code uses 64 vnodes per node. Here is a version that measures how evenly keys spread as you change the vnode count.
import bisect
import hashlib
from collections import Counter
def h(key: str) -> int:
return int.from_bytes(hashlib.md5(key.encode()).digest()[:8], "big")
class Ring:
def __init__(self, nodes, vnodes=64):
self._points = sorted(
(h(f"{node}#{i}"), node) for node in nodes for i in range(vnodes)
)
self._hashes = [p[0] for p in self._points]
def owner(self, key: str) -> str:
idx = bisect.bisect(self._hashes, h(key)) % len(self._points)
return self._points[idx][1]
nodes = ["node-a", "node-b", "node-c", "node-d"]
keys = [f"user:{i}" for i in range(20000)]
for vnodes in (1, 8, 64, 256):
ring = Ring(nodes, vnodes)
counts = Counter(ring.owner(k) for k in keys)
ideal = len(keys) / len(nodes)
worst = max(abs(c - ideal) / ideal for c in counts.values())
print(f"vnodes={vnodes:>3} worst deviation from ideal: {worst:.1%}")Run it and the deviation shrinks as the vnode count grows. The exact numbers depend on the hash, but the trend is consistent. Deviation falls roughly with the square root of the vnode count.
Choosing the vnode count
More vnodes give better balance, but they cost something:
| Factor | Few vnodes | Many vnodes |
|---|---|---|
| Load balance | Uneven | Even |
| Ring metadata | Small | Larger, gossiped and stored on every node |
| Lookup cost | Small binary search | Slightly larger binary search |
| Repair and streaming | Few large ranges | Many small ranges, more bookkeeping |
Vnodes also let you handle mixed hardware. Give a machine with twice the capacity twice as many vnodes, and it takes about twice the keys.
Common errors
- Hashing vnode labels poorly. If labels like
node-a#0hash to clustered values, balance suffers. Use a well-mixed hash. - Replicas landing on the same machine. When walking clockwise for N replicas, consecutive vnodes may belong to one physical node. The preference list must skip duplicates and count distinct physical nodes, as the
preference_listmethod does. - Changing the vnode count on a live cluster. This reshuffles ownership. Treat the count as a fixed design parameter.
Exercise
A cluster has 3 nodes with 1 vnode each. One node fails. Why does one surviving node likely take more load than the other, and how do vnodes change this?
Show answer
The failed node's single arc passes entirely to its clockwise successor, so that node absorbs all of the extra load. With many vnodes per node, the failed node owns many small arcs scattered around the ring. Each arc goes to a different successor, so the extra load is shared across the survivors.