Learn Labs
Load Balancing

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.

upstream app_rr { zone app_rr 64k; server app-1:8000; server app-2:8000; server app-3:8000; }upstream app_weighted { zone app_weighted 64k; server app-1:8000 weight=3; server app-2:8000; server app-3:8000; }upstream app { zone app 64k; least_conn; server app-1:8000 ... resolve; ... }upstream app_iphash { zone app_iphash 64k; ip_hash; ... }upstream app_hash { zone app_hash 64k; hash $arg_user consistent; ... }

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
round robin30 requests
  • app-1: 10
  • app-2: 10
  • app-3: 10
weighted (app-1 weight=3)30 requests
  • app-1: 18
  • app-2: 6
  • app-3: 6
ip_hash (one client IP)30 requests
  • app-1: 30
  • Round robin (the default): each server in turn.
  • Weighted: weight=3 gives 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: 12

Round 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
least_conn, no zone: each of 12 workers counts only its own requests12 quick requests
  • app-1 (busy): 4
  • app-2: 4
  • app-3: 4
least_conn, zone app 64k: shared counts12 quick requests
  • 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.

Choosing

WorkloadMethod
Similar, short requestsRound robin
Requests of very different cost (reports, uploads, AI calls)least_conn
Instances of different sizeweight=
Per-key state or cache localityhash $key consistent
"Sticky sessions"Avoid: keep session state in Redis. If unavoidable, hash a session cookie, not the IP

Lab

  1. Run load_balancing.py, then remove the zone lines, reload, and run it again.
  2. Add a fourth server to app_hash. How many of 100 user ids move? Then remove consistent and repeat.
  3. Give app-1 weight=0 and read the error from nginx -t (invalid parameter "weight=0"). How else could you take one server out of rotation? (Look up down and backup.)

On this page