Health Checks
Removing sick servers.
A server can crash, freeze, or lose its database connection. The load balancer needs to know, quickly, so it stops sending users there. It finds out with health checks.

- Servers crash or freeze
- The balancer must know
- Health checks
A simple health checker
Every few seconds, the balancer calls a health endpoint on each server, often slash health. If a server fails several checks in a row, it's removed. When it passes again, it's added back. Here we simulate five rounds of checks. Run it. Server B fails twice in a row and is removed, then recovers and returns.
results = { # True = healthy answer
"A": [True, True, True, True, True],
"B": [True, False, False, True, True],
}
fails = {"A": 0, "B": 0}
for rnd in range(5):
live = []
for name, checks in results.items():
fails[name] = 0 if checks[rnd] else fails[name] + 1
if fails[name] < 2:
live.append(name)
print(f"round {rnd + 1}: serving from {live}")round 1: serving from ['A', 'B'] round 2: serving from ['A', 'B'] round 3: serving from ['A'] round 4: serving from ['A', 'B'] round 5: serving from ['A', 'B']
Good health checks
A good health check is cheap, so it can run often. It checks what really matters, like whether the server can reach its database. It needs a threshold, so one slow answer doesn't remove a server. And it should be separate from the main traffic, so a busy server isn't marked dead by mistake.
| key | value |
|---|---|
| Cheap | runs every few seconds |
| Meaningful | can it reach the DB? |
| Threshold | 2–3 fails before removing |
| Recovery | add it back automatically |
Summary
Health checks let the balancer remove failing servers automatically, and add them back when they recover. Next, why servers should keep no memory of users.
Key takeaways- Check every few seconds
- Remove after repeated fails
- Add back when healthy