Skip to content

Lesson 7 of 9 · 25m

Upstream Load Balancing That Fails Gracefully

Upstream blocks, balancing algorithms, passive health behavior, keepalive connections, and the routing tradeoffs that matter when backends degrade.

An upstream group is nginx's answer to a deceptively simple question: which backend should receive this request, and what happens when that backend is sick? The simple answer — round robin — works until one server is slower, one is restarting, one has a warm cache, or clients need connection reuse. Production balancing is failure shaping.

Model upstreams explicitly

upstream app_upstream {
    least_conn;
    server 10.20.1.11:8080 max_fails=3 fail_timeout=10s max_conns=200;
    server 10.20.1.12:8080 max_fails=3 fail_timeout=10s max_conns=200;
    server 10.20.1.13:8080 backup;

    keepalive 32;
}

least_conn sends new requests to the server with fewer active connections, which is often better than round robin when request cost varies. backup servers receive traffic only when primary peers are unavailable. max_conns protects a backend from being assigned more concurrent work than it can handle.

Passive health checks are not continuous probes. max_fails counts failed communication attempts within fail_timeout; after the window passes, the counter resets. This is useful but crude. Open-source nginx learns from traffic; NGINX Plus or an external controller is needed for active health probes.

Keepalive upstream or pay connection tax

Without upstream keepalive, nginx often opens a new TCP connection to the backend for every client request. Under load that creates handshake cost, ephemeral-port pressure, and TIME_WAIT buildup.

location / {
    proxy_pass http://app_upstream;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    include snippets/proxy-headers.conf;
}

HTTP/1.1 plus clearing Connection allows reuse. Do not blindly copy this into WebSocket locations where upgrade headers are intentionally managed. Context matters more than snippets.

Affinity is a compromise, not a feature

ip_hash pins clients by address, but NAT collapses many users into one hash input and mobile clients can move between addresses. It also concentrates load unevenly.

Prefer stateless applications or shared session stores. If affinity is unavoidable, understand what breaks during deployment: removing a server changes hash distribution and can move many users. Cookie-aware persistence in more advanced load balancing layers is often more predictable, but it adds operational complexity.

Weight what you know, not what you hope

upstream rollout_upstream {
    server 10.20.1.11:8080 weight=5;
    server 10.20.1.12:8080 weight=1;
}

Weights are useful for heterogeneous hardware or controlled traffic shifting, but they are not a canary system. If the new version fails only on specific user behavior, splitting raw traffic 5:1 will still hit real users. Use explicit routing keys or application deployment controls when correctness matters.

Field notes

  • Remove dead backends from config during maintenance rather than relying on passive failure detection alone.
  • backup is excellent for maintenance pages or emergency capacity, but test it before the outage.
  • DNS names in upstreams have resolution and TTL semantics that differ by nginx version and resolver configuration; do not assume dynamic re-resolution.
  • Connection reuse changes backend concurrency accounting; monitor both nginx active connections and application worker saturation.

Was this lesson helpful?

Sign in to rate