Key takeaways
- ✓Until the load balancer notices a dead server, every request it sends there fails. Failed requests ≈ the server's share of the traffic × the time to notice: 25 requests a second × 45.2 s ≈ 1,130, against 1,116 measured with a check every 15 s.
- ✓A frozen server accepts the connection and never answers, so each failed check waits out its timeout before the next one starts. Three failures in a row take two to three cycles of (interval + timeout): 32–48 s for a check every 15 s with a 1 s timeout. Checking every second noticed in 6.0 s, with 172 failures.
- ✓A passive check watches real requests. Closing after 5 failed requests in a row noticed the frozen server in 2.3 s, with 53 failures, 21 times fewer than the 15 s check alone. It can't bring a server back: only passed active checks reopen it (19.9 s here).
- ✓A deep check that waits in the same line as real requests measures queueing, not health. At 85% load with uneven work, an eager deep check (every 0.5 s, 100 ms limit, one failure closes) took healthy servers out 108 times in 40 s: closed 43% of the time, 1,644 requests turned away. The same deep check with a 1 s limit and 3 failures, or an eager shallow check that answers at once: 0.
- ✓Closing one server moves its traffic to the rest. Four servers here could serve about 820 requests a second; three, about 615, less than the 700 arriving. So their lines grow and their checks start failing too.
- ✓Restarting each server took under a second, too short for a check every 1 s that needs 2 failures. So the load balancer kept sending requests to a server that was gone: 109 and 105 failed with a kill, 148 and 137 with a graceful stop, which took longer to exit.
- ✓Draining tells the load balancer first: no new requests to this server, wait until it has none in flight, then stop it. 0 failures and a p99 of 14 ms, for 0.8 s more deploy time. Retrying on another server also gave 0 failures, but at least 1 request in 100 still took about a second.
A load balancer sits in front of a group of servers and sends each request to one of them. This chapter is about the moments when one of those servers isn't there. One of four servers froze, and the load balancer kept sending it requests for 45 seconds: 1,116 failed. A health check meant to protect the service took perfectly healthy servers out 108 times in 40 seconds, and 1,644 requests were turned away. And restarting every server gently, letting each one finish its work first, failed more requests than killing them. The lab measures each one, and the fixes: check often, watch real requests, keep checks out of the line, and drain before you stop.
The examples come from Lab 07 (Load Balancing and Proxies) of the system design labs, scripts 03_health_checks.py (a frozen server, and health checks under heavy load) and 04_deploys.py (restarting every server, four ways). The same lab's balancing-algorithm script and its connection test (02_algorithms.py and Exercise 3 of 01_proxy_overhead.py) are Chapter 12; what a proxy costs per request (01_proxy_overhead.py, Exercises 1–2) and sticky sessions (05_sessions.py) aren't covered in this track. The setup is the earlier chapters' laptop (an Apple M4 Max, Docker Desktop with 16 virtual CPUs): four copies of a small Python web service, called s1 to s4, each with 4 worker slots (it works on at most 4 requests at a time; the rest wait in line), behind HAProxy 3.2, a widely used open-source load balancer, all in Docker. The lab's 407 unit tests (its config generator, metrics and simulation) run in about 2 seconds without Docker; they all pass. Other work kept the laptop busy (a load average of 4–7), so read the shapes, not the last digit.
The host and the tables
Think of a restaurant. The load balancer is the host at the door. Each server is a table with its waiter. Each request is a guest. The host seats each guest at a table in turn.
To know which tables are working, the host checks each one on a schedule. That's a health check: the load balancer asks each server a question (here an HTTP request to /health) and expects an answer in time. HAProxy's settings for it, which the rest of the chapter uses:
- Interval (HAProxy calls it
inter): how often it checks each server. - Timeout: how long one check waits for an answer before it counts as failed.
- Fall: how many failed checks in a row mark the server DOWN (closed: no more guests).
- Rise: how many passed checks in a row mark it UP again.
When a request can't be served, HAProxy answers with an error in the 5xx range of HTTP status codes, which mean "the server side failed". Here they're 503 (Service Unavailable) and 504 (Gateway Timeout).
A server freezes
The first test froze s2 for 60 seconds with docker pause, which stops every process in the container without closing anything. It doesn't crash. It just stops answering.
Three separate HAProxy setups (three hosts) ran side by side in front of the same four servers, checking every 1, 5 and 15 seconds. Each had a 1 s check timeout, fall 3 and rise 2, and gave up on a real request after 2 s. Each was sent 100 requests a second of 10 ms work, so with round robin (each server in turn) about 25 a second went to the frozen server. A fourth host also watched real requests; it comes up below.
| Health check | Failed requests | Marked DOWN after | Predicted | UP again after the server recovered |
|---|---|---|---|---|
| every 1 s | 172 | 6.0 s | 4–6 s | 2.0 s |
| every 5 s | 347 | 13.4 s | 12–18 s | 5.5 s |
| every 15 s | 1,116 | 45.2 s | 32–48 s | 15.4 s |
| every 15 s + watching real requests | 53 | 2.3 s | 19.9 s |
With the 15 s check, the frozen server failed about one request in four for 45 seconds: 118 to 130 failures in every 5 s, then 2, then none.
Seconds after s2 froze · bars are recorded; check marks are placed from the formula
25 req/s × 45.2 s = 1,130 expected · 1,116 measured
A check every 15 s, each waiting up to 1 s: one failed check every 16 s (a cycle). 3 failures in a row take 2 to 3 cycles, 32–48 s, depending on where in its cycle the freeze landed. Measured: 45.2 s. Coming back took 2 passed checks: 15.4 s after the thaw (predicted 15–30 s).
Failed requests per 5 s, 15 s check · recorded
Each host was its own HAProxy listener, sent 100 requests a second of 10 ms work, in front of the same four servers; all used fall 3, rise 2 and a 1 s check timeout. Counts and times are recorded. The check marks are drawn: the lab didn't log each check, so they're placed backwards from the recorded DOWN and UP times, one cycle apart, which is what the lab's formula says. The table picture is the analogy, not a measurement.
Why 45 seconds
The check runs every 15 seconds and closes after 3 failures. So why 45, and not 30?
A frozen server's operating system still accepts the connection, so the check doesn't fail at once: it waits out its full 1 s timeout. HAProxy starts the next check 15 s after that. So there's one failed check every 16 s, a cycle of interval + timeout. The first failure can come almost at once after the freeze or a full cycle later, depending on where the schedule was. Then two more are needed:
cycle: 15 s interval + 1 s timeout = 16 s
DOWN: 2 to 3 cycles = 32–48 s measured: 45.2 s
failed: 25 requests a second × 45.2 s ≈ 1,130 measured: 1,116
The lab's code has this as Check.detection_range_s(): (fall − 1) to fall cycles. Its first version said at most fall × interval + timeout, 4 s for the 1 s check, and HAProxy took 6.0 s; the measurement is what corrected it. All three measured times fall inside the corrected ranges.
The second line is the rule to remember: failed requests ≈ the server's share × the time to notice. To fail fewer, notice sooner, or send the server a smaller share. It's an estimate: for the 1 s check, 25 × 6.0 = 150 against 172 measured, and the lab doesn't break down the difference.
Coming back works the same way in reverse: 2 passed checks, an interval apart. That took 2.0, 5.5 and 15.4 s after the server recovered, inside the predicted 1–2, 5–10 and 15–30 s.
Watching real guests
The fourth host also checked every 15 s, but it also counted guests coming back unserved. HAProxy calls this passive checking (here observe layer7 error-limit 5): after 5 failed real requests in a row, close the table. A scheduled check is active.
This host gave up on a real request after 2 s. So the first failures came back about 2 s after the freeze, and at 25 a second, five more took a fifth of a second: DOWN after 2.3 s, 53 failed, 21 times fewer than the same 15 s check alone.
But a closed table gets no guests, so there's nothing left to watch. Only passed active checks can reopen it, so after the server recovered, it waited 19.9 s for two good ones 15 s apart. Passive checks are for noticing fast; active checks are still needed to come back, and to notice a server that gets no traffic.
Shallow or deep
There are two ways to answer a health check. The lab's service has both, in service/app.py (shortened):
@app.get("/health")
async def health(deep: int = 0):
if deep:
# goes through the same 4 worker slots as real requests
await hold_slot(0.001) # wait for a free slot, hold it for 1 ms
return {"ok": True}
- A shallow check answers at once: "I'm here."
- A deep check first waits for a free worker slot, in the same line as real requests, then answers. It asks a harder question: could you serve a guest right now?
The deep check catches more problems (a server whose workers are all stuck, say). It also has a catch, which the next test found.
The check that caused an outage
All four servers were healthy and 85% busy: 700 requests a second of uneven work, 70% taking 5 ms, 20% taking 30 ms and 10% taking 100 ms, for 40 s each run. Three checks:
- Eager deep: quick to close. Every 0.5 s, a 100 ms timeout, and one failure (fall 1) closes the server; one pass (rise 1) reopens it.
- Lenient deep: patient. Every 2 s, a 1 s timeout, fall 3, rise 2.
- Eager shallow: the eager settings, with the shallow check.
| Health check | Server time closed | Times closed | Requests turned away (503) | p99 of the rest |
|---|---|---|---|---|
| eager, deep | 43% | 108 | 1,644 | 929 ms |
| lenient, deep | 0% | 0 | 0 | 239 ms |
| eager, shallow | 0% | 0 | 0 | 263 ms |
Every server was healthy, and the eager deep check closed them 108 times in 40 seconds. Servers were closed 43% of the time, and 1,644 requests were turned away with a 503 because no server was left open. The p99 is the 99th percentile: 99 requests in 100 finished within that time.
Here's what most likely happened; the lab measured the outcome, not each check. At 85% busy, some requests wait in line a long time. In the lenient run, where no server ever closed, 1 request in 100 took about 240 ms, mostly waiting (the longest work is 100 ms). That's more than twice the eager check's 100 ms. The deep check waits in that same line, so it sometimes misses its limit, and one miss closes the server.
Then its traffic moves to the other three. This part is arithmetic, not a measurement:
mean work: 70% × 5 + 20% × 30 + 10% × 100 = 19.5 ms
four servers: 16 slots ÷ 19.5 ms ≈ 820 requests a second (700 is 85%)
three servers: 12 slots ÷ 19.5 ms ≈ 615 requests a second (less than 700)
Three servers can't keep up with 700 a second. Their lines grow, and their deep checks likely start failing too. That's how one slow answer becomes a partial outage.
The lenient deep check tolerated a slow answer, and the eager shallow check never waited in line, so neither ever closed a healthy server.
deep: takes a worker slot for 1 ms · every 500 ms · waits up to 100 ms · closed after 1 failed · reopened after 1 passed
Where the check stands at one busy table · drawn
How long the check may wait vs. how long the line gets
Every server was healthy. The check waits in the same line as real requests, and at 85% busy the line sometimes holds it past its 100 ms limit. One slow answer closes the table: 108 closures in 40 s, 1,644 of 28,150 requests (5.8%) turned away when no table was left open.
If tables close · arithmetic, not a measurement
4 × 4 slots ÷ 19.5 ms mean work ≈ 820 req/s · 700 ÷ 820 = 85% busy.
Recorded: three 40 s runs of 700 req/s (70% take 5 ms, 20% 30 ms, 10% 100 ms) on four servers with 4 worker slots each; "p99 of the rest" is over the requests that succeeded. The lab didn't record how long each check waited; the 239 ms is the slowest 1% of real requests in the lenient run, where no table closed, and it includes up to 100 ms of their own work. The line picture is drawn, and the capacity bar is the lab's arithmetic, not a measurement.
The lab's first attempt didn't find this. With every request taking a fixed 20 ms at 90% load, the line never got long enough to fail the check. Uneven work, which real servers have, is what makes the long waits.
A rolling deploy
A deploy replaces the running servers with a new version. A rolling deploy does it one server at a time, so the others keep serving. The lab simulates one by restarting each server in turn (the same container, not a new version), while 300 requests a second of 10 ms work arrive. For this test the check runs every 1 s, and 2 failures close a server. In the restaurant: each table's waiter steps away for a moment and comes back, one table at a time.
Four ways, each run twice, interleaved:
- Kill:
docker kill, which sends SIGKILL, a signal that stops the process instantly. Then start it again. - Graceful stop:
docker stop, which sends SIGTERM, a signal that asks the process to finish what it has and exit. The server stops accepting new connections and finishes the requests it's working on. Then start it. - Graceful stop + retries: the same, with HAProxy retrying a refused connection on another server (
retry-on conn-failure,option redispatch). - Drain, then stop: tell HAProxy first, wait, then stop. Below.
| Deploy | Failed (round 1 / round 2) | Requests during the deploy | p99 | Deploy took |
|---|---|---|---|---|
| kill | 109 / 105 | 2,872 / 2,854 | 1,013 ms | 6.6 s |
| graceful stop | 148 / 137 | 3,056 / 3,042 | 1,013 ms | 7.2 s |
| graceful stop + retries | 0 / 0 | 3,030 / 3,056 | 1,013 ms | 7.2 s |
| drain, then stop | 0 / 0 | 3,065 / 3,158 | 14 ms | 7.4 s |
The p99 and deploy time are the medians of the two rounds. Every failure was a 503 from HAProxy: it couldn't connect to the server.
docker stop (SIGTERM: finish what it has, then exit), then start
The deploy, from the script's log · seconds
More failures than the kill (109 and 105). The server stops taking new connections at once but takes longer to exit: 0.39–0.51 s here against about a quarter of a second for a kill. Longer away, more refused requests, and still nobody told the host.
Bars are the script's recorded step times, in seconds from the start of the deploy: "away" runs from the stop command to the server answering again after it was started; the script sleeps 1 s between servers. Each deploy ran twice, interleaved with the other ways. The failed count and p99 cover requests sent from the start of the deploy until 3 s after it ended. No DOWN was logged, so "never closed" is the likely reading, not an observation.
Why gentle failed more
Each server was away for 0.54–0.93 s, from the stop command until it answered again. Two failed checks a second apart need it gone for more than a second. So the load balancer most likely never closed the table, and kept sending each restarting server its quarter of the traffic. The lab didn't log a DOWN, which fits that reading.
The graceful stop took longer to stop: 0.38–0.51 s, against 0.20–0.27 s for a kill. Every moment a server is away, requests still get sent to it and refused. So longer away means more failures. A graceful stop is meant to protect the requests already being served (the lab didn't measure that part, since every failure was a refused connection). It doesn't tell the load balancer.
Retry, or drain
Retries hid every failure: a refused connection went to another server instead. But like the kill and the graceful stop, at least 1 request in 100 took about a full second instead of 10 ms. HAProxy's connect timeout and its wait before a retry are both 1 s in this setup, and the lab didn't separate which one causes it. Retries also only suit requests that are safe to send twice (Chapter 10).
Draining tells the host first: stop seating guests at this table, wait until it's empty, then let the waiter step away. In 04_deploys.py (shortened), through HAProxy's runtime API, a control channel for changing a running HAProxy:
set_state(listener, server, "drain") # no new requests to this server
while server_states(listener)[server]["scur"] > 0: # requests still in flight
time.sleep(0.05)
docker("stop", server) # only then stop it
docker("start", server)
wait_status(listener, server, "DRAIN") # healthy again
set_state(listener, server, "ready") # back in rotation
Each server had nothing in flight within 0.06 s. 0 failed, and no slow second: p99 14 ms. The deploy took 7.4 s against 6.6 s for a kill. Here the drain stopped each server with a graceful stop; with nobody left, even a kill should be safe, but the lab didn't test that.
The wait was short because these requests take 10 ms. With long requests (uploads, streams, WebSockets), the drain is what takes the time, and needs a deadline.
Caveats
- One laptop, one run per health-check row, two rounds per deploy. The README reports an earlier run of the frozen-server test within a few requests (170, 348, 1,117 and 53 failures); that earlier run isn't in the saved results.
- The chain from one slow check to an outage is the likely mechanism, not a trace. The lab counted closures, closed time and 503s; it didn't record how long each check waited. The 820 and 615 a second are arithmetic.
- "Never closed" during the deploy is an inference. No DOWN was logged, and the away times were shorter than two check intervals.
- The check marks in the first widget are placed, not logged. They're drawn backwards from the recorded DOWN and UP times, one cycle apart, as the lab's formula says.
- The one-second delay has two candidate causes (HAProxy's connect timeout and its retry wait, both 1 s here) that the lab didn't separate.
- "Share × time" is an estimate. It matched the 15 s check within 1%, but the 1 s check failed 172 against 150 estimated.
The checklist
- Know your time to notice. For a server that hangs, it's (fall − 1) to fall cycles of interval + timeout, and failed requests ≈ its share × that time.
- Check often, and watch real requests too. A passive check notices in seconds; keep active checks to bring servers back.
- Keep the check that removes a server out of the line. Make it shallow, or make it lenient: a timeout well above the slow end of normal waits, and more than one failure.
- Remember what closing a server does to the others. Check that the servers left can carry the load; if they can't, closing one makes things worse.
- Tell the load balancer before you stop a server. Drain, wait until nothing is in flight (with a deadline), then stop.
- Don't count on the health check to notice a deploy. A restart can be shorter than the time to notice, and the requests sent in between fail.
- Use retries as a backstop, not the plan. They hide refused connections, but not the delay, and only for requests that are safe to repeat.
Practice: health checks and draining
Write the lab's arithmetic: the detection and recovery ranges (checked against the lab's predicted 4–6, 12–18 and 32–48 s), share × time, a passive check's rough time to notice, how much traffic three or four servers can serve, and whether a restart is long enough for the check to notice. The tests use the recorded runs: 45.2 s and 1,116 failures, 2.3 s for the passive check, 85% busy, and the deploy log's away times.
Quiz
1.A load balancer sends 100 requests a second, round robin, to four servers. One freezes, and the health check takes 45 s to notice. About how many requests fail?
2.A check runs every 15 s, waits up to 1 s for an answer, and closes a server after 3 failures in a row. Why does the lab predict 32–48 s to notice a frozen server?
3.What can a passive check (watching real requests fail) do that a scheduled check can't, and what can't it do?
4.All four servers are healthy at 85% load. A deep health check takes a worker slot, waits at most 100 ms, and one failure closes the server. What happened in the lab?
5.During a rolling deploy, each server was gone for under a second. Why did a graceful stop fail more requests than a kill?
6.Retries on another server and draining both gave 0 failed requests. Why drain?
Check your understanding
Try each question before opening its answer.
A server freezes and the check runs every 15 s with a 1 s timeout and 3 failures to close it. Why can it take up to 48 s, and how many requests fail?
A frozen server accepts the connection and never answers, so each failed check waits its 1 s timeout before the next one starts 15 s later: one failure every 16 s. The first can come almost at once or a full cycle later, so 3 take 32–48 s; the lab measured 45.2 s. Failed requests are the server's share times that time: 25 a second × 45 s ≈ 1,125, against 1,116 measured.
How do you make that number smaller?
Notice sooner. Checking every second noticed in 6.0 s, with 172 failures. Adding a passive check that closes the server after 5 failed real requests in a row noticed in 2.3 s, with 53 failures. Active checks are still needed to bring the server back: the passive host waited 19.9 s for two passed checks.
How can a health check take healthy servers out?
A deep check that waits for a worker slot measures the line as well as health. At 85% busy with uneven work, the line is sometimes longer than an eager 100 ms limit, and with one failure enough to close, a healthy server is closed. Its traffic moves to the others: three servers can serve about 615 a second, less than the 700 arriving, so their lines grow too. In the lab: 108 closures in 40 s and 1,644 requests turned away.
Kubernetes has liveness probes (restart the container if it fails) and readiness probes (stop sending it traffic if it fails). Which would you make deep, and what goes wrong with a deep liveness probe?
Keep liveness shallow: it should only ask "is the process stuck?". A deep or eager liveness probe fails on a busy container and restarts it, which makes the overload worse. Readiness can be deeper, but lenient, for the reasons above. And a check that queries a shared database fails on every server at once when the database is slow, removing them all.
Why did a rolling deploy fail requests even though each server was back in under a second?
Because the load balancer was never told. Each server was away for 0.54–0.93 s, too short for two failed checks a second apart, so it most likely stayed marked UP and kept getting a quarter of the traffic. Those connections were refused and HAProxy answered 503: 105 to 148 failed requests per deploy.
Why did the graceful stop fail more than the kill?
It stops accepting new connections at once, like a kill, but takes longer to exit: about 0.4 s against 0.25 s. The load balancer keeps sending requests the whole time, so the server is refusing them for longer. A graceful stop is meant to protect requests already being served; it doesn't stop new ones from being sent.
Kubernetes removes a pod from the list of servers and sends it SIGTERM at about the same time. Why does a few seconds' sleep before shutting down (a preStop hook) help, and what does it cost?
The load balancers learn about the removal a little later, so requests keep arriving after the shutdown starts, which is the graceful-stop failure above. Sleeping a few seconds first gives them time to stop sending, a crude drain. It costs those seconds on every pod in every deploy.
Next up: Chapter 14, caching and cache stampedes. Not doing the work at all: what a Redis cache buys, and how it breaks when many cached items expire at once.