Sub topic 9
Detecting failed backends
A reverse proxy that spreads traffic across several backends must also notice when one stops working. Without that, it keeps sending a share of requests to a dead server, and users see errors. This chapter assumes you know HTTP status codes, the round-robin idea from the load balancing chapter, and the basic nginx upstream block.
Passive health checks
A passive check watches real traffic. The proxy does not send extra requests. It counts failures on the requests clients already make.
In the first nginx example, each server has two settings:
max_fails=3is the number of failed attempts that mark the server as unavailable.fail_timeout=30sis both the window in which those failures are counted and the time the server is skipped afterward.
upstream app_servers {
server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
}By default, nginx counts a connection error or a timeout as a failure. A 500 response is not counted unless you add proxy_next_upstream http_500. Responses such as 404 are never counted, because they usually mean the client asked for something missing, not that the server is broken.
The weakness is that real users pay for the detection. The first requests that hit a dead backend either fail or wait for a timeout.
Active health checks
An active check sends a probe request on a schedule, for example GET /healthz every five seconds, and removes the backend after several failed probes. A backend can be removed before any user reaches it, and it can be returned to service once probes succeed again.
Open source nginx has only passive checks. Active checks come with NGINX Plus, HAProxy, Envoy, and most cloud load balancers.
| Property | Passive | Active |
|---|---|---|
| Extra traffic | None | A probe per backend per interval |
| Detects a failure | After real requests fail | Within a few probe intervals |
| Detects an idle backend that is broken | No | Yes |
| Cost to users | Some requests fail first | Usually none |
Many systems use both. Active probes catch failures early, and passive checks catch problems that probes miss.
What the check should test
A probe is only as good as the question it asks. Three common depths:
- Port open. A TCP connect succeeds. This proves the process is listening and nothing more.
- Liveness.
GET /healthzreturns 200. The application is running. - Readiness. The endpoint also checks what the app needs to serve traffic, such as a database connection.
Deep checks find more faults, but they have a danger. If every backend shares one database and the readiness check queries it, a database outage makes all backends fail together. The proxy then has no healthy server to use, and a partial problem becomes a full outage. Keep the probe cheap, and check only what this instance controls.
Failure and recovery
Detection has to avoid two mistakes. Marking a healthy server as failed shrinks capacity for no reason. Keeping a failed server in rotation sends users to errors.
A diagram helps here, so the lifecycle of a backend is shown below.
Probes also use thresholds. A common pattern is to require three failures to remove a backend and two successes to restore it. A single slow response does not remove a server, and a server that flaps up and down does not return too quickly.
Retrying on another backend
Once a failure is detected, the proxy can retry the same request on a different backend. In nginx this is proxy_next_upstream.
location / {
proxy_pass http://app_servers;
proxy_next_upstream error timeout;
proxy_next_upstream_tries 2;
}Retry only requests that are safe to repeat. By default nginx does not retry non-idempotent requests such as POST after they have been sent, because the first backend may have processed it already.
Common errors
- Probing a path that needs login. The probe gets 401 and the backend looks dead.
- Timeouts that are too long. With a 60-second
proxy_connect_timeout, one dead backend stalls each request for a minute. - No capacity for loss. If the remaining backends cannot carry the load, removing a failed one overloads the rest.
Exercise
Your nginx proxy uses only passive checks, with maxfails=3 and failtimeout=30s. A backend crashes. Roughly how many user requests are affected before the proxy stops using it, and why might a deep readiness probe that queries a shared database be a poor choice for an active check?
Show answer
About three requests sent to that backend fail or time out, because nginx must see three failed attempts before it skips the server for 30 seconds. A readiness probe that queries a shared database makes every backend fail when the database is down, so the proxy removes all of them and a partial outage becomes a total one. A cheap liveness probe of the instance itself avoids this.