ealeng

Episode 13 of 55 · Load balancing

Health checks and draining

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 healthy servers out 108 times in 40 seconds and turned away 1,644 requests. And restarting every server gently failed more requests than killing them. How a load balancer notices a dead server, how a check can cause an outage, and why a deploy should drain before it stops.

Lab 07 · Load Balancing and Proxies16 min read
Video transcript10:02

0:00Cold open

0:01

One of my four servers froze. My load balancer kept sending it requests for forty-five seconds.

0:09

A thousand one hundred sixteen of them failed.

0:13

Then I restarted every server, one at a time, gently, letting each one finish its work first. That failed more requests than killing them outright.

0:25

And a health check, meant to protect me, closed healthy servers a hundred and eight times. One thousand six hundred forty-four requests were turned away.

0:35

So how do you take servers out, and bring them back, without failing anyone?

0:42Intro

0:44

Hands-on system design, part thirteen.

0:47

Today: health checks, deploys, and the requests caught in between.

0:53Meet Lucía

0:54

I’m Lucía, a platform engineer.

0:57

Four servers, behind a load balancer called HAProxy. Every request goes to one of the four.

1:05

Think of a restaurant. The load balancer is the host. Each server is a table, with its waiter. Each request is a guest.

1:14

To know which tables are working, the host checks each one on a schedule. That’s a health check.

1:21

Three failed checks in a row, and the table is closed. Two passed checks, and it opens again.

1:2901 · What you need

1:30

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

1:38

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

1:43

No cloud, and no API keys.

1:4802 · A server freezes

1:49

First test. I froze one server, s2, for a minute. It doesn’t crash. It just stops answering.

1:57

I ran three separate hosts, side by side, in front of the same four servers. One checks every second, one every five seconds, one every fifteen.

2:08

Each gets a hundred requests a second. About twenty-five of those go to the frozen server.

2:15

How many are sent to the frozen table before the host notices?

2:2303 · How long until it notices

2:24

Checking every second: it noticed after six seconds. A hundred seventy-two requests failed.

2:32

Every five seconds: thirteen seconds. Three hundred forty-seven failed.

2:38

Every fifteen seconds: forty-five seconds. A thousand one hundred sixteen failed.

2:46

For those forty-five seconds, about one request in four just failed.

2:5304 · Why forty-five seconds

2:54

It checks every fifteen seconds. So why forty-five?

2:59

Each check waits up to one second for an answer. That limit is its timeout. Then the next check is fifteen seconds later.

3:08

So one failed check every sixteen seconds. Each check and wait is one cycle. And it takes three failures in a row.

3:17

The first failure can come almost at once, or a full cycle later. So three failures take two to three cycles: thirty-two to forty-eight seconds.

3:28

It took forty-five.

3:31

And the failures: twenty-five a second, times forty-five seconds, is about eleven hundred. Measured: eleven hundred sixteen.

3:41

Failed requests are the server’s share, times the time to notice.

3:4705 · Watching real guests

3:49

A fourth copy also checks every fifteen seconds. But it also notices guests coming back unserved: five in a row, and it closes the table.

4:00

This host gives up on a guest’s order after two seconds. So it noticed after two point three seconds. Fifty-three failed. Twenty-one times fewer.

4:11

But a closed table gets no guests, so there’s nothing to watch. Only passed checks can reopen it. So after the server recovered, it waited about twenty seconds for two good ones.

4:2406 · Shallow or deep

4:26

There are two ways to check a server. Here’s the code.

4:30

The shallow check answers at once: I’m here.

4:34

The deep check first waits for the waiter to be free, in the same line as real guests.

4:41

So it asks a harder question: could you serve a guest right now?

4:4707 · The check that caused an outage

4:49

Second test. All four servers healthy, eighty-five percent busy: seven hundred requests a second, of uneven work.

4:58

This check has a stricter rule. Eager means quick to close: it runs every half second, waits only a tenth of a second, and one slow answer closes the table. Lenient means patient.

5:12

Every server was healthy. What do you think the eager deep check did?

5:19

It closed a table a hundred and eight times in forty seconds. Tables were closed forty-three percent of the time.

5:28

And one thousand six hundred forty-four guests were turned away, with no open table left.

5:35

The lenient deep check, and the eager shallow check: zero.

5:4108 · How it spread

5:42

Here’s what most likely happened. At eighty-five percent busy, some requests wait in line a long time. In the lenient run, one request in a hundred took about two hundred forty milliseconds, mostly waiting in line. That’s more than twice the eager check’s limit of a hundred.

6:01

The deep check waits in that same line. So it misses its tenth of a second, and the table closes.

6:08

Its guests move to the other three. Together, all four can serve about eight hundred twenty a second. Three can serve about six hundred fifteen: less than the seven hundred arriving. That’s arithmetic, not a measurement.

6:23

So their lines grow too, and their checks likely start failing as well.

6:3009 · A rolling deploy

6:31

Last test: a deploy, simulated by restarting each server, one at a time, while three hundred requests a second arrive. That’s a rolling deploy.

