Sub topic 6
Detecting failure with health checks and heartbeats
This chapter assumes you know what a load balancer is, what a timeout is, and the basic difference between a process being up and a service being useful. It covers how a system learns that a component has failed, which is the first step of any failover.
Why detection comes first
Failover cannot start until something notices a failure. Detection is also where most tuning mistakes happen. A slow detector extends the outage. A twitchy detector triggers failovers that nobody needed, and each failover carries its own risk.
A detector can only observe behavior from the outside. It never sees "the server is dead." It sees "no answer arrived in time," and it has to guess what that means.
Two detection styles
- Health checks (pull): A monitor, such as a load balancer, sends a probe to the component on a schedule and judges the reply.
- Heartbeats (push): The component sends a small "I'm alive" message to its peers or a coordinator. Silence for too long means trouble.
Health checks suit a central watcher that checks many backends. Heartbeats suit clusters where members watch each other, such as database replicas.
Shallow and deep checks
A shallow check confirms that the process answers, for example a TCP connect or an HTTP 200 from a static route. A deep check exercises dependencies, such as running a trivial database query.
| Check type | Catches | Risk |
|---|---|---|
| Shallow | Crashed process, closed port | Reports healthy while the database is unreachable |
| Deep | Broken dependencies | A shared dependency outage marks every server unhealthy at once |
Speed versus false positives
Three settings control detection: the probe interval, the timeout for each probe, and the number of consecutive failures required (the threshold).
Worst-case detection time is roughly interval × threshold, plus one timeout. With a 5-second interval and a threshold of 3, a failure may go unnoticed for about 15 seconds.
Lowering these values speeds detection but raises the chance of a false positive: declaring a healthy component dead because of a garbage collection pause, a network blip, or a brief overload. Raising them reduces false positives but lengthens the outage. Pick values from your recovery time objective, and measure how often healthy components miss a single probe.
A health endpoint example
This small server exposes a shallow check at /health and a deeper one at /ready.
from http.server import BaseHTTPRequestHandler, HTTPServer
def database_ok():
# Replace with a real, fast query such as SELECT 1.
return True
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/health":
self.send_response(200)
elif self.path == "/ready":
self.send_response(200 if database_ok() else 503)
else:
self.send_response(404)
self.end_headers()
HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()Keep probe handlers fast and cheap. A check that takes seconds to run can itself cause timeouts.
Common errors
- Checking the wrong thing. A port that accepts connections can still serve only errors.
- Equal thresholds for removal and return. Requiring more successes to re-admit a server than failures to remove it prevents flapping, where a component bounces between healthy and unhealthy.
- Heartbeat timeout shorter than normal jitter. Measure real delay variation before choosing it.
- A monitor that is itself a single point of failure. Run more than one, ideally in different fault domains.
Exercise
A load balancer probes every 4 seconds, with a 1-second timeout and a threshold of 3 failures. About how long can a dead server keep receiving traffic in the worst case, and what is the cost of lowering the threshold to 1?
Show answer
Roughly 4 × 3 = 12 seconds, plus up to 1 second for the last timeout, so about 13 seconds. A threshold of 1 cuts detection to about 5 seconds but makes a single dropped probe, such as from a brief pause or network blip, remove a healthy server, which raises false positives and unnecessary failovers.