Learn Labs
Load Balancing

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.

upstream app {zone app 64k;least_conn;server app-1:8000 max_fails=2 fail_timeout=10s resolve;2 failures in 10 s -> out for 10 s...}location /api/ {proxy_next_upstream error timeout http_502 http_503;what counts as "try another server"proxy_next_upstream_tries 2;at most 2 servers per requestproxy_next_upstream_timeout 6s;don't START another try after 6 s}

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

clientnginxapp-2 (hung)app-3GET /api/whoamiproxyno answer for 5 sretry (proxy_next_upstream timeout)200 after 5.0 sPOST /api/uploadproxyno answer for 10 s504: POST is never 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 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.012
6 s: no new tries after thisattempt 1app: 5.005 s → 504attempt 2starts at 5 s: 5.012 s → 504clientwaits 10.02 s, gets 504seconds → 0 … 10

Three 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 6s didn'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.

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: 6
app-2: 4 of 12refused, retried elsewhereupstream appserver app-2 … resolveupstream app_rrresolved at reloadapp-2now 172.30.0.8ip-thiefhas 172.30.0.3without resolve, app-2 got 0 of 12 requests until nginx -s reload

The 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.

resolver 127.0.0.11 valid=10s ipv6=off;Docker's DNS; Kubernetes: kube-dnsupstream app {zone app 64k;required for resolveserver app-1:8000 ... resolve;re-resolved every 10 s (open source since 1.27.3)}

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

  1. Run failover.py. Then set proxy_connect_timeout 60s (the default) and run it again. How long does the slowest request take?
  2. Run timeouts.py with timeout removed from proxy_next_upstream. How long does the 7 s request take now, and how many backends run it?
  3. Run stale_dns.py, then reload Nginx and repeat the requests through /lb/round-robin/.

On this page