Sub topic 8
Load balancing behind a reverse proxy
This chapter assumes you know how a reverse proxy sits in front of servers and relays HTTP requests. It also assumes you can read a basic nginx configuration. Here you will see how a reverse proxy spreads requests across several backends and what can go wrong.
Why balance load at the proxy
One backend has a fixed capacity and is a single point of failure. Running several identical backends fixes both problems, but clients need one address to talk to. The reverse proxy provides it. Clients connect to the proxy, and the proxy picks a backend for each request.
Choosing a balancing method
The method decides which backend gets the next request.
| Method | How it picks | Good for | Watch out for |
|---|---|---|---|
| Round robin | Takes backends in turn | Similar backends, similar requests | Ignores how busy each backend is |
| Weighted round robin | Gives bigger backends more turns | Mixed hardware | Weights must be kept up to date |
| Least connections | Picks the backend with the fewest active requests | Requests with uneven duration | Needs the proxy to track connection counts |
| Hash (for example by client IP or URI) | Maps a key to a backend | Stickiness, cache locality | Uneven spread, and keys can shift when the pool changes |
Round robin is the nginx default. Least connections is usually a better choice when some requests take much longer than others, such as report generation next to simple page loads.
A working configuration
upstream app_servers {
least_conn;
server 10.0.0.11:8080 weight=3 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.13:8080 backup;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}The first server counts as three units of capacity when least connections compares load. The backup server receives traffic only when the other servers are unavailable. After three failed attempts, nginx stops sending requests to a server for 30 seconds. This is passive failure detection: the proxy learns about a failure only when a real client request fails. The next chapter covers active checks.
Session state and stickiness
Balancing is easiest when any backend can answer any request. If a backend keeps a user's session in its own memory, the next request may land on a different backend and the user appears logged out. There are two ways to deal with this:
- Move the state out of the backends, into a shared store such as a database or Redis. This is the preferred option, because it lets you add or remove backends freely.
- Make routing sticky, so the same client reaches the same backend. A hash on a key does this:
upstream app_servers {
hash $cookie_sessionid consistent;
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}The consistent parameter limits how many keys move to a different backend when you add or remove a server. Sticky routing still loses sessions when the chosen backend dies, and it can leave one backend overloaded.
Common errors
- Hashing on the wrong address. Hashing on
$remote_addrbehind another proxy or a cloud load balancer sees only that device's address, so all traffic lands on one backend. - Retrying unsafe requests. When a backend fails mid-request, the proxy may retry on another one. A retried POST can create a duplicate order. nginx does not retry non-idempotent requests by default, so keep it that way unless the application handles duplicates.
- Passing a dead server too much traffic. With passive detection, each failed attempt is a real user's failed request. Keep
max_failsandfail_timeoutin mind when choosing values. - Forgetting the proxy is now the shared point. One proxy balancing many backends is itself a single point of failure. A later chapter covers running several.
Exercise
Your backends handle two kinds of requests: quick lookups and slow exports that take up to a minute. Round robin leaves some backends overloaded while others sit idle. Which method would you switch to, and why?
Show answer
Least connections. Round robin hands out requests in turn without looking at load, so a backend that happens to receive several slow exports falls behind. Least connections sends each new request to the backend with the fewest active requests, which keeps long-running work from piling up on one server.