AI Hints
Home›System Design Tutorial›Healthy, Stateless Servers
System Design · Lesson 28 of 120

Health Checks

Removing sick servers.

⏱ 2 min read▶ Video at 31:57🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

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.

Health Checks
From the video: 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.

health.pypython
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}")
Output
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.

keyvalue
Cheapruns every few seconds
Meaningfulcan it reach the DB?
Threshold2–3 fails before removing
Recoveryadd 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
Next up: Stateless Servers — any server can answer