Sub topic 7
Why a plain ring distributes load unevenly
This chapter assumes you know how the hash ring maps keys to the first node clockwise from the key's position. It also assumes you can read short Python programs and know a little probability, such as what an average is. The chapter shows that a ring with one position per node gives uneven shares, measures how uneven, and says what the imbalance does to a cluster. The next chapter fixes it with virtual nodes.
The promise and the gap
Earlier chapters showed that a ring moves only about 1/N of the keys when a node joins or leaves. That is a statement about movement. It says nothing about balance, meaning whether each node ends up holding about 1/N of the keys.
On a plain ring, each node is hashed to exactly one position. A node owns the arc of the ring that ends at its position and starts just after the previous node's position. A key belongs to the node whose position is the first at or after the key's hash. So a node's share of the keys equals the length of its arc as a fraction of the ring.
Nothing forces those arcs to be equal. The positions come from a hash function, and a good hash function places them like random points. Random points on a circle leave gaps of very different sizes.
A small example
Suppose four nodes land on a ring with 100 positions at 10, 18, 63 and 80.
| Node | Position | Arc owned | Share of keys |
|---|---|---|---|
| a | 10 | from 81 around to 10 | 30% |
| b | 18 | 11 to 18 | 8% |
| c | 63 | 19 to 63 | 45% |
| d | 80 | 64 to 80 | 17% |
Node c holds more than five times as many keys as node b. The ideal share is 25% each. Node c is 80% over its fair share, and node b is at about a third of it.
Nothing is wrong with the hash function here. The unevenness comes from having only four random points.
How uneven is it, on average?
Place N points uniformly at random on a circle. The N arcs between neighbouring points are not equal, and the statistics are well known.
- The mean arc is 1/N. This is true by definition, since the arcs sum to 1.
- The expected largest arc is H(N)/N, where H(N) = 1 + 1/2 + ... + 1/N is the N-th harmonic number. H(N) grows roughly like ln(N) plus 0.58.
- The expected smallest arc is about 1/N², which is a fraction 1/N of the mean arc.
The busiest node therefore carries about H(N) times its fair share, on average.
| Nodes (N) | Fair share (1/N) | Expected largest arc | Largest vs. fair share | Expected smallest arc |
|---|---|---|---|---|
| 4 | 25% | 52.1% | 2.08x | 6.25% |
| 10 | 10% | 29.3% | 2.93x | 1% |
| 50 | 2% | 9.0% | 4.50x | 0.04% |
| 100 | 1% | 5.19% | 5.19x | 0.01% |
Two points matter here.
- Adding nodes does not cure the imbalance. The ratio between the busiest node and the average node gets worse as the cluster grows, though only logarithmically. Going from 10 to 100 nodes takes the ratio from about 2.9 to about 5.2.
- The averages hide the spread. These are expectations over random placements. A particular cluster can be luckier or unluckier, and you cannot choose which, because the positions are fixed by the node names.
Measuring it yourself
The following program places N random points, computes the arcs, and averages the ratios over many trials. It uses only the standard library.
import random
def arc_shares(n, rng):
points = sorted(rng.random() for _ in range(n))
gaps = [points[i] - points[i - 1] for i in range(1, n)]
gaps.append(1.0 - points[-1] + points[0]) # the arc that wraps around zero
return gaps
def harmonic(n):
return sum(1.0 / k for k in range(1, n + 1))
rng = random.Random(42)
trials = 2000
for n in (4, 10, 50, 100):
max_ratio = 0.0
min_ratio = 0.0
for _ in range(trials):
gaps = arc_shares(n, rng)
mean = 1.0 / n
max_ratio += max(gaps) / mean
min_ratio += min(gaps) / mean
print(
f"N={n:3d} largest/fair={max_ratio / trials:5.2f} "
f"(theory {harmonic(n):5.2f}) "
f"smallest/fair={min_ratio / trials:6.4f} (theory {1 / n:6.4f})"
)The measured values should land close to the theory column. They will not match exactly, because each run is a finite sample.
The code uses random floats instead of hashed node names to keep it short. A good hash such as MD5 behaves statistically like this. You can check by replacing rng.random() with _hash(f"node{i}") / 2**128 from the lookup code in earlier chapters. The numbers stay in the same range.
Why the imbalance hurts
A node's share of the keys translates into real costs.
- Memory and disk. For a cache or a sharded store, the node with the biggest arc fills up first. If all nodes have the same capacity, you must size every machine for the worst arc, not the average. With 10 nodes and a 2.9x peak, about two thirds of the fleet's capacity sits idle in the typical case.
- Request rate. If keys are requested about equally often, a node with 29% of the keys serves 29% of the traffic. Its CPU, network and disk saturate before the others do, and latency at that node sets the tail latency for the whole service.
- Small clusters. With 3 or 4 nodes, one node can easily own half of the ring. This is the case people notice first, because a small test cluster looks fine on paper and then shows one overloaded machine.
Imbalance when nodes leave
Imbalance is also tied to what happens on a failure. When a node leaves a plain ring, its whole arc passes to its clockwise neighbour. No other node gains anything.
In the four-node example, suppose node c (45%) fails. Its arc goes to node d, which was holding 17%.
| Node | Before | After c leaves |
|---|---|---|
| a | 30% | 30% |
| b | 8% | 8% |
| d | 17% | 62% |
Node d's load jumps by a factor of about 3.6. If d was already running at 50% of capacity, it is now far past 100%, and it may fail too. Then node a inherits d's 62% and a's load reaches 92%. This is a cascading failure.
The ideal behaviour is for the departing node's keys to spread evenly across all survivors, each taking 1/(N-1) of the extra. A plain ring cannot do that, because a node's position fixes exactly one successor.
Joining is gentler. A new node lands somewhere on the ring and takes part of one existing arc, which lightens exactly one neighbour. The newcomer's own share is again random, so it may receive almost nothing or a large slice.
What imbalance does not explain
Two problems look similar and are worth separating.
- Arc imbalance is what this chapter covers. It comes from node positions and is a property of the ring. It affects the number of keys per node even when every key is equally popular.
- Hot keys come from a skewed access pattern. One very popular key lands on one node regardless of how well the ring is balanced. A perfectly balanced ring cannot fix this. Remedies are replicating hot keys, caching in front of the shard, or splitting a key into several sub-keys.
When you measure uneven load in production, check both. Count keys per node to find arc imbalance, and request counts per key to find hot keys. Balanced key counts with uneven traffic point to hot keys.
Ideas that do not work well
A few obvious fixes fail, and the reasons are useful to know.
- Choose better node names. Brute-forcing names until positions are evenly spaced works for a fixed cluster. It breaks the moment nodes are added, removed or renamed, and it ties placement to names rather than to the hash function's behaviour.
- Assign positions by hand. You can space N positions evenly, 1/N apart. Then adding a node means re-spacing everyone, and moving positions moves keys. You lose the "only 1/N moves" property, which was the reason to use a ring.
- Use a better hash function. The hash is already about as uniform as you can ask. The imbalance comes from placing few points, not from bias in the hash.
The approach that works keeps random placement but uses many points per node, which makes the arcs average out. That is the subject of the next chapter.
A quick intuition for the fix
Each node's share is a sum of arc lengths. With one point, it is one arc, and one arc is a very noisy quantity. With V points per node, a node's share is the sum of V small arcs. The sum of many independent random pieces has a relative spread that shrinks roughly like 1/√V. At V = 100, the relative spread is around a tenth of what it is at V = 1. The cost is memory for a larger ring and more work when nodes join or leave. Those trade-offs are covered when virtual nodes are introduced.
Summary of what to remember
- A plain ring gives each node one random point, so arc lengths vary widely.
- The expected busiest node carries about H(N) = 1 + 1/2 + ... + 1/N times the fair share, and the expected smallest carries about 1/N of it.
- The imbalance grows slowly with cluster size, so adding nodes does not remove it.
- When a node leaves, its entire arc goes to one neighbour, which can overload that neighbour.
- Arc imbalance and hot keys are different problems with different fixes.
Exercise
A plain ring has 10 nodes and each node has a single position. Using the expected-value formulas from this chapter, what share of the keys does the busiest node hold on average, and how many times its fair share is that? Why can you not fix this by adding more nodes?
Show answer
The expected largest arc is H(10)/10, where H(10) = 1 + 1/2 + ... + 1/10 ≈ 2.93, so the busiest node holds about 29.3% of the keys. The fair share is 10%, so that is about 2.93 times the fair share. Adding nodes does not help because the ratio H(N) keeps growing with N (about 5.19 at N = 100). The cause is that each node has only one random point, not that the cluster is small.
Exercise
In a four-node plain ring, node a owns 30%, b owns 8%, c owns 45% and d owns 17%. Node c fails and node d is its clockwise neighbour. What are the new shares, and which node is at risk of a cascading failure?
Show answer
Node c's whole arc goes to d, so d owns 17% + 45% = 62%. Nodes a and b stay at 30% and 8%. Node d is at risk, because its load rises by a factor of about 3.6 and it may fail next. If d then fails, its 62% moves on to its own clockwise neighbour a, which would reach 92%.