System Design (High-Level Design) Module 4 – Networking for system designers
Load-balancing algorithms, from round robin up
Round robin, least connections, latency-aware and two-random-choices balancing, and hashing for cache locality, raced on one fleet with a slow server.
What you will learn
- Compare round robin, least connections, latency-aware and random-two-choices policies
- Use consistent hashing when the same key should keep reaching the same server
- Measure the effect of each policy on tail latency in a simulation you can change
Before you start
What you’ll learn · Before you start
What you will learn
- Compare round robin, least connections, latency-aware and random-two-choices policies
- Use consistent hashing when the same key should keep reaching the same server
- Measure the effect of each policy on tail latency in a simulation you can change
Before you start
On this page
Ten servers share 900 requests a second, and one of them has become three times slower than the rest. Round robin still sends it a tenth of the requests, nearly twice what it can finish, and by the end of a 40-second run the slowest 1 % of all requests take 30 seconds or more. A load-balancing algorithm decides which healthy server gets the next request, and that decision is what keeps one slow server a small problem.
Which one is right depends on three questions: do requests cost about the same, do the servers run at the same speed, and how fresh is the balancer’s view of the load? Below, six policies race on the same requests, first one decision at a time and then in a fleet you can change: add servers, raise the traffic, break a server, and watch the latency and the failures move.
What each policy reads before it sends
The simplest policies ignore load: round robin takes the servers in turn and random picks any of them. The load-aware ones look before they send: least connections picks the server with the fewest requests in progress, latency-aware policies prefer servers that have been answering quickly, and the power of two random choices compares just two servers picked at random and sends to the less busy one. Hashing policies send the same key to the same server, so that its cache stays warm. Here is the server each of them picks at one moment of a small fleet:
The drawing shows the whole scene; its 9 steps are listed below.
Every step as text
- The moment. The three servers hold 2, 0 and 5 active requests (requests in progress), and their recent responses averaged 9 ms, 14 ms and 31 ms. Server 3 is slow, so requests pile up there. A request from user 7 arrives.
- Round robin. Round robin reads nothing but its own turn. The turn has come round to server 3, so the request joins the 5 already there.
- Random. Random gives each server the same chance, 1 in 3, and reads nothing either. This time it draws server 1; the next request may just as well land on server 3.
- Least connections. Least connections reads the requests in progress on every server and takes the fewest. Server 2 holds none, so it gets the request.
- Lowest latency. Lowest latency takes the best moving average, 9 ms on server 1, although server 2 is idle. An average moves only when requests finish, so it describes the recent past, not this moment.
- Two random choices. Two random choices draws two servers, here 1 and 3, and takes the one with fewer requests in progress, server 1. It never looked at server 2, the idlest; it only had to avoid the worse of the two it drew.
- Latency x load. Latency x load also draws two servers, here 2 and 3, and compares average latency times requests in progress plus one. Server 2 scores 14 × 1 = 14 and server 3 scores 31 × 6 = 186, so server 2 wins.
- Hashing. Hashing reads no load at all. It turns user 7 into a server, here server 3, where that user's cached data is warm, so the request goes to the busiest server on purpose.
- Seven answers. The same moment gave seven answers. The policies that read load avoided server 3; round robin and hashing sent the request into its queue. Which answer is right depends on the costs of requests, the age of the counts and the need for a key to keep its server.
The moment is made up; each step applies the policy’s own rule. In more detail:
Round robin sends each request to the next server in a fixed rotation. It is the default of many balancers,
Amazon’s Application Load Balancer among them, and it is the right choice when requests cost about the same and the
servers are alike (ALB routing algorithms).
Equal counts are not equal work, though. Google’s SRE book reports requests whose CPU cost differs by a factor of
1,000 or more, and round robin leaving up to twice as much CPU use on the busiest task as on the idlest
(SRE book, chapter 20). Weighted round robin gives
bigger servers more turns: with weight=3 on one of three nginx servers, it gets 3 of every 5 requests
(nginx load balancing).
Random picks a server uniformly at random. On average it spreads load like round robin, with more variation from moment to moment; Envoy notes that it generally beats round robin when no health checks are configured, because it has no bias towards the server that follows a failed one in the rotation (Envoy load balancers).
Least connections, called least requests or least outstanding requests on HTTP balancers, sends each request to
the server with the fewest in progress. It reacts to slow servers without measuring their speed: by Little's law: In a stable system, the average number of requests inside equals the arrival rate times the average time each one spends inside, L = λW. At 100 requests a second and 50 ms each, about 5 are in progress. More in “Little's law, utilisation and server counts”
a server that takes longer holds more requests at once, so it looks busier and gets fewer new ones. HAProxy recommends
leastconn for long sessions such as LDAP and SQL and says it suits short HTTP sessions less well; Amazon suggests
least outstanding requests when requests vary in cost or targets vary in capacity
(HAProxy manual, balance). Its trap is a server that fails fast,
which the SRE book calls sinkholing.
Reading requests in progress and nothing else
A server that fails every request in 1 ms holds almost nothing in progress, so it looks like the least busy one: in the race figure below, least connections sent such a server 19.7 % of the requests, twice its share, all of them failing (load that case). Count each recent error as load, and eject a server after a run of errors.
Latency-aware policies keep a moving average of each server’s response times, usually an exponentially weighted moving average (EWMA), and favour servers that answer quickly. Linkerd uses EWMA to send requests to the fastest endpoints, and Finagle offers a “peak EWMA” that reacts sharply to slow responses and weighs the average by each server’s outstanding requests (Linkerd, Finagle). Used alone, a latency average herds traffic, as the race below shows.
The power of two random choices samples two servers at random and sends the request to the one with fewer
requests in progress. It needs no sorted list and no global view, and it avoids the herd behaviour of always picking
the single least loaded server. It is a default in several proxies: Envoy’s least request balancer compares 2 random
hosts by default when their weights are equal, HAProxy’s balance random makes 2 draws by default, nginx offers
random two least_conn, and Finagle’s default balancer is P2C, short for power of two choices
(Envoy,
HAProxy manual,
nginx upstream module).
Hashing turns a key of the request (the client’s address, a user ID, the URL) into a server, so that the same key keeps reaching the same server while the set of servers stays the same. It trades even load for locality, and the section on hashing below covers how to keep that trade cheap when servers come and go.
Key idea
A policy is defined by what it reads before it sends: nothing, a key, every server’s requests in progress, recent response times, or the load of two servers drawn at random.
Checkpoint
Check: What each policy reads
The race: a slow server’s share decides the tail
The race sends the same 36,079 requests to 10 servers under each policy. Each server works on one request at a time, most requests take 4 or 10 ms, and one in 200 takes 100 ms. In fleet 1, server 8 is three times slower than the rest, as a server with a noisy neighbour or a failing disk might be; in fleet 2, half the servers are twice as fast as the other half. The last two lines repeat fleet 1 with load counts that are 100 ms old, which is what each balancer sees when many balancers share a fleet and learn the load from periodic reports.
The figure runs fleet 1 under round robin. Press Play to watch its 40 seconds, then switch the policy and compare the same moment: requests travel to the server each one is sent to, and a request that takes longer than a second counts as failed, as a client with a 1-second timeout would see it. Its cases load the other fleets of this lesson, and the text below links to each one when it needs it.
- a request waiting or being worked on
- waiting longer than the 1 s timeout
- three times slower
- the server the narration is about
Second 40: 961 requests arrived. Round robin sent 96 of them to server 8, where 1,650 requests now wait, 39 more than a second ago; a request sent there now waits 33.2 s before its work starts. Half of this second's answered requests took up to 7.9 ms, the slowest 1 % 33.2 s or more; 9.8 % of the requests sent so far fail.
- slowest 1 % of the requests arriving that second
- the median: half of them took this long or less
- the timeout: a request above it fails
Over 40 s, round robin spread 36,079 requests over 10 servers: half of the answered ones took up to 8.6 ms, the slowest 1 % 30.1 s or more, and 9.8 % took longer than the 1 s timeout. Server 8, three times slower, received 10.0 % of them. The fleet can finish about 1,348 requests a second, so 900 a second keeps it about 67 % busy.
Every policy on this fleet
Each server over the run
The chart as a table
The program behind the figure prints every policy on both fleets, and with old counts. Change the numbers at the top and run it again:
// A load-balancer race: six policies send the same requests to the same fleet of 10 servers.
// Each server works on one request at a time, in arrival order. Change FLEETS, RATE or STALE_MS and run it again.
const RATE = 900; // requests a second, arriving at random (about Poisson)
const SECONDS = 40;
const STALE_MS = 100; // the last part: load reports this old, as many separate balancers would see them
// Work per request in ms on a normal server: 70 % take 4 ms, 25 % 10 ms, 4.5 % 25 ms, 0.5 % 100 ms.
const COSTS = [[0.7, 4], [0.95, 10], [0.995, 25], [1, 100]];
const FLEETS = [
{ name: "Fleet 1: server 8 is 3 times slower", speed: [1, 1, 1, 1, 1, 1, 1, 3, 1, 1], watch: [7], label: "to 8" },
{ name: "Fleet 2: servers 1-5 are twice as fast", speed: [0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1], watch: [0, 1, 2, 3, 4], label: "to 1-5" },
];
function random(seed) {
// mulberry32: a small seeded generator, so every run prints the same numbers
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = Math.imul(a ^ (a >>> 15), a | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function workload() {
const r = random(42);
const requests = [];
for (let tick = 0; tick < SECONDS * 10000; tick++) {
if (r() < RATE / 10000) {
const u = r();
requests.push({ at: tick / 10, cost: COSTS.find(([upTo]) => u < upTo)[1] });
}
}
return requests;
}
const POLICIES = {
"round robin": (s) => s.turn++ % s.n,
random: (s) => Math.floor(s.r() * s.n),
"least connections": (s) => {
let best = Infinity;
let ties = [];
for (let i = 0; i < s.n; i++) {
if (s.load[i] < best) [best, ties] = [s.load[i], [i]];
else if (s.load[i] === best) ties.push(i);
}
return ties[Math.floor(s.r() * ties.length)];
},
"two random choices": (s) => {
const [a, b] = twoDifferent(s);
return s.load[b] < s.load[a] ? b : a;
},
"lowest latency": (s) => s.latency.indexOf(Math.min(...s.latency)), // the best moving average
"latency x load": (s) => {
// two random choices again, comparing average latency times (requests in progress + 1)
const [a, b] = twoDifferent(s);
return s.latency[b] * (s.load[b] + 1) < s.latency[a] * (s.load[a] + 1) ? b : a;
},
};
function twoDifferent(s) {
const a = Math.floor(s.r() * s.n);
const b = Math.floor(s.r() * (s.n - 1));
return [a, b >= a ? b + 1 : b];
}
function race(requests, speed, policy, staleMs = 0) {
const n = speed.length;
const s = { n, r: random(7), turn: 0, load: new Array(n).fill(0), latency: new Array(n).fill(10) };
const done = speed.map(() => []); // finish times of each server's requests, in order
const next = new Array(n).fill(0); // the first of them not finished yet
const freeAt = new Array(n).fill(0);
const sent = new Array(n).fill(0);
const waits = [];
let reportAt = 0;
for (const q of requests) {
for (let i = 0; i < n; i++) {
while (next[i] < done[i].length && done[i][next[i]].end <= q.at) {
const d = done[i][next[i]++];
s.latency[i] += 0.2 * (d.end - d.at - s.latency[i]); // moving average of response times
}
if (!staleMs) s.load[i] = done[i].length - next[i]; // requests in progress right now
}
if (staleMs && q.at >= reportAt) {
for (let i = 0; i < n; i++) s.load[i] = done[i].length - next[i]; // a fresh report, then none for a while
reportAt = (Math.floor(q.at / staleMs) + 1) * staleMs;
}
const i = POLICIES[policy](s);
const end = Math.max(q.at, freeAt[i]) + q.cost * speed[i];
freeAt[i] = end;
done[i].push({ at: q.at, end });
sent[i]++;
waits.push(end - q.at);
}
waits.sort((x, y) => x - y);
const at = (p) => waits[Math.floor(p * waits.length)];
return { p50: at(0.5), p99: at(0.99), sent };
}
const ms = (x) => (x < 100 ? x.toFixed(1) : String(Math.round(x))).padStart(7);
const requests = workload();
console.log(`10 servers, ${RATE} requests a second for ${SECONDS} s`);
console.log(`(${requests.length} requests, the same for every policy)`);
for (const fleet of FLEETS) {
console.log(`\n${fleet.name}`);
console.log(`${"policy".padEnd(19)}${"p50 ms".padStart(7)}${"p99 ms".padStart(7)}${fleet.label.padStart(7)}`);
for (const policy of Object.keys(POLICIES)) {
const { p50, p99, sent } = race(requests, fleet.speed, policy);
const share = (100 * fleet.watch.reduce((sum, i) => sum + sent[i], 0)) / requests.length;
console.log(`${policy.padEnd(19)}${ms(p50)}${ms(p99)}${`${share.toFixed(1)}%`.padStart(7)}`);
}
}
console.log(`\nFleet 1, load reports ${STALE_MS} ms old`);
for (const policy of ["least connections", "two random choices"]) {
const { p50, p99 } = race(requests, FLEETS[0].speed, policy, STALE_MS);
console.log(`${policy.padEnd(19)}${ms(p50)}${ms(p99)}`);
} Output
10 servers, 900 requests a second for 40 s (36079 requests, the same for every policy) Fleet 1: server 8 is 3 times slower policy p50 ms p99 ms to 8 round robin 8.6 30145 10.0% random 11.6 32365 9.8% least connections 4.0 60.3 4.5% two random choices 7.6 100 4.8% lowest latency 7831 29773 2.2% latency x load 10.0 102 2.2% Fleet 2: servers 1-5 are twice as fast policy p50 ms p99 ms to 1-5 round robin 4.0 85.8 50.0% random 5.0 111 50.1% least connections 4.0 25.0 58.5% two random choices 4.0 34.6 55.7% lowest latency 3271 16956 65.1% latency x load 4.0 49.3 67.1% Fleet 1, load reports 100 ms old least connections 31.1 471 two random choices 13.5 182
Recorded with Node.js 24.21.0 on macOS 26 arm64. To run it yourself: mise exec node@24.21.0 -- node lb_race.mjs
Runs on this device, in your browser. The first run downloads JavaScript (about 0.6 MB), which is kept for the next runs.
Your run, in this browser
Read fleet 1 first. Round robin and random give the slow server its 10 %, about 90 requests a second, while it can finish only about 48, so its queue grows for as long as the run lasts and the p99 (99th percentile): The latency within which 99 % of requests finish, so the slowest 1 % take longer. Tail latency is read from such high percentiles, because an average hides the slowest requests. More in “Latency, throughput and percentiles” reaches 30 s; in the figure 9.8 % of round robin’s requests fail, nearly every one sent to server 8 after its first second. Both load-aware policies cut its share to under 5 % and keep the p99 near 60 to 100 ms. Least connections wins here, because one balancer with exact counts can always find an idle server; two random choices comes close with far less information. “Lowest latency”, which always picks the best moving average, is the warning: an average changes only when requests finish, so every new request goes to whichever server looked fastest until its queue has grown, then to the next one, and the median wait reaches 7.8 s. Comparing latency times load between two random servers (“latency x load”) fixes that, and in fleet 2 it sends the fast servers 67.1 % of the requests, close to their two thirds of the capacity: load fleet 2 into the race and switch to latency x load to watch it.
Key idea
A policy that ignores load gives a slow server its full share, and its queue grows for as long as the traffic lasts; a policy that reads requests in progress sends it only what it can finish.
Checkpoint
Check: A slow server's share
Capacity first: what no policy can fix
A policy can only share out the capacity the fleet has. With the race’s mix, an average request needs 6.925 ms of work (0.7 × 4 + 0.25 × 10 + 0.045 × 25 + 0.005 × 100), so one normal server finishes about 144 requests a second and server 8, three times slower, about 48. Fleet 1 can finish 9 × 144 + 48 ≈ 1,348 requests a second, so 900 a second keeps it about 67 % busy: that is its utilisation: The share of time a server spends working. For a fleet, the arrivals a second times the average work of a request, divided by the number of servers; at 100 % or more, queues grow without limit. More in “Little's law, utilisation and server counts”. A load-aware policy spends the spare third on the requests server 8 cannot take; round robin leaves it unused.
Two experiments show the limits. Keep round robin and add servers: each one cuts server 8’s share, but its queue keeps growing until the share falls below 48 a second, which takes 19 servers (900 ÷ 19 ≈ 47). With 16 servers the p99 is still 7.8 s. Then make every server alike and raise the traffic. Ten alike servers can finish about 1,444 requests a second, and here is the share of requests that failed, by taking longer than a second, in the same model:
| Policy | 1,400 a second | 1,500 a second | 1,500 a second, 11 servers |
|---|---|---|---|
| Round robin | 0 % | 55.5 % | 0 % |
| Random | 2.4 % | 44.2 % | 0 % |
| Least connections | 0 % | 47.0 % | 0 % |
| Two random choices | 0 % | 47.9 % | 0 % |
| Lowest latency | 93.9 % | 94.1 % | 93.4 % |
| Latency x load | 0 % | 47.5 % | 0 % |
At 1,400 requests a second the fleet is 97 % busy and least connections keeps the p99 at 146 ms. At 1,500, just above capacity, every queue grows a little every second and never drains, and every policy fails about half of the requests; lowest latency fails nearly all of them even below capacity, because it herds. One more server lifts the capacity to about 1,588 requests a second, and least connections is back to a p99 of 110 ms. Load ten alike servers at 1,500 a second into the race, press Play, then add one server and watch the queues drain.
The way out of an overload is more capacity, or turning work away early, as load shedding does; a policy only decides who waits.
Key idea
Capacity comes first: above 100 % utilisation every queue grows whatever the policy, and below it the policy decides who waits.
Checkpoint
Check: Capacity before policy
Old counts turn the best pick into a herd
The last part of the race is the surprise. With counts 100 ms old, least connections sends every request between two reports to the same few servers that looked idle, and its p99 rises to 471 ms; two random choices spreads the same requests over random pairs and stays at 182 ms. Here is the same effect, shrunk to four servers and six requests between two reports:
The drawing shows the whole scene; its 7 steps are listed below.
Every step as text
- A report arrives. A load report says the four servers hold 3, 0, 2 and 4 requests in progress. The balancer keeps these counts until the next report, 100 ms from now. To keep the counting simple, no request finishes before then, so the true counts only grow.
- Request 1. Least connections sees server 2 at 0, the fewest, and sends the request there. Server 2 now really holds 1.
- Requests 2 and 3. The balancer's counts still say server 2 holds 0, so the next two requests go there as well. Server 2 now really holds 3.
- Requests 4 to 6. Every request until the next report follows the same old count. Server 2 ends the 100 ms holding 6, the most of any server, while the balancer still believes it is idle.
- The next report. The next report shows 3, 6, 2 and 4. Now server 3 looks idlest, and the next 100 ms of requests herd onto it instead. The counts are right every 100 ms and wrong in between.
- Two random choices instead. Rewind to the first report and send the same six requests with two random choices. Server 2 wins only the draws that include it, 3 of the 6 possible pairs, so it gets about 3 requests and the rest spread to servers 3 and 1.
- Why it holds up. A stale low count can win only the draws it appears in, about 2 in n of them, so with 10 servers it draws at most a fifth of the requests between reports. In the race, with counts 100 ms old, that kept the p99 at 182 ms where least connections reached 471 ms.
Mitzenmacher showed this in queueing models: with old load information, sending tasks to the apparently least loaded server can significantly hurt, while the less loaded of two random servers stays effective across a wide range of settings (How Useful Is Old Information?). Each of many balancers that counts only its own requests is in a similar position, and that is how sidecar proxies usually see the load.
Old counts in the race
Load fleet 1 with counts 100 ms old into the race: least connections now has a p99 of 471 ms. Switch to two random choices (182 ms), then untick the old counts to compare with fresh ones: 60 ms and 100 ms.
Client-side balancing
Everything above can run in a proxy or in the client itself. With client-side balancing each client gets the list of servers from service discovery and picks one per request, as gRPC clients can. gRPC’s guide weighs it against a proxy: no extra hop and better performance, but a more complex client, a separate implementation for every language, and clients that must be trusted, or a separate “lookaside” balancer that tells clients which servers to use (gRPC Load Balancing). Because every client sees only its own requests, client-side balancers are exactly the case where two random choices beats picking the single least loaded server.
Why two choices are enough
The classic model is balls thrown into bins. Throw n balls into n bins at random and the fullest bin ends up with about log n / log log n balls; let each ball look at d ≥ 2 random bins and take the emptiest, and the fullest bin holds about log log n / log d plus a constant (Mitzenmacher, Richa and Sitaraman). The jump from one choice to two is exponential; a third choice only improves things by a constant factor, which Mitzenmacher also found for queues where tasks wait for service (The Power of Two Choices). Here is that model run:
"""Balls into bins: the fullest bin with one random choice, two choices and three choices.
Each ball goes to one random bin, or to the emptier of d bins chosen at random. Five seeded runs each.
"""
import random
BINS = 1_000
def fullest(balls, choices, seed):
rng = random.Random(seed)
load = [0] * BINS
for _ in range(balls):
best = rng.randrange(BINS)
for _ in range(choices - 1):
other = rng.randrange(BINS)
if load[other] < load[best]:
best = other
load[best] += 1
return max(load)
for balls in (1_000, 100_000):
average = balls // BINS
print(f"{balls:,} balls in {BINS:,} bins ({average} per bin)")
print(f"{'':10}{'fullest bin, 5 runs':>20}{'over avg':>9}")
for choices in (1, 2, 3):
runs = [fullest(balls, choices, seed) for seed in range(5)]
label = "1 choice" if choices == 1 else f"{choices} choices"
print(f"{label:10}{''.join(f'{r:>4}' for r in runs)}{max(runs) - average:>9}") Output
1,000 balls in 1,000 bins (1 per bin)
fullest bin, 5 runs over avg
1 choice 5 5 6 6 5 5
2 choices 3 3 3 3 3 2
3 choices 2 2 2 2 2 1
100,000 balls in 1,000 bins (100 per bin)
fullest bin, 5 runs over avg
1 choice 138 142 135 129 132 42
2 choices 102 102 102 102 102 2
3 choices 101 102 102 102 101 2
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 p2c.py
Runs on this device, in your browser. The first run downloads Python (about 13.5 MB), which is kept for the next runs.
Your run, in this browser
With 1,000 balls in 1,000 bins, one random choice left a bin with 5 or 6 balls and two choices kept every bin at 3 or fewer. With 100 balls per bin on average, one choice left the fullest bin 29 to 42 balls above the average, while two choices kept it 2 above, in every run. A third choice made almost no difference. For a load balancer, the bins are servers and the balls are requests: one extra look at the load is what removes the hot spots.
Checkpoint
Check: Why two choices are enough
Hashing when the same key should reach the same server
Hashing is the policy to use when serving a key from the same server is worth more than perfectly even load: a cache
tier, where a second server would miss; per-user state that is expensive to rebuild; or a shard. HAProxy’s balance uri, for example, hashes the URL so that proxy caches see each object on one server and keep their hit rate high
(HAProxy manual).
The obvious way, hash(key) mod N, breaks when N changes. HAProxy’s manual says of its map-based hashing that most
mappings change when the server count changes, and nginx warns that adding or removing a server may remap most keys
(nginx upstream module). For a cache tier, that is a
mass miss at the moment you added capacity. Consistent hashing places servers and keys on a ring and gives each key
to the next server point after it, so a server that joins or leaves moves only the keys next to its points: about
1/N of them, as Envoy puts it for its ring hash. Try it on the ring below, and compare the keys that move with hash mod
n:
Add and remove servers, change the number of virtual nodes, and see which keys move. Each key belongs to the first server point clockwise from it.
At the start: 3 servers with 4 virtual nodes each hold 200 keys: A 69 (35%), B 41 (21%), C 90 (45%).
Maglev hashing, from Google’s software network load balancer, fills a fixed lookup table instead of a ring. By Envoy’s figures it builds its table about 10 times faster and picks hosts about 5 times faster than a large ring, at the cost of moving about twice as many keys when hosts change (Envoy load balancers, Maglev).
Hashing ignores load, so one hot key can overload its server. Bounded-load consistent hashing caps every server
at a multiple of the average load and, when a request’s first-choice server is full, picks another from the same
hash.
HAProxy’s hash-balance-factor does this: at 150, no server takes more than 1.5 times the average, and the manual
calls 125 to 200 reasonable values (HAProxy manual).
Checkpoint
Check: Hashing for locality
Three questions choose the policy
Three questions about your fleet decide most of the choice. Answer them in the table below: it marks what fits and says why the rest does not, with the race’s numbers in every cell.
Answer the questions above: the table then marks what fits your case and says why the rest does not.
| Policy | One slow server | Old or partial counts | A server failing fast | Same key, same server |
|---|---|---|---|---|
| Round robin Reads before it sends: its own rotation | Poor keeps its full share: p99 30 s in the race | Good reads no counts, so none can be old | Mixed its fair share fails: 10 % of the requests | Poor a key may land on any server |
| Random Reads before it sends: nothing | Poor keeps its share on average: p99 32 s | Good reads no counts | Mixed about its fair share fails: 9.8 % | Poor a key may land on any server |
| Least connections Reads before it sends: requests in flight on every server | Good gets 4.5 %, less than it can finish: p99 60 ms | Poor herds between reports: p99 471 ms | Poor looks idle, so it draws twice its share: 19.7 % | Poor a key may land on any server |
| Lowest latency Reads before it sends: each server's recent response times | Poor herds onto whoever looks fastest: p50 7.8 s | Poor an average is old news by nature | Poor errors are fast, so it draws 99.5 % | Poor a key may land on any server |
| Two random choices Reads before it sends: requests in flight on two random servers | Good gets 4.8 %: p99 100 ms | Good p99 182 ms with counts 100 ms old | Mixed held under 2 in n of the picks: 14.8 % | Poor a key may land on any server |
| Latency x load Reads before it sends: latency times load on two random servers | Good gets 2.2 %: p99 102 ms | Good p99 137 ms with counts 100 ms old | Poor fast errors score well: 20.3 % | Poor a key may land on any server |
| Consistent hashing Reads before it sends: a key of the request | Poor keeps every key it owns, slow or not | Good reads no counts | Poor its keys fail until it is removed | Good a key keeps its server; a new server takes about 1 in n keys |
How the answers decide
- “Do your requests cost about the same, on servers that run at the same speed?” No, costs or speeds differ: rules out Round robin and Random. A slow or costly server keeps its full share and its queue grows: in the race, server 8 got 10 % of the requests and could finish about half of them.
- “Do your requests cost about the same, on servers that run at the same speed?” No, costs or speeds differ: adds a caveat to Least connections, Two random choices and Latency x load. Count errors as load, or a server that fails fast looks idle and draws more requests, twice its share under least connections.
- “Do your requests cost about the same, on servers that run at the same speed?” Yes, they are alike: rules out Lowest latency. A latency average used alone herds even when every server is alike: with ten alike servers the slowest 1 % still took 25 s.
- “Do your requests cost about the same, on servers that run at the same speed?” No, costs or speeds differ: rules out Lowest latency. A latency average used alone herds onto whoever looks fastest: half the requests in the race waited 7.8 s or more.
- “Does one balancer see fresh counts of the requests in flight on every server?” No, many balancers or old counts: rules out Least connections. Between two reports every request goes to the server that looked idlest: with counts 100 ms old, the p99 rose from 60 ms to 471 ms.
- “Must the same key keep reaching the same server, as for a cache, a session or a shard?” Yes: rules out Round robin, Random, Least connections, Lowest latency, Two random choices and Latency x load. Only hashing keeps a key on its server; any other policy may send the next request for a key elsewhere, where its cache or session is missing.
- “Must the same key keep reaching the same server, as for a cache, a session or a shard?” Yes and “Do your requests cost about the same, on servers that run at the same speed?” No, costs or speeds differ: adds a caveat to Consistent hashing. Cap every server's load, as bounded-load consistent hashing does, so that a hot key or a slow server cannot take one server down.
- “Must the same key keep reaching the same server, as for a cache, a session or a shard?” No: rules out Consistent hashing. Hashing gives up even load for locality you do not need: a slow server keeps every key it owns.
In short: round robin (weighted if servers differ in size) for alike requests and servers, least connections for one balancer with exact counts, two random choices for many balancers or old counts (on latency times load when speeds differ), and consistent or Maglev hashing, with a load cap, when a key must keep its server. Whatever you pick, count errors as load, or a fast-failing server will attract traffic: in the race’s fleet, a server that fails every request takes 19.7 % of them under least connections, twice its share (load the failing server into the race). Keep health checks and slow start from the previous lesson too.
When you are not sure
If costs or speeds differ and you cannot promise fresh counts, start from two random choices, on requests in flight or on latency times load, with errors counted as load: in every race of this lesson with a slow server, fresh counts or old, both kept the p99 under 200 ms.
Practise: write four policies
Write the decision each of four policies makes, in Python, and run the sample tests. The race and its figure use the same rules:
Exercise · Easy · Python
Write the choices of four balancing policies
Write the decision each policy makes, in policies.py. Servers are numbered from 0, and in_flight lists how many requests each server has in progress.
round_robin(n, turn)returns the server for request numberturn(counting from 0) when there arenservers.least_loaded(in_flight, start=0)returns the server with the fewest requests in flight. Look at the servers in order fromstart, wrapping around after the last one, and on a tie keep the first one you found, so that different values ofstartspread ties over different servers.two_choices(in_flight, a, b)returns whichever of serversaandbhas fewer requests in flight, andaon a tie (the two servers were already picked at random).update_average(average, sample, weight=0.2)returns a moving average of response times after one more sample: the old average movedweightof the way towards the sample.
For example, least_loaded([3, 1, 4, 1], start=2) is 3, and update_average(10, 20) is 12.0.
Starter code · policies.py
def round_robin(n, turn):
"""The server for request number `turn` of n servers."""
# Replace this line with your code.
return 0
def least_loaded(in_flight, start=0):
"""The server with the fewest requests in flight; ties go to the first found from `start`."""
# Replace this line with your code.
return 0
def two_choices(in_flight, a, b):
"""The less loaded of servers a and b; a on a tie."""
# Replace this line with your code.
return 0
def update_average(average, sample, weight=0.2):
"""The moving average after one more sample."""
# Replace this line with your code.
return 0 The sample tests · test_policies.py
import math
from policies import least_loaded, round_robin, two_choices, update_average
def test_round_robin():
"""takes the servers in turn and starts again after the last"""
assert [round_robin(3, t) for t in range(7)] == [0, 1, 2, 0, 1, 2, 0]
assert round_robin(10, 25) == 5
def test_least_loaded():
"""finds the server with the fewest requests in flight"""
assert least_loaded([5, 2, 7, 3]) == 1
assert least_loaded([0, 4, 4]) == 0
assert least_loaded([9, 8, 7, 6]) == 3
def test_least_loaded_ties():
"""breaks ties by the first server found from start, wrapping around"""
assert least_loaded([3, 1, 4, 1]) == 1
assert least_loaded([3, 1, 4, 1], start=2) == 3
assert least_loaded([3, 1, 4, 1], start=3) == 3
assert least_loaded([2, 2, 2], start=1) == 1
assert least_loaded([1, 5, 1], start=2) == 2
def test_two_choices():
"""keeps the less loaded of the two, and the first on a tie"""
assert two_choices([4, 1, 6], 0, 1) == 1
assert two_choices([4, 1, 6], 2, 0) == 0
assert two_choices([4, 4, 6], 1, 0) == 1
def test_update_average():
"""moves the average part of the way towards each sample"""
assert math.isclose(update_average(10, 20), 12.0)
assert math.isclose(update_average(10, 20, weight=0.5), 15.0)
assert math.isclose(update_average(50, 10, weight=0.1), 46.0) A hint
turn % n is round robin. For least_loaded, walk k from 0 to len(in_flight) - 1 and look at server (start + k) % len(in_flight); replace the best so far only when a server has strictly fewer requests in flight. The moving average is average + weight * (sample - average).
Results of the sample tests
| Test | Result | Details |
|---|
What your code printed
The sample tests run on this device, in your browser (Pyodide): nothing is sent to mysmartcopilot.com. The first run downloads Python (about 13.5 MB), which is kept for the next runs. A check in your browser is feedback for you, not proof that the code is right for every input.
Then check yourself, one question at a time, weakest idea first:
Practice that adapts to you
Practise this lesson
10 questions about 7 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Design practice: fleets to balance
These are the decisions a design round asks you to make about balancing. For each brief, decide first, then test your choice in the race figure above: it takes 2 to 20 servers, up to 2,000 requests a second and every kind of trouble in this lesson, and its case “One server fails every request” is the second brief below. Start with the first decision of two of them:
Checkpoint
Your first decision
Interview questions
Warm-up (fresher to mid level): what is the power of two random choices in load balancing, and why is it popular? The balancer picks two servers at random and sends the request to the one with fewer requests in progress, or the lower latency-times-load score. In the balls-and-bins model it removes almost all of the imbalance that pure random choice leaves: the fullest of n bins drops from about log n / log log n balls to about log log n / log 2, and a third choice adds little. It is popular because it costs two lookups instead of a scan of every server, needs no global view, and stays good when load counts are old or each balancer sees only its own requests, where picking the single least loaded server sends everyone to the same place. Envoy, HAProxy, nginx and Finagle all offer it, and several use it by default.
Key takeaways
- Round robin and random give a slow server its full share: in the race its queue grew without limit and the p99 hit 30 s.
- Least connections reacts to slow servers through the requests they hold; with one balancer and exact counts it gave the best tail, 60.3 ms.
- No policy beats capacity: above 100 % utilisation every queue grows, and round robin needed 19 servers to stop one slow server overloading, where a load-aware policy needed none.
- Latency averages used alone herd traffic; combined with load and two random choices they adapt to faster servers.
- With old or partial counts, two random choices (p99 182 ms) beat least connections (471 ms); two choices instead of one cut the fullest bin from 42 above the average to 2.
- Hash on a key only when locality matters, use consistent or Maglev hashing so scaling moves few keys, and cap the load so a hot key cannot take one server down.
Terms in this lesson
- consistent hashing
- Servers and keys are placed on one ring, and each key belongs to the first server point after it. A server that joins or leaves moves only the keys next to its points, about 1 in n of them.
- EWMA
- An exponentially weighted moving average. Each new sample moves the average a fixed share of the way towards it, so recent samples count most; the race in this lesson moves it 0.2 of the way per response.
- herd behaviour
- Many senders acting on the same old information pick the same target at the same moment, overload it, and then move on together to the next one.
- power of two random choices
- Draw two servers at random and send the request to the one with less load. One extra look removes almost all of the imbalance that a purely random pick leaves.
- sinkholing
- A failing server that answers errors quickly holds almost no requests in progress, so a load-aware balancer takes it for the least busy server and sends it more and more traffic, all of which fails.
References
- The Power of Two Choices in Randomized Load Balancing (IEEE TPDS, 2001) (IEEE (author's copy, Michael Mitzenmacher))
- The Power of Two Random Choices: A Survey of Techniques and Results (Handbook of Randomized Computing (authors' copy, Mitzenmacher, Richa and Sitaraman))
- How Useful Is Old Information? (IEEE TPDS, 2000) (IEEE (author's copy, Michael Mitzenmacher))
- Supported load balancers (Envoy documentation) (Envoy Project)
- Maglev: A Fast and Reliable Software Network Load Balancer (Google Research (USENIX NSDI 2016))
- HAProxy 3.2 Configuration Manual (balance, hash-type, hash-balance-factor) (HAProxy Technologies)
- Module ngx_http_upstream_module (nginx)
- Using nginx as HTTP load balancer (nginx)
- Edit target group attributes for your Application Load Balancer (routing algorithms) (Amazon Web Services)
- Site Reliability Engineering, chapter 20: Load Balancing in the Datacenter (Google (O'Reilly Media))
- Load balancing (Linkerd documentation) (Linkerd Authors)
- Finagle clients (load balancing) (Finagle project)
- gRPC Load Balancing (gRPC Authors)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress