ealeng

Episode 12 of 55 · Load balancing

Load balancing with one slow server

Four servers behind HAProxy, one of them 5× slower. At 31% of their total capacity, round robin's slowest requests waited 5.4 s; least connections kept them at 53 ms. Same servers, same traffic, one setting. How a balancer picks a server, why a policy that doesn't look at load collapses far below capacity, why a slow server stays in your p99 until you stop sending it traffic, why one p99 over mixed requests hides the difference, and what an L4 balancer spreads.

Lab 07 · Load Balancing and Proxies16 min read
Video transcript7:29

0:00Cold open

0:01

Four servers behind a load balancer. One of them is slow.

0:06

At less than a third of their total capacity, my slowest requests waited over five seconds.

0:14

I changed one setting in the balancer. Fifty-three milliseconds.

0:20

Same servers, same traffic. What changed?

0:26Intro

0:27

Hands-on system design, part twelve.

0:30

Today: how a load balancer picks a server, and what one slow server does to it.

0:37Meet Lucía

0:38

I’m Lucía, a platform engineer.

0:41

I have four servers. Each one handles four requests at a time, and a request takes ten milliseconds. So each server does four hundred a second.

0:52

Except server two. It’s five times slower: fifty milliseconds a request, eighty a second. All four together can do one thousand two hundred eighty.

1:03

Think of a supermarket. A server is a till, with four checkout slots. A request is a shopper. And the load balancer is the person at the front, pointing each shopper to a till.

1:1601 · What you need

1:18

To run it yourself: Python with a few packages, and Docker for the balancer and the four servers.

1:25

The quick tests run in a couple of seconds, without Docker.

1:30

No cloud, and no API keys.

1:3502 · The setting, in code

1:37

The choice is one setting. Here are four balancers, one per setting, all in front of the same four servers.

1:45

Round robin: the next till in turn.

1:48

Least connections: the till with the shortest line.

1:53

Random: any till.

1:56

And random of two: look at two tills at random, and take the shorter line. It’s called the power of two choices. From here on, I’ll just say two choices.

2:0803 · Light traffic

2:09

First, light traffic: two hundred requests a second. Sixteen percent of what the four can do.

2:16

I’ll look at the p99: the slowest one in a hundred. Round robin: fifty-seven milliseconds.

2:24

Random: eighty-nine. Random sometimes sends the slow till five shoppers within fifty milliseconds. It has four slots, so the fifth waits.

2:35

Least connections: fifty-four. Two choices: fifty-five.

2:4204 · Twice the traffic

2:43

Now four hundred a second. Still less than a third of what the four can do.

2:49

What happens to the slowest requests?

2:55

Round robin: five point four seconds. Random: six point four.

3:01

Least connections: fifty-three milliseconds. Two choices: sixty.

3:0805 · Why

3:10

Round robin sends server two a quarter of the shoppers: a hundred a second. It can serve eighty.

3:17

Least connections counts the open requests at each till. Server two had more open, because it’s slow, so it got fewer: about thirty-nine a second.

3:29

With round robin, twenty more arrive than it can serve, every second. So its queue grew for the whole twenty-second test. That’s how the slowest waits reached over five seconds.

3:41

The balancer had three tills with room to spare, and kept feeding the overloaded one.

3:4906 · Even more traffic

3:50

At eight hundred a second, about sixty-three percent of capacity: round robin, thirty-one seconds. Random, thirty-two.

3:59

Least connections: sixty milliseconds. Two choices: a hundred and five.

4:06

Before the real test, I ran a small simulation: a quick program that fakes the four tills. It got the order right. Its biggest miss: it predicted sixty-four milliseconds for two choices. The real balancer gave a hundred and five. We didn’t find out why.

4:2507 · Never below 50 ms

4:26

But no policy got below fifty milliseconds. Look at server two’s share: the part of all shoppers it got. Even the best policies sent it eight to sixteen percent.

4:38

And each of those takes fifty milliseconds. So the slowest one in a hundred is at least that.

4:45

If more than one shopper in a hundred goes to the slow till, the slowest one in a hundred is one of them, so it waits at least fifty milliseconds. Its share has to fall below one percent. Stop sending it traffic, don’t just send it less.

5:0208 · Uneven shoppers

5:03

Second test: four equal servers, but uneven shoppers. Most requests take five milliseconds. One in twenty takes a hundred.

5:13

The p99 of all requests: a hundred and two milliseconds, for every policy. No difference at all.

5:22

Why? One in twenty takes a hundred milliseconds, so the slowest one in a hundred is always one of those.

5:30

Now look only at the quick requests. Round robin: twenty-eight milliseconds. Random: forty-six.

5:39

Least connections: eight. Two choices: eleven. A till busy with a big order has fewer free slots, and least connections sees that.

5:49

So split latency, how long requests take, by request type, quick or big, before you compare.

5:5709 · What it spreads

5:59

One more thing. A connection is like a bus: a client opens it and sends many requests on it. Some balancers point the whole bus to one till. That’s called L4. Others point each shopper. That’s L7.

6:14

One client keeps one connection open and sends two thousand requests. Through the per-connection balancer, all two thousand went to one server.

6:25

Through the per-request balancer: five hundred each.

6:30

With two connections, two servers got everything. With four or more connections, it evened out.

6:37

Apps that keep a few connections open can hit this.

6:43Back to Lucía

6:44

So, four lessons.

6:47

Pick a balancer that looks at the queues, like least connections or two choices.

6:53

A slow server still owns your p99 until you stop sending it traffic.

7:00

Split latency by request type before you compare.

7:05

And know what your balancer spreads: connections, or requests.

7:10

Next time: servers that die, and servers that get replaced.

7:16

For today: look at the lines.Lucía: Look at the lines.

7:19Summary

7:21

Look at the lines, remove the slow one, split by request type, and know what you spread.

Key takeaways

  • ✓A load balancer picks a server for each piece of work. Round robin takes turns and random picks any server: neither looks at load. Least connections picks the server with the fewest requests in flight; the power of two choices looks at two random servers and takes the less busy one.
  • ✓With one server 5× slower (80 requests a second instead of 400; 1,280 for the four together), round robin and random collapsed at 400 requests a second, 31% of capacity: p99 5.4 s and 6.4 s. They sent the slow server a quarter of the traffic, 100 a second, and its queue grew for the whole 20 s run while three servers had room to spare.
  • ✓Least connections kept p99 at 53–60 ms at 200, 400 and 800 requests a second, and two choices at 55–105 ms. The slow server holds its requests longer, so it has more in flight and gets fewer new ones: 9.8% of the traffic at 400 a second, about 39 requests a second.
  • ✓No policy got p99 below 50 ms. Even the best sent the slow server 8–16% of requests, and each takes 50 ms. To get it out of p99, its share has to fall below 1%: stop sending it traffic, don't just send it less.
  • ✓With uneven requests (95% take 5 ms, 5% take 100 ms), p99 over all requests was 102 ms for every policy. The quick requests alone showed the difference: 28 ms with round robin, 46 with random, 8 with least connections, 11 with two choices. Split latency by request type before you compare.
  • ✓An L4 balancer picks a server once per connection, an L7 balancer once per request. One client with one keep-alive connection put all 2,000 requests on one server through L4; L7 spread them 500 each. From 4 connections up, L4 was even.
  • ✓The lab's simulation, written before the real test, predicted the ranking and the slow server's share. Its biggest miss: 64 ms predicted for two choices at 800 requests a second, against 105 ms measured. The lab didn't find out why.

Four servers sit behind a load balancer, and one of them is slow. At less than a third of their total capacity, the slowest requests waited more than five seconds. One setting changed in the balancer brought them down to 53 milliseconds: same servers, same traffic. This chapter measures how a load balancer picks a server, what one slow server does to each way of picking, and two traps when you compare them.

The examples come from Lab 07 (Load Balancing and Proxies) of the system design labs: script 02_algorithms.py (four balancing policies in front of one slow server, then uneven requests), its simulation module lb_sim.py, and Exercise 3 of 01_proxy_overhead.py (what L4 and L7 balancers spread). The same lab's health-check and deploy scripts (03 and 04) are the next chapter, Chapter 13. Two parts of the lab aren't covered in this track: what a proxy costs per request (Exercises 1 and 2 of 01_proxy_overhead.py; in short, tens of microseconds) and sticky sessions (05_sessions.py). The setup is the earlier chapters' laptop (an Apple M4 Max, Docker Desktop with 16 virtual CPUs): four copies of a Python web service in Docker, with HAProxy 3.2 (a widely used open-source load balancer) in front, and the load generator in another container next to it. The lab's 407 unit tests (the simulation, the metrics and the config generator) run in about 2 seconds without Docker; they all pass.

A supermarket with four tills

A load balancer sits between clients and a pool of servers and picks a server for each piece of work. Think of a supermarket. A server is a till with four checkout slots, a request is a shopper, and the load balancer is the person at the front pointing each shopper to a till.

The lab's four servers are called s1–s4. Each has 4 worker slots, and a request holds a slot for 10 ms: each server does 4 ÷ 10 ms = 400 requests a second. Except s2, which the lab makes 5× slower on purpose (each request holds its slot for 50 ms): 80 a second, like a server with a bad disk or a noisy neighbour. Together, the four can do 400 + 80 + 400 + 400 = 1,280 requests a second.

The choice of how to pick is one setting. The lab runs four balancers side by side in one HAProxy, one per setting, all in front of the same four servers. From lb_config.py, which writes HAProxy's config from Python (shortened):

Listener("algo_rr", 8101, balance="roundrobin", **algo),
Listener("algo_lc", 8102, balance="leastconn", **algo),
Listener("algo_random", 8103, balance="random(1)", **algo),   # plain "random" means random(2) in HAProxy
Listener("algo_p2c", 8104, balance="random(2)", **algo),

A listener is one port on HAProxy (8101–8104 here) with its own list of the same four servers; algo_rr and the rest are just their names, and balance is the setting. (algo holds settings shared by all four: a long server timeout, so an overloaded queue shows up as latency rather than errors, and a server weight of 100, explained in the caveats.)

  • Round robin (roundrobin): the next till in turn, s1, s2, s3, s4, s1… Doesn't look at load.
  • Random (random(1)): any till. Doesn't look at load.
  • Least connections (leastconn): the till with the fewest shoppers, waiting or being served. HAProxy counts the requests it has sent to each server that haven't come back yet.
  • Power of two choices (random(2); the lab calls it p2c, this chapter says two choices): look at two tills at random and take the one with fewer shoppers. It needs two looks instead of a scan of every server.

Watch them in the lab's own simulation, lb_sim.py: a short program that fakes the four tills, with shoppers arriving at random. Pick a policy and a rate, and press play:

Try it · Four tills, one slowA simulation · Lab 07 lb_sim.py · 4 slots per till, 10 ms a shopper

A simulation, not a measurement: the lab's own load-balancer simulation, one 20 s run (seed 0), shoppers arriving at random. Each till has 4 checkout slots and spends 10 ms per shopper; till 2 spends 50 ms. The person at the front picks a till for each shopper.

20.0 s of 20 s
balancerTill 1400/sTill 25× slower · 80/s+395Till 3400/sTill 4400/s
Slot busy Slot free Waiting in line
Till 2's line now
417
Sent to till 2
25.0%
Till 2 gets
100/s
p99, this run
5.0 s

Till 2 gets 100 shoppers a second and serves 80: 20 more arrive than leave, every second, so its line grows for the whole run. A shopper joining it at 20.0 s waits about 417 ÷ 80 ≈ 5.2 s. Tills 1, 3 and 4 have free slots the whole time.

