Backend Failures
Passive health checks, retries, idempotency, timeouts, and stale DNS
Backends crash, hang, get redeployed and move. Open-source Nginx has no active health checks: it learns a backend is broken by sending it real client requests and watching them fail. This lesson measures what that costs.
A stopped backend
docker compose -f compose_prod.yaml run --rm labs python failover.py
all up: 200 app-1: 10, 200 app-2: 10, 200 app-3: 10
app-2 stopped
round 1: 200 app-1: 14, 200 app-3: 16 (30 requests in 6.16s, slowest 2.02s)
round 2: 200 app-1: 15, 200 app-3: 15 (30 requests in 2.15s, slowest 0.00s)
round 3: 200 app-1: 15, 200 app-3: 15 (30 requests in 2.15s, slowest 0.00s)No client saw an error: each request sent to app-2 failed to connect
and was retried on another instance. But two requests paid
proxy_connect_timeout (2 s) before app-2 hit max_fails=2 and was taken
out. Every 10 seconds (fail_timeout), Nginx lets one real request test it
again. With the default 60 s connect timeout, those clients would have
waited a minute.
A hung backend
docker pause app-2: the kernel still accepts TCP connections, but the
process never answers. This is worse than a crash: connecting succeeds, so
only proxy_read_timeout catches it.
app-2 paused (TCP accepted by the kernel, no response)
GET /api/whoami, paused again:
round 1: 200 app-1: 6, 200 app-3: 6 (12 requests in 5.87s, slowest 5.01s)
round 2: 200 app-1: 6, 200 app-3: 6 (12 requests in 0.85s, slowest 0.00s)Again no errors, and this time the unlucky request waited the full 5 s read timeout before the retry.
POST is not retried
POST /api/upload x6 first: a POST that reached a backend is never retried
200 app-1: 1, 200 app-3: 3, 504 -: 2 (slowest 10.02s)Nginx doesn't know whether the hung backend processed the upload before
going quiet. Retrying could create a duplicate order, a double payment, a
second email. So once a non-idempotent request (POST, PATCH, LOCK) has
been sent to a backend, Nginx never passes it to another one. The two
POSTs sent to app-2 failed with 504 after /api/upload's 10 s
timeout.
Retries are only safe for idempotent operations. Make writes idempotent
with a client-generated idempotency key that the backend
deduplicates on, and then the client can retry safely. Adding
non_idempotent to proxy_next_upstream turns POST retries on in Nginx;
only do that for endpoints you know are idempotent.
Retries multiply slow requests
/api/slow?ms=7000 through a 5 s read timeout with retries on timeout:
backend takes 1.0s -> 200 after 1.01s
backend takes 4.5s -> 200 after 4.52s
backend takes 7.0s -> 504 after 10.02s
access log for the 7 s request:
status 504
request_time 10.017
upstream 172.30.0.7:8000, 172.30.0.2:8000
upstream_status 504, 504
upstream_response_time 5.005, 5.012Three findings in one log line:
- The work ran twice. Both backends spent 5 seconds on the same request. If slowness comes from overload, retrying on timeout doubles the load at the worst moment.
proxy_next_upstream_timeout 6sdidn't cap the request at 6 s. It only stops Nginx from starting another attempt after 6 s. The second attempt started at 5 s and ran its full 5 s.- Slow requests mark healthy servers as failed: a timeout counts toward
max_fails, so a burst of genuinely slow requests can take every instance out of rotation.
Set timeouts per route from measured latency, and don't retry on
timeout for routes where slowness means load. Keep timeout in
proxy_next_upstream only where a hang means a dead process.
A backend that moves
Nginx resolves upstream host names once, when it starts or reloads. In
stale_dns.py, app-2 is restarted while another container takes its old
IP address:
app-2 moved 172.30.0.3 -> 172.30.0.8; another container now has 172.30.0.3
with resolve app-1: 4, app-2: 4, app-3: 4
without resolve app-1: 6, app-3: 6The upstream without resolve kept sending app-2's share to the old
address, where a different container refused it. It was retried elsewhere,
so clients saw no errors, but app-2 got no traffic at all until the
next reload. With a different container listening on the old port, the
requests would have gone to the wrong service.
A side effect showed up in the caching lab: with resolve, stopped
containers disappear from DNS, so Nginx drops them at once instead of
paying connect timeouts.
Lab
- Run
failover.py. Then setproxy_connect_timeout 60s(the default) and run it again. How long does the slowest request take? - Run
timeouts.pywithtimeoutremoved fromproxy_next_upstream. How long does the 7 s request take now, and how many backends run it? - Run
stale_dns.py, then reload Nginx and repeat the requests through/lb/round-robin/.