Skip to content

Lesson 3 of 9 · 25m

Upstreams, Health Checks, and Failover Without Amplification

Model upstream failure, configure balancing deliberately, and prevent retries and failover from multiplying load during partial outages.

Load balancing is easy on a whiteboard and dangerous in a brownout. When one upstream slows down, naive retry logic sends the same expensive request to the next server, then the next, until every worker is busy serving duplicates. Good upstream design assumes failure and decides in advance which failures deserve another attempt.

Start with an explicit upstream group

upstream app_backend {
    least_conn;
    server 10.20.0.11:8080 max_fails=3 fail_timeout=10s;
    server 10.20.0.12:8080 max_fails=3 fail_timeout=10s;
    server 10.20.0.13:8080 max_fails=3 fail_timeout=10s backup;
}

server {
    location / {
        proxy_pass http://app_backend;
        proxy_next_upstream error timeout http_502 http_503;
        proxy_next_upstream_tries 2;
        proxy_next_upstream_timeout 10s;
    }
}

least_conn is usually safer than round robin when requests have uneven cost. The backup server is a pressure valve, not a hidden third production node; if it cannot carry traffic, use it for maintenance responses or critical read-only behavior.

Health checks must test reality

Open-source Nginx passive health checks infer failure from proxied responses. They do not independently probe upstreams. Apache mod_proxy_balancer has balancer members and can integrate with external health checking depending on version and distribution, but many production systems still rely on an external orchestrator or load balancer for active checks.

A useful health endpoint answers: can this instance serve traffic now? It should not depend on every downstream service unless serving truly requires all of them. A database hiccup that returns 500 on /healthz can eject every application node simultaneously, converting a database brownout into a total application outage.

Retries are not free

client sends 1 request
proxy retries node A -> node B
application internally retries database twice
queue consumer retries job three times

One user click has now become a dozen units of work. Only retry idempotent operations unless the application explicitly uses idempotency keys. GET is usually retryable. POST /payments is not automatically retryable just because the HTTP method reached a timeout before you saw the response.

Connection reuse changes upstream capacity

Keepalive connections between proxy and app reduce TCP churn:

upstream app_backend {
    server 10.20.0.11:8080;
    keepalive 64;
}

location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass http://app_backend;
}

Without Connection "", Nginx may forward hop-by-header state that prevents clean reuse. With Apache, pool sizing and worker model interact differently, so measure instead of copying numbers. High keepalive is not a universal good; stale connections can pin traffic to draining or half-dead nodes.

Drain before you remove

A node should pass through states: active, draining, inactive, removed. During draining it accepts no new upstream selection but finishes in-flight requests. Configuration reloads, orchestration hooks, and load balancer health checks all participate. Pulling a process out from under active requests creates the exact 502s your proxy was supposed to hide.

Field notes

  • max_fails is passive failure accounting, not a synthetic active health check.
  • Timeouts can mean the work succeeded upstream even though the client never received a response.
  • Retrying non-idempotent requests is a business-logic decision before it is a proxy setting.
  • Watch per-upstream latency separately; fleet averages hide the one node that is poisoning tail latency.

Was this lesson helpful?

Sign in to rate