6:43

In the restaurant: each table’s waiter steps away for a moment and comes back, one table at a time. For this test, the host checks every second, and two failures close a table.

6:55

First way: kill the server, stopping it instantly. Then start it again. I ran each way twice: a hundred and nine failed, then a hundred and five.

7:06

Second way: a graceful stop. The server finishes what it has first. Fewer failures, right?

7:15

A hundred forty-eight. And a hundred thirty-seven. More than the kill.

7:2210 · Why gentle failed more

7:23

Each server was gone for less than a second. Too short for two failed checks a second apart. So the host most likely never closed the table, and kept seating guests there. We didn’t log it.

7:37

Every failed request got the same answer from the host: a 503, couldn’t serve you.

7:44

And the graceful stop took longer to stop: about four tenths of a second, against a quarter for a kill. Every moment a server is away, guests still get seated at its empty table. So longer away likely means more failures.

7:59

A graceful stop is meant to protect the guests already seated. But it doesn’t tell the host.

8:0711 · Retry, or drain

8:09

Third way: a graceful stop, and the host retries a refused guest at another table. Zero failed.

8:17

But like the kill and the graceful stop, the slowest one in a hundred took a full second. We didn’t find out which timeout causes it.

8:26

Fourth way: drain. Tell the host first: stop seating at this table. Wait until it’s empty. Then let the waiter step away.

8:36

Each table was empty within six hundredths of a second. Zero failed. And only draining avoided the slow second: the slowest one in a hundred took fourteen milliseconds.

8:49

The whole deploy took seven point four seconds, against six point six for a kill.

8:5612 · Draining, in code

8:58

Here’s the drain, in the test’s code.

9:01

Tell HAProxy: this server is draining. No new guests.

9:06

Wait until it has nobody left.

9:10

Only then, stop it. Here with a graceful stop. With nobody seated, even a kill should be safe, but we didn’t test that.

9:20Back to Lucía

9:21

So, three lessons.

9:24

Failed requests are the server’s share, times the time to notice. Check often, and watch real requests too.

9:33

Don’t let a health check wait in line with real work. Make it shallow, or lenient.

9:39

And tell the load balancer before you stop a server. Drain first.

9:45

Coming up in the series: not doing the work at all. Caching.

9:50

For today: tell the host first.Lucía: Tell the host first.

9:54Summary

9:55

Notice fast, keep checks out of the line, and drain before you stop.

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.

Try it · A table freezes: how long until the host notices?Lab 07 · 03_health_checks.py · s2 frozen for 60 s
Hostload balancer100 guests/ss125 guests/ss2frozen: 25/s fails325 guests/ss425 guests/s

Seconds after s2 froze · bars are recorded; check marks are placed from the formula

0 s10 s20 s30 s40 s50 s60 s70 s80 sTable s2frozen: takes guests, never answersCheckspredicted 32–48 s✕✕✕✓✓Host's signclosedDOWN 45.2 sUP +15.4 sFailed1,116 requests
Frozen Failed check (waits its 1 s timeout) / guests failing Open, or a passed check Closed: no guests sent
Failed requests
1,116
Marked down after
45.2 s
Predicted
32–48 s
Up after the thaw
15.4 s

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

0
130
122
128
119
125
129
122
121
118
2
0
0
0
−5 sfreeze at 0 · about 25 a second fail until DOWN at 45 s65 s

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.

Try it · A health check that waits in lineLab 07 · 03_health_checks.py · 700 req/s, all servers healthy

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

table4 waiters (slots)ggggggcheckwaits behind real guests

How long the check may wait vs. how long the line gets

0 ms250 ms500 ms750 ms1000 ms1 in 100 real requests took 239 ms (lenient run)the check gives up at 100 ms
Server time closed
43%
Times closed
108
Turned away (503)
1,644
p99 of the rest
929 ms

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 tables can serve about 820 req/sarriving: 700 req/s

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.

Try it · Restarting four servers under loadLab 07 · 04_deploys.py · 300 req/s · check every 1 s, 2 failures close

docker stop (SIGTERM: finish what it has, then exit), then start

The deploy, from the script's log · seconds

0 s1 s2 s3 s4 s5 s6 s7 sdone 7.3 ss1away 0.90 ss2away 0.81 ss3away 0.77 ss4away 0.84 s
Away, but the host still sends it its share stop command returned
Failed
148
Requests
3,056
p99
1,013 ms
Deploy took
7.3 s

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

  1. 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.
  2. Check often, and watch real requests too. A passive check notices in seconds; keep active checks to bring servers back.
  3. 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.
  4. 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.
  5. Tell the load balancer before you stop a server. Drain, wait until nothing is in flight (with a deadline), then stop.
  6. 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.
  7. 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.

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

Quiz

Try it · Check your understanding0 of 6 answered
  1. 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. 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. 3.What can a passive check (watching real requests fail) do that a scheduled check can't, and what can't it do?

  4. 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. 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. 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.

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.