The Power of Two Choices
Put each ball into the less loaded of two random bins, instead of one random bin. That tiny change reduces the maximum load of balls in bins from (Balls into Bins: The Maximum Load) to : an exponential improvement for the price of one extra probe. A third choice only improves the constant. The result is due to Azar, Broder, Karlin and Upfal (1994; journal version 1999), and it underlies load balancers, distributed hash tables and cuckoo hashing. This lesson states the theorem, explains the layered-induction idea behind it, and measures the effect.
The process and the theorem
D-CHOICE(n, m, d) // m balls, n bins, d >= 1 choices per ball
0 load[0 .. n-1] = 0
1 for ball = 0 to m - 1
2 choose bins b_1 .. b_d independently and uniformly
3 put the ball in a b_j with the smallest load (ties: any rule)
4 return max(load)Theorem (Azar, Broder, Karlin and Upfal, 1999). For and any fixed , the maximum load of D-CHOICE is
For it is . Going from one choice to two changes the order of growth; going from two to divides the leading term by .
Why: layered induction (sketch)
Let be the number of bins with load at least at the end. A ball lands at height or more only if all of its choices already have load at least , which (as long as at most bins are that high) has probability at most . So the number of bins reaching height is at most a binomial with mean , and with a Chernoff bound, roughly,
Starting from (at most bins can hold 4 or more of balls), the fractions fall doubly exponentially: . They drop below once , that is after more levels. Making this rigorous requires conditioning on the high-probability events level by level and a separate argument for the last levels, where Chernoff bounds are too weak; see Mitzenmacher and Upfal, chapter "Balanced allocations". With one choice, by contrast, for the Poisson tail, which decays only like .
The program measures the fraction of bins with load at least for one and two choices and compares the two-choice fractions with the square of the previous level.
import random
rng = random.Random(120)
n = 30000
one, two = [0] * n, [0] * n
for _ in range(n):
a, b = rng.randrange(n), rng.randrange(n)
one[a] += 1
two[a if two[a] <= two[b] else b] += 1
def tail(load, i):
return sum(x >= i for x in load) / n
print(" i one choice: Pr[load >= i] two choices: Pr[load >= i] (previous two-choice level)^2")
prev = None
for i in range(1, 7):
t1, t2 = tail(one, i), tail(two, i)
sq = f"{prev ** 2:.2e}" if prev is not None else "-"
print(f"{i:2} {t1:26.2e} {t2:27.2e} {sq:>29}")
prev = t2
print("max load: one choice", max(one), " two choices", max(two)) i one choice: Pr[load >= i] two choices: Pr[load >= i] (previous two-choice level)^2
1 6.29e-01 7.58e-01 -
2 2.67e-01 2.32e-01 5.75e-01
3 8.10e-02 9.53e-03 5.40e-02
4 1.88e-02 3.33e-05 9.09e-05
5 3.43e-03 0.00e+00 1.11e-09
6 5.33e-04 0.00e+00 0.00e+00
max load: one choice 7 two choices 4
With one choice the fractions shrink by a factor that grows only slowly with . With two choices each level stays below the square of the one before, and the fractions collapse: the doubly exponential decay that the layered induction predicts. (The square is only the leading behaviour of an upper bound; the fluid limit of the process, studied by Mitzenmacher, gives the precise fractions.)
Watching both strategies
The lab runs both processes on the same random stream: the two-choice side uses the one-choice side's bin as its first candidate. With 60 bins and 300 balls, run 1 ends with a fullest bin of 16 for one choice and 6 for two choices; runs 2 to 5 give 11, 9, 11, 12 against 7 each time. The one-choice maximum varies a lot from run to run; the two-choice maximum barely moves, and the average load is 5.
That last observation is a theorem too: for balls, the two-choice maximum is with high probability, so the gap above the average does not grow with (Berenbrink, Czumaj, Steger and Vöcking, 2000), while for one choice it grows like .
More choices, and a twist
The program compares for growing (averages over a few seeded runs; the C++ version below goes to a million bins and ).
import random
from math import log
def max_load(n, d, rng):
load = [0] * n
for _ in range(n):
best = rng.randrange(n)
for _ in range(d - 1):
c = rng.randrange(n)
if load[c] < load[best]:
best = c
load[best] += 1
return max(load)
rng = random.Random(121)
print(" n d=1 d=2 d=3 ln ln n / ln 2")
for n, runs in [(100, 20), (1000, 6), (10000, 2), (30000, 1)]:
row = [sum(max_load(n, d, rng) for _ in range(runs)) / runs for d in (1, 2, 3)]
print(f"{n:6} " + " ".join(f"{x:5.2f}" for x in row) + f" {log(log(n)) / log(2):14.2f}") n d=1 d=2 d=3 ln ln n / ln 2
100 4.20 2.60 2.00 2.20
1000 5.50 3.00 2.33 2.79
10000 6.00 3.50 3.00 3.20
30000 7.00 3.00 3.00 3.37
The jump from to is large; further choices help much less. Vöcking (2003) found a surprising twist: split the bins into groups, take one choice from each group, and break ties towards the leftmost group ("always go left"). The maximum load drops to , where is the growth rate of the -th order Fibonacci numbers (, the golden ratio). Asymmetric tie-breaking beats any symmetric rule, even with the same number of probes.
In C++
The -choice process in C++ for one million balls and bins, with a fixed-seed std::mt19937; the stream differs from Python's, so individual maxima can differ by one.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>
#include <vector>
int max_load(int n, int d, std::mt19937& rng) {
std::vector<int> load(n, 0);
for (int ball = 0; ball < n; ++ball) {
int best = rng() % n;
for (int j = 1; j < d; ++j) {
int c = rng() % n;
if (load[c] < load[best]) best = c;
}
++load[best];
}
return *std::max_element(load.begin(), load.end());
}
int main() {
std::mt19937 rng(121);
for (int n : {10000, 1000000}) {
std::printf("n = %7d:", n);
for (int d = 1; d <= 4; ++d) std::printf(" d=%d max %2d", d, max_load(n, d, rng));
std::printf(" ln ln n / ln 2 = %.2f\n", std::log(std::log(n)) / std::log(2.0));
}
}n = 10000: d=1 max 6 d=2 max 3 d=3 max 3 d=4 max 2 ln ln n / ln 2 = 3.20
n = 1000000: d=1 max 9 d=2 max 4 d=3 max 3 d=4 max 3 ln ln n / ln 2 = 3.79
In practice
- Load balancing. Sending each request to the less busy of two random servers needs only two load queries and avoids the herd behaviour of "send to the least loaded server overall", where stale load information makes every client pick the same server. Mitzenmacher's analysis of the supermarket model (1996) showed that in a queueing system two choices reduce the expected waiting time exponentially. Load balancers such as NGINX and HAProxy offer a "random with two choices" policy.
- Hashing. Two-choice hashing keeps chains of length . Cuckoo hashing goes further: each key has two possible slots and may move later, which makes the maximum load 1 (Advanced & Succinct Data Structures).
Test yourself
What is the maximum load when balls go into bins, each to the less loaded of two random bins?
In the layered-induction argument for choices, how does the fraction of bins with load at least relate to the fraction with load at least ?
- It is about half of it
- It is at most about its -th power
- It is equal
- It is about of it
How much does a third choice improve on two, for large ?
- Exponentially
- The leading term goes from to , a constant factor
- Not at all
- It makes the load constant
With balls and two choices, how does the gap between the maximum and the average load behave?
- It grows like
- It stays , independent of
- It grows like
- It shrinks to 0