Balancing Methods
Round robin, weighted, least_conn, ip_hash, consistent hashing, and shared zones
An upstream block names a group of servers; proxy_pass http://<name>
sends each request to one of them. The method decides which. This
module defines one upstream per method over the same three instances, and
/lb/<method>/ routes to each, so labs/load_balancing.py can compare
them side by side.
Sequential requests
30 sequential requests per method:
round-robin app-1: 10, app-2: 10, app-3: 10
weighted app-1: 18, app-2: 6, app-3: 6
least-conn app-1: 10, app-2: 10, app-3: 10
ip-hash app-1: 30- app-1: 10
- app-2: 10
- app-3: 10
- app-1: 18
- app-2: 6
- app-3: 6
- app-1: 30
- Round robin (the default): each server in turn.
- Weighted:
weight=3gives app-1 three of every five requests, for bigger machines or gradual rollouts. - least_conn: the server with the fewest active requests. With sequential requests nothing is ever active, so it degrades to round robin.
- ip_hash: the client address picks the server. All 30 requests came from one IP and all went to app-1. Behind a NAT or another proxy, a whole office or a whole CDN is one "client."
Hashing by a key
hash $arg_user consistent: each user sticks to one instance
user=alice -> ['app-2']
user=bob -> ['app-1']
user=carol -> ['app-1']
user=dave -> ['app-3']
user=erin -> ['app-3']hash <key> consistent maps any key (a user id, a URL, a tenant) to a
server with consistent hashing: adding or removing a server moves only
about 1/n of keys. That matters when instances keep per-key state, such as
in-process caches, or when the upstreams are cache servers themselves.
Plain hash without consistent remaps almost every key when the server
list changes.
Uneven requests: where least_conn earns its keep
Two 3-second requests go to the upstream, then 12 quick ones while the slow ones are still running:
round-robin slow requests on ['app-1', 'app-2']; 12 quick ones went to app-1: 4, app-2: 4, app-3: 4
least-conn slow requests on ['app-2', 'app-3']; 12 quick ones went to app-1: 12Round robin keeps sending its share to the busy instances, where those
requests queue behind the slow ones. least_conn sends every quick
request to the only idle instance.
The zone that wasn't there
The first run of this lab had no zone directives, and the results were
very different:
round-robin slow requests on ['app-1', 'app-1']; 12 quick ones went to app-1: 4, app-2: 4, app-3: 4
least-conn slow requests on ['app-1', 'app-1']; 12 quick ones went to app-1: 4, app-2: 4, app-3: 4- app-1 (busy): 4
- app-2: 4
- app-3: 4
- app-1 (idle): 12
Both slow requests landed on app-1 even with round robin, and least_conn
kept sending quick requests to the busy app-1. The reason is the
process model: without a zone,
each of the 12 workers keeps its own copy of the upstream's state:
its own round-robin position, its own active-connection counts, its own
failure marks. Each worker's round robin starts at app-1, and each worker
only sees the requests it is proxying.
Give every upstream a zone. It puts the state in shared memory, so all
workers balance as one. It's also required for resolve (re-resolving
DNS) and for changing upstreams at runtime in NGINX Plus. 64 KB is enough
for dozens of servers.
Choosing
| Workload | Method |
|---|---|
| Similar, short requests | Round robin |
| Requests of very different cost (reports, uploads, AI calls) | least_conn |
| Instances of different size | weight= |
| Per-key state or cache locality | hash $key consistent |
| "Sticky sessions" | Avoid: keep session state in Redis. If unavoidable, hash a session cookie, not the IP |
Lab
- Run
load_balancing.py, then remove thezonelines, reload, and run it again. - Add a fourth server to
app_hash. How many of 100 user ids move? Then removeconsistentand repeat. - Give app-1
weight=0and read the error fromnginx -t(invalid parameter "weight=0"). How else could you take one server out of rotation? (Look updownandbackup.)