Explaining virtual nodes in consistent hashing
Standard consistent hashing maps each physical node to a single random position on a hash ring, partitioning keys to the next node clockwise. Because node positions are random, the arc lengths vary widely, following a Dirichlet distribution with high variance—some nodes end up responsible for disproportionately large key segments, leading to uneven load. Assigning multiple virtual nodes (random ring positions) per physical node breaks down these large arcs into many smaller ones; sums of these arcs concentrate around the average load due to the law of large numbers, reducing variance roughly by a factor of the number of virtual nodes. This averaging improves balance, making key distribution more uniform and minimizing load skew across nodes.
In Karger et al.'s original scheme you take a 32‑ or 64‑bit hash space, wrap it into a ring and hash two things into that space:
- Each data item is placed at the point given by the hash of its key.
- Each storage node is placed at one point, chosen at random, on the same ring.
Ownership is defined geometrically: a node owns the arc between its point and its predecessor, and any key that hashes into that arc is stored on that node.
Two properties fall straight out of that construction:
- The expected load on each of (N) nodes is (1/N) of the keys.
- The actual load is a random variable, because the arc lengths are a random partition of the circle.
If you throw (N) points uniformly and independently on a circle, the vector of arc lengths has a Dirichlet((1,\dots,1)) distribution. For modest (N) the spacings are quite uneven – one node may have two neighbours very close together and own a tiny sliver of the ring, another may have an unusually large gap and own a disproportionate number of keys. The variance of a single spacing in the Dirichlet((1,\dots,1)) case is (\frac{N-1}{N^2(N+1)}), so for (N=10) the standard deviation is about 10 % of the mean.
A simple way to smooth this out is to give each physical machine not one but (v) independent positions on the ring. These are often called “virtual nodes” or “vnodes”. Each vnode behaves like a real node for the purposes of the hash function, but several vnodes map back to the same physical machine.
Two things happen when you do that:
- The key space is chopped into many more, much smaller, intervals. Instead of one gamma‑distributed chunk of size (\sim 1/N), a machine receives the sum of (v) independent chunks of size (\sim 1/(N v)).
- By the central limit theorem the sum of many small, independent pieces is tightly concentrated around its mean. If you take the Dirichlet analysis above and replace the parameter 1 with (v), the mean share per machine stays at (1/N) but the variance shrinks roughly by a factor of (v); the coefficient of variation scales like (1/\sqrt{v}).
In practice that means that with, say, 100 vnodes per server the largest and smallest loads differ only by a few per cent, rather than tens of per cent, even for small cluster sizes. It also gives you a finer‑grained unit of movement when nodes join or leave.