Nginx Load Balancing: Least Connections, Hashing and Random Selection
Least connections for uneven request durations
Imagine two report workers. One is processing five slow reports while the other has one. Equal round robin can keep sending new work to the busy worker. With equal weights, least_conn favors the peer with fewer active connections; ties use weighted round robin. It is not a measurement of CPU, memory, queue length or application health.
upstream reports { least_conn; server 127.0.0.1:18171; server 127.0.0.1:18172;}Weights also influence least-connections selection. Do not compare raw connection counts alone when weights differ. A single slow request can be more expensive than many fast ones, so inspect application latency and saturation. A sequential lab verifies routing only; it cannot establish that least connections outperforms round robin.
Affinity through a stable key
upstream by_client { ip_hash; server 127.0.0.1:18171; server 127.0.0.1:18172;}IP hash uses the first 24 bits of an IPv4 client address or all of an IPv6 address. Requests from one client tend to stay with one available peer. Clients behind NAT can cluster together, and a mobile client can change address. Ensure the address trust policy from the previous chapter is correct; affinity is not authentication.
upstream asset_pool { hash $uri consistent; server 127.0.0.1:18171; server 127.0.0.1:18172;}Here the normalized path is the key, so different query strings on the same path use the same key. If queries distinguish objects, this may be the wrong cache routing key. Consistent hashing reduces key movement when peers change, but does not guarantee no movement. Choose a bounded meaningful key; missing keys may concentrate traffic. Never use routing affinity as authorization.
Random choices and latency-aware routing
upstream random_pool { random two least_conn; server 127.0.0.1:18171; server 127.0.0.1:18172;}Random selection is probabilistic. A handful of requests cannot establish its long-run distribution. The two-choice variant considers two candidates rather than scanning the entire pool. It is useful to evaluate when many load balancers distribute work independently.
upstream latency_pool { least_time header; server 127.0.0.1:18171; server 127.0.0.1:18172;}The standalone least-time method combines observed timing with connection counts. The current module documentation identifies open-source availability from 1.31.0; earlier releases require the commercial edition. The least-time option inside random two remains separately commercial in the checked documentation. Do not use the fragment on the observed 1.26.3 open-source build.
Session state remains an application concern
Affinity makes a routing preference, not a durable session. A failed peer can cause a different server to answer. Cookie-based sticky routing is a separate facility; the current documentation identifies OSS availability of sticky from 1.29.6, with some synchronization options still commercial. Check exact version and product. For an application with important session data, design storage and recovery so losing a single instance does not erase the session.
Practice
Choose a method for similar stateless requests, variable-length reports and path-keyed cache shards. Explain why a local in-memory login session still needs a recovery design.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Load Balancing: Least Connections, Hashing and Random Selection
Apply upstream selection concepts to original examples.
With equal weights, A has 5 active connections and B has 1. Which is favored by least_conn?
What can IP hash group together?
What does consistent hashing help reduce?
Which key ignores query arguments in the fragment?
Does random selection guarantee an exact 50/50 split in 4 requests?