One simulated run per setting, sampled every 0.5 s (a burst between two samples doesn't show). Shares and p99 drop the first 2 s as warm-up, as the lab does. The lab's prediction table is the median of 5 such runs; the next widget has those and the real HAProxy measurements. Two choices means the power of two choices: look at two tills at random and take the one with fewer shoppers.

Light traffic: everyone copes, not equally

First, 200 requests a second, 16% of what the four can do. Each policy ran for 20 s, twice, in alternating order; the first 2 s of each run are dropped as warm-up. p99 is the latency the slowest 1 request in 100 exceeds; p50, the median, was 11–12 ms for every policy at every rate of this test, and no request failed.

Policy p99 Share of requests sent to s2
round robin 57 ms 25.0%
random 89 ms 25.4%
least connections 54 ms 11.7%
two choices 55 ms 15.6%

At this rate s2 keeps up even with a full quarter: 25% of 200 is 50 a second, under its 80. So every p99 sits a little above 50 ms, the slow server's own time. Random is the worst. Round robin hands s2 every fourth request, evenly spaced; random sometimes sends it several close together. Five within 50 ms, with four slots, and the fifth waits for a slot. (That's the likely mechanism, consistent with the simulation; the lab didn't trace individual requests.)

Twice the traffic: collapse at 31%

Now 400 requests a second, still less than a third of the 1,280 the four can do:

Policy p99 Share sent to s2 s2 got
round robin 5.4 s 25.0% 100 a second
random 6.4 s 26.0% 104 a second
least connections 53 ms 9.8% 39 a second
two choices 60 ms 12.8% 51 a second

Round robin and random collapsed. Round robin sends s2 a quarter of the shoppers: 100 a second, to a till that serves 80. Twenty more arrive than it can serve, every second, so its line grew for the whole 20-second run: about 400 waiting by the end, and 400 ÷ 80 = 5 s of work in front of the last one to join. That's how the slowest waits reached 5.4 s. Meanwhile the other three servers had room to spare, and the balancer kept feeding the overloaded one.

Least connections and two choices didn't. s2 holds each request five times longer, so it has more open at any moment, and a policy that counts them sends it fewer: 39 a second under least connections, under the 80 it can serve. Nobody told the balancer s2 was slow. It saw the pile.

Note what "31% of capacity" means here. The pool as a whole was nowhere near full. The capacity that matters is each server's, against what the balancer actually sends it.

Even more traffic

At 800 requests a second, 62.5% of capacity, round robin and random sent s2 about 200 requests a second for its 80, so its line grew six times faster:

Policy p99 Share sent to s2
round robin 30.8 s 25.0%
random 32.4 s 26.0%
least connections 60 ms 8.2%
two choices 105 ms 9.5%

The same arithmetic: 120 more than s2 can serve, every second, for 20 s, is 2,400 waiting, 30 s of work. Least connections stayed at 60 ms; two choices rose to 105 ms. Compare all three rates, and the simulation's prediction, here:

Try it · Four policies, one slow server, measuredLab 07 · 02_algorithms.py · HAProxy, 2 rounds of 20 s

p99 latency · log scale

Round robin5.4 s
Random6.4 s
Least connections53 ms
Two choices60 ms
10 ms100 ms1 s10 s

Bar: median of the two rounds. Ticks: each round.

Share of requests sent to the slow server (s2)

Round robin
25.0% = 100/s of 80
Random
26.0% = 104/s of 80
Least connections
9.8% = 39/s of 80
Two choices
12.8% = 51/s of 80

Scale 0–30%. Green tick: 1%, the most a 50 ms server can get and stay out of p99. Red: more than s2 can serve.

At 31% of capacity, round robin and random send s2 a quarter of the traffic: about 100 requests a second to a server that serves 80. Its queue grows for all 20 s: p99 5.4 s and 6.4 s. Least connections sent it 9.8%, 39 a second, and stayed at 53 ms.

HAProxy 3.2 with 4 threads, two interleaved rounds per row, the first 2 s of each dropped as warm-up. No request failed. Random's two rounds at 400 req/s differ a lot (5.4 s and 7.3 s): when a queue grows for the whole run, p99 depends on how unlucky the run was.

The simulation, checked

Before the real test, the lab ran its simulation (lb_sim.py, written from scratch: four servers with four slots and their own lines, shoppers arriving at random, the four policies) with 5 different random seeds (a seed fixes the random arrivals, so a run can be repeated exactly) and took the median. Its predictions next to HAProxy:

Rate Policy p99, HAProxy p99, simulation s2's share, HAProxy s2's share, simulation
200 round robin 57 ms 54 ms 25.0% 25.0%
random 89 ms 85 ms 25.4% 24.4%
least connections 54 ms 50 ms 11.7% 11.7%
two choices 55 ms 50 ms 15.6% 12.5%
400 round robin 5,433 ms 5,039 ms 25.0% 25.0%
random 6,378 ms 5,777 ms 26.0% 25.4%
least connections 53 ms 50 ms 9.8% 9.8%
two choices 60 ms 50 ms 12.8% 10.8%
800 round robin 30,767 ms 29,324 ms 25.0% 25.0%
random 32,407 ms 29,429 ms 26.0% 25.0%
least connections 60 ms 53 ms 8.2% 8.2%
two choices 105 ms 64 ms 9.5% 8.8%

It got the order right, and s2's share usually within a point. It was a little optimistic everywhere: it has no HTTP, no proxy and no busy machine. Its biggest miss is two choices at 800 a second, 64 ms predicted against 105 measured, and HAProxy sent s2 a larger share under two choices at every rate. The lab didn't find out why. Its two candidates: HAProxy's 4 threads see each other's connection counts a little late, and the simulation's two random draws may not be made the way HAProxy makes them. (Rerunning the lab's simulation from its own code gives the same predictions to the decimal.)

