Sub topic 5
Health checks: keeping traffic off failed backends
This chapter assumes you know how HTTP and TCP connections work, and that you have read the earlier chapters on what a load balancer does and on balancing algorithms. It covers how a load balancer decides that a backend is unfit for traffic, and how to configure that in NGINX and HAProxy.
Why health checks exist
A load balancer that keeps sending requests to a dead server turns one failure into many failed user requests. A health check is a test the load balancer uses to decide whether a backend may receive traffic. When a backend fails the test, the balancer takes it out of rotation. When it passes again, the balancer puts it back.
There are two families: passive and active.
Passive health checks
Passive checking watches real traffic. If connections to a backend fail or time out, the balancer counts the failures. After enough of them, it stops using that server for a while.
NGINX open source uses this model. In the example below, max_fails=3 and fail_timeout=30s mean that three failed attempts within 30 seconds mark the server as unavailable for the next 30 seconds.
upstream app {
least_conn;
server 10.0.0.11:8080 weight=3 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.13:8080 backup;
}
server {
listen 80;
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}The backup server receives traffic only when the other servers are unavailable.
Passive checks cost nothing extra and need no endpoint on the backend. The drawback is that real users pay for the detection. The first failing requests are the probes.
Active health checks
Active checking sends synthetic probes on a timer, independent of user traffic. The balancer requests a URL or opens a connection every few seconds and judges the result.
HAProxy supports this directly. The check keyword on a server line enables probing, and option httpchk makes the probe an HTTP request.
frontend web
mode http
bind *:443 ssl crt /etc/haproxy/certs/site.pem
default_backend app
backend app
mode http
balance roundrobin
option httpchk GET /healthz
http-check expect status 200
cookie SRV insert indirect nocache
server app1 10.0.0.11:8080 check cookie app1
server app2 10.0.0.12:8080 check cookie app2Here each server is probed with GET /healthz, and only a 200 response counts as healthy. The cookie lines handle session persistence, which a later chapter covers.
Active checks find failures before users do. They also let a server that has recovered come back without waiting for a real request to succeed.
Check depth
The probe can test different things, and deeper tests catch more problems.
| Check type | What it proves | What it misses |
|---|---|---|
| TCP connect | The port accepts connections | The application may be hung or erroring |
| HTTP status | The app answers a request correctly | Failures in dependencies the endpoint does not touch |
| Deep check | The app can reach its database and cache | Can cause a mass outage if a shared dependency fails |
A deep check has a real hazard. If every backend's /healthz queries the same database and that database slows down, all backends fail their checks at once. The balancer then has no healthy servers. Keep health endpoints cheap, and consider separating "this process is alive" from "this process can serve useful work".
Thresholds and timing
Active checks have a few settings that trade speed against stability:
- Interval: how often the probe runs.
- Fall: how many consecutive failures mark a server down.
- Rise: how many consecutive successes mark it up again.
- Timeout: how long to wait for a response.
In HAProxy these are inter, fall, rise and timeout check. For example, check inter 2s fall 3 rise 2 takes a server out after about six seconds of failures and restores it after two good probes.
Short intervals and low thresholds react quickly but can cause flapping, where a server moves in and out of rotation because of one slow response. Long intervals are stable but leave users hitting a dead server longer.
Common errors
- Checking a path that needs authentication. The probe gets a 401 and the balancer marks a working server down.
- Checking only the TCP port. A deadlocked application still accepts connections.
- Sending probes to a different port or host name than real traffic uses, so the check passes while real requests fail.
- Using a very low
fallvalue, which causes flapping.
Exercise
A backend's /healthz endpoint queries the shared database. The database becomes slow, and every backend starts failing its health check. What happens at the load balancer, and how would you reduce the risk?
Show answer
The balancer marks all backends unhealthy and has no server to send traffic to, so a partial slowdown becomes a full outage. Reduce the risk by making the health endpoint check only whether the process can serve requests, or by using a separate readiness check with lenient thresholds for dependencies.
Exercise
In HAProxy, a server uses check inter 2s fall 3 rise 2. Roughly how long after it stops responding is it removed, and how many good probes are needed to bring it back?
Show answer
About six seconds (three failed probes at two-second intervals). It needs two consecutive successful probes to return.