Sub topic 7
Choosing a Load-Balancing Method
Assume you understand Nginx server and location blocks, HTTP headers, and how an application listens on a TCP port. This chapter assumes that you already know how to proxy a request to one backend. It focuses on choosing a policy when several backends can serve the same application.
A load-balancing method decides which upstream server receives each request. The right method depends on request duration, session state, and whether all servers have similar capacity.
Round robin: the default
Round robin sends requests to servers in sequence. Nginx uses it when you define an upstream group without another method.
upstream app_backend {
server 10.0.0.11:8080;
server 10.0.0.12:8080;
server 10.0.0.13:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}For similar servers and requests with similar processing time, round robin is simple and effective. It does not measure current load. A slow request can remain active while later requests go to the next server.
Least connections
least_conn sends a new request to the server with the fewest active connections.
upstream app_backend {
least_conn;
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}This method often works better than round robin when requests take different amounts of time or when clients keep connections open. It balances active connection counts, not CPU use, memory use, or application queue length. A server can have few connections but still be overloaded by expensive work.
Client affinity with ip_hash
ip_hash usually sends requests from the same client IP address to the same server.
upstream app_backend {
ip_hash;
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}This can help legacy applications that store login sessions in local memory. It is not a replacement for shared session storage. Many users behind one NAT gateway may appear as one client, which can create an uneven distribution. Client IPs can also change.
Hashing a chosen key
The hash method lets you select a key, such as a request argument or host name. The consistent option reduces remapping when servers are added or removed.
upstream app_backend {
hash $request_uri consistent;
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}Hashing is useful when the same key should usually reach the same backend, such as a cache partition. It can overload a server if one key is much more popular than others. Choose a key with enough variation.
Weights and method selection
A weight gives a server more or less traffic. The meaning depends on the selected method.
| Method | Useful when | Main risk |
|---|---|---|
| Round robin | Servers and requests are similar | Ignores current connection load |
| `least_conn` | Request duration varies | Connections do not equal workload |
| `ip_hash` | Temporary client affinity is needed | NAT and changing IPs cause skew |
| `hash` | A stable request key matters | Hot keys overload one server |
upstream app_backend {
least_conn;
server 10.0.0.11:8080 weight=3;
server 10.0.0.12:8080 weight=1;
}Here, the first server has three times the selection weight of the second when their connection counts are comparable. Test the result under realistic traffic. Load balancing cannot fix an application that requires local state without either shared state or deliberate affinity.
Exercise
An application has long-lived requests with widely varying durations, and its servers have similar capacity. Which method is a sensible starting point, and why?
Show answer
Use least_conn because it tends to send new requests to servers with fewer active connections. Confirm the choice with load testing because connection count does not measure every kind of application work.