Never below 50 ms

No policy got p99 below 50 ms. Look at s2's share again: even the best policies sent it 8–16% of requests, and each of those takes 50 ms.

p99 is the latency that only 1 request in 100 exceeds. If more than 1 request in 100 goes to the slow server, the slowest 1 in 100 are all its requests, so p99 is at least 50 ms. For p99 to escape, s2's share has to fall below 1%. Least connections sends it less; it would have to send it almost nothing. Stop sending it traffic, don't just send it less: take the slow server out of the pool (outlier ejection, which the lab leaves to a later lab), or fix it.

Uneven shoppers

A second test: four equal servers, but uneven requests. 95% take 5 ms, 5% take 100 ms: one in twenty is a big order. 800 requests a second, about half of what the four can do with this mix (the average request takes 9.75 ms, so 16 slots do about 1,641 a second). Two rounds of 15 s each. p99 in ms:

Policy All requests Quick (5 ms) requests only Big (100 ms) requests only
round robin 102 28 128
random 102 46 127
least connections 102 8 103
two choices 102 11 104

p99 over all requests was 102 ms for every policy. One request in twenty takes 100 ms, so everything from the 95th percentile up is a big request: the slowest one in a hundred is always one of those, whatever the balancer does. The lab's first version of this exercise reported only that number, and found no difference.

The quick requests alone show it. A till busy with one or two big orders has fewer free slots. Round robin still sends it its turn, and the quick shopper waits behind the big ones: 28 ms at p99 for a 5 ms request. Least connections sees the slots in use and goes elsewhere: 8 ms. So split latency, how long requests take, by request type before you compare. One p99 over a mix hides the slow side of the fast ones.

Try it · Uneven shoppers: split before you compareLab 07 · 02_algorithms.py Ex. 3 · 800 req/s, 2 rounds of 15 s

Four equal servers. 95% of requests take 5 ms, 5% take 100 ms. 800 requests a second, about half of capacity.

p99 latency, ms · all requests

Round robin
102
Random
102
Least connections
102
Two choices
102
050100

102 ms for every policy. One request in twenty takes 100 ms, so everything from the 95th percentile up is a big request: the slowest one in a hundred is always one of them, whatever the balancer does.

Medians of two rounds; ticks show each round for the quick requests. Whole milliseconds, as the lab reports them.

What the balancer spreads: buses or shoppers

One more thing. A connection here is a TCP connection, and HTTP clients usually keep one open and send many requests on it (keep-alive). A connection is like a bus: a client opens it, and many shoppers ride it.

Balancers come in two kinds, named after the network layer they read:

  • An L4 (layer 4, transport) balancer sees TCP connections, not the requests inside them. It picks a server once per connection: it points the whole bus at one till. HAProxy in mode tcp, AWS's Network Load Balancer, IPVS.
  • An L7 (layer 7, application) balancer reads every HTTP request, so it picks a server for each one: it points each shopper. HAProxy in mode http, nginx, Envoy, AWS's Application Load Balancer. It can also route by path or header and retry.

The lab sent 2,000 requests from one client through HAProxy round robin, both ways, varying the number of keep-alive connections:

Client connections L4: requests to s1–s4 L4 busiest ÷ average L7: requests to s1–s4
1 [0, 0, 0, 2,000] 4 [500, 500, 500, 500]
2 [998, 1,002, 0, 0] 2 [500, 500, 500, 500]
4 [499, 505, 498, 498] 1.01 [500, 500, 500, 500]
16 [499, 493, 493, 515] 1.03 [500, 500, 500, 500]
1, new for each request [500, 500, 500, 500] 1

