Why you need it
A load balancer happily keeps sending visitors to a server that has crashed, and every one of them gets an error. Something has to notice that a server is dead, stop using it, and notice again when it comes back.
Think of it like this
A teacher taking the register every morning. Whoever does not answer gets no work handed to them that day. When they answer again, they are back in.
Where you’ll find it
- AWS: Elastic Load Balancing calls a page on each server every few seconds and stops sending traffic after a few misses.
- Kubernetes: Uses liveness and readiness probes to restart stuck programs and hold back ones not ready yet.
- NGINX and HAProxy: Both can check servers on their own: NGINX and HAProxy.
What to know first
Health check. A small question the balancer sends to each server, often a request to a page like /health. A quick, correct answer means "alive".
Heartbeat. The same idea turned around: each server sends "I am alive" every few seconds, and silence means trouble.
How it works
- Ask on a timer. Every few seconds the balancer calls each server's health page. This is the question in the animation.
- Do not judge on one miss. Networks drop the odd message. So a server is taken out only after several misses in a row (say 3), and let back in only after several good answers (say 2).
- Stop sending, keep asking. A server marked dead gets no visitors, but the checks continue. When it answers again it rejoins by itself, with nobody woken up.
- Check the right depth. A shallow check asks "is the program running?". A deep check also tests the database. Deep checks catch more, but if the shared database blips, every server fails at once and the balancer removes them all. Many balancers then "fail open" and use all servers anyway, because none at all is worse.
- Also watch real traffic. Some balancers notice errors in real answers too (a passive check), so a broken server is caught between timed checks.
An example
A balancer checks every 5 seconds and takes a server out after 3 misses.
At 10:00:00 one server freezes. Checks fail at 10:00:05, 10:00:10 and 10:00:15, and it is out. About 15 seconds of some visitors seeing errors, instead of hours.
At 10:04 it is restarted. Two good answers later, at about 10:04:10, it gets visitors again.
Further reading
- Implementing health checks, AWS Builders' Library. Amazon's own lessons, including how a too-clever check took systems down. Start with this one.
- Configure liveness, readiness and startup probes, Kubernetes docs. Three kinds of check, and when to use each.
- HeartBeat, Unmesh Joshi, on martinfowler.com. The heartbeat pattern, step by step.