"Busiest ÷ average" is the lab's imbalance measure: 1 is perfectly even, 4 means one of four servers did everything. Through L4, one connection put all 2,000 requests on one server; with two connections, two servers got everything. With four or more connections, it evened out. Through L7, the same single connection was spread 500 each.

Try it · What the balancer spreads: buses or shoppersLab 07 · 01_proxy_overhead.py Ex. 3 · 2,000 keep-alive requests

One client sends 2,000 requests over a few long-lived (keep-alive) connections, through HAProxy round robin. A connection is a bus: it carries many shoppers. L4 points each bus at a till; L7 points each shopper.

1 bus · 2,000 shoppers each
L4 (per connection)busiest ÷ average 4.00

HAProxy mode tcp: picks a server once per connection

s10
s20
s30
s42,000
L7 (per request)busiest ÷ average 1.00

HAProxy mode http: picks a server for each request

s1500
s2500
s3500
s4500

One bus, one till: all 2,000 requests went to one server through L4, while three sat idle. L7 opened the bus and spread the shoppers: 500 each.

Requests per server, recorded. Busiest ÷ average is the lab's imbalance measure: 1 is perfectly even, 4 means one of four servers did everything.

Apps that keep only a few connections open can hit this: an HTTP/2 or gRPC client (both send many requests over one connection), or a small connection pool. Behind an L4 balancer, the load is only as even as the connections, and a new server gets nothing until clients reconnect. The fixes are an L7 balancer that understands the protocol, balancing in the client, or capping how long a connection lives so clients reconnect.

Caveats

  • The slow server is slow on purpose. The lab's service holds a worker slot 5× longer on s2. A real slow server (a bad disk, a noisy neighbour, a garbage-collection pause) may be slow in bursts, which a count of open requests sees later.
  • An overloaded queue's p99 is set by the test, not the system. Round robin's 5.4 s and 30.8 s are what 20 s of growth gives; a longer test gives a bigger number, and no number is "the" p99 of a queue that never stops growing. The same is why random's two rounds at 400 a second differed so much: 5.4 s and 7.3 s.
  • Two rounds per row, on a busy laptop. Other jobs kept the load average (the number of processes wanting a core) around 4–8. The load generator's send lag, how late it sent requests against its schedule, stayed under 1.1 ms in every run but one: 7.7 ms in the second round of round robin at 800 a second, where p99 was 31 s anyway. (The lab's README says it stayed under 1.1 ms; the recorded run says otherwise for that one round.)
  • One balancer. Least connections works well here because one HAProxy sees every request in flight. With many balancers, each sees only its own; that's where two choices is meant to win. The lab didn't measure it.
  • HAProxy's plain random isn't random. It means random(2), two choices. The lab's first "random" run sent s2 only 12.3% instead of 25%, which made random look nearly as good as least connections, and the share column next to the simulation's caught it. Even random(1) then sent s2 17%: HAProxy's random draws from a hash ring with weight × 16 points per server, uneven at the default weight of 1. A weight of 100 on every server fixed it. Check a balancer's shares, not only its name.
  • The supermarket widget is a simulation. It is one run of the lab's lb_sim.py per setting (seed 0), not HAProxy. The tables and the other widgets are the recorded runs.
  • Not covered here: what a proxy costs (tens of microseconds per request in the lab), sticky sessions, and what happens when a server dies or is replaced. That last one is next.

The checklist

  1. Use a policy that looks at load: least connections, or two choices if you run many balancers that can't share counts.
  2. Compare the load each server gets with what that server can serve, not the pool's total with the pool's capacity. A pool at 31% can have one server at 125%.
  3. Watch each server's share. A slow server that still gets more than 1% of requests owns your p99; remove it, don't just send it less.
  4. Split latency by request type (endpoint, or quick and heavy) before comparing balancers, or anything else.
  5. Know what your balancer spreads: connections (L4) or requests (L7). With few long-lived connections, L4 is only as even as the connections.
  6. Check the setting does what its name says (HAProxy's random), by looking at the shares.
  7. Simulate first, then measure. A short simulation predicted the ranking here; where it missed, it showed where to look.

Practice: balance the tills

Write the lab's logic: the pool's capacity, how long a queue that grows for 20 s makes the last request wait (5 s and 30 s, against the 5.4 s and 30.8 s measured), the four policies' pick() inside a small simulation (a standard-library copy of the lab's lb_sim.py, already written for you), the 1% rule for p99, p99 by request type, and what L4 and L7 spread. The tests check the simulation's shapes against the lab's (a quarter to s2 under round robin, about 10% under least connections, p99 in seconds against tens of milliseconds) and the recorded L4 counts.

Try it · Practice in PythonRuns in your browser · ⌘/Ctrl + Enter to run
balance_practice.py127 lines

Quiz

Try it · Check your understanding0 of 6 answered
  1. 1.Four servers, one of them 5× slower, can serve 1,280 requests a second together. At 400 a second (31% of that), round robin's p99 was 5.4 s. Why?

  2. 2.How does least connections avoid the slow server's collapse?

  3. 3.Least connections sent the slow server 8–12% of requests and kept p99 at 53–60 ms. Why didn't p99 go down to the fast servers' 11–12 ms?

  4. 4.With 95% of requests taking 5 ms and 5% taking 100 ms, p99 over all requests was 102 ms for all four policies. What does that tell you?

  5. 5.One client sends 2,000 requests over a single keep-alive connection through an L4 (TCP) balancer in front of four servers. What happens?

  6. 6.You run 20 balancers, each seeing only its own traffic. Why might you prefer the power of two choices over least connections?

Check your understanding

Try each question before opening its answer.

The pool could serve 1,280 requests a second and got 400. Why did round robin's p99 reach 5.4 s?

Round robin gives every server every fourth request, whatever its speed. The slow server got 100 a second and could serve 80, so 20 more arrived than left, every second. Over the 20-second run its line grew to about 400, which is 5 s of work in front of the last arrival. The other three servers had room to spare the whole time.

How does least connections know a server is slow, when nobody tells it?

It counts the requests it has sent to each server that haven't come back. A slow server holds each one longer, so its count is higher, and it gets picked less often. At 400 requests a second it got 9.8% of them, about 39 a second, well under the 80 it can serve.

Least connections kept p99 at 53–60 ms. What would it take to get it down to the fast servers' 11–12 ms?

The slow server's share would have to fall below 1%. It still got 8–12% of requests under least connections, and each takes 50 ms, so the slowest 1 in 100 were all its requests. Sending it less isn't enough: take it out of the pool (outlier ejection) or fix it.

Why was random worse than round robin at light load (89 ms against 57)?

Both send the slow server a quarter of the requests, but round robin spaces them evenly, one every fourth request. Random sometimes sends several close together; with four slots, a fifth arriving within 50 ms waits for a slot. A slow server queues on bursts.

Every policy had the same p99, 102 ms, with uneven requests. Does the balancer not matter for that traffic?

It matters; the overall p99 just can't see it. 5% of requests take 100 ms, so everything from the 95th percentile up is one of those. The quick requests alone had a p99 of 28 ms under round robin and 8 ms under least connections, because round robin keeps sending quick requests to a server whose slots are held by big ones.

Your gRPC service has five servers behind an L4 balancer. One sits at 90% CPU and the others are idle. Why?

gRPC sends many requests over one long-lived connection, and an L4 balancer picks a server once per connection, so every request on it goes to the same server. In the lab, one connection put all 2,000 requests on one server. Fixes: an L7 balancer that reads HTTP/2, balancing in the client, or a maximum connection age so clients reconnect and spread out.

The simulation predicted 64 ms for two choices at 800 requests a second; HAProxy measured 105 ms. Was the simulation useless?

No. It got the ranking right and the slow server's share within a point or so, which is what the comparison needed, before any real test ran. Its misses point at what the real system has and the simulation doesn't: here, HAProxy sending the slow server a larger share under two choices at every rate. The lab didn't find out why.

Next up: Chapter 13, health checks and draining: this chapter's slow server stayed in the pool. The next one kills servers and replaces them under load, and measures how many requests fail before the balancer notices, how a health check can cause an outage, and how draining makes a deploy drop nothing.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.