On this pageThe algorithms at a glance
A load balancing algorithm is the rule that picks a server for each new request or connection. The choice looks minor until requests stop being alike: one slow report ties up a server while round robin keeps sending it more work, or a cache loses its hit rate because every request for the same page lands somewhere new. The algorithms fall into three families. Static ones decide without looking at the servers, dynamic ones look at the current load, and adaptive ones change their own weights from feedback. Knowing what each looks at tells you which one fits.
The algorithms at a glance
| Algorithm | Picks the server by | Good for | Weak spot | HAProxy | NGINX |
|---|---|---|---|---|---|
| Round robin | Taking turns | Short, similar requests | Ignores load | balance roundrobin |
Default |
| Weighted round robin | Turns in proportion to weights | Servers of different sizes | Weights go stale | weight per server |
weight= per server |
| Least connections | Fewest open connections | Uneven request times, WebSockets | Connections are not work | balance leastconn |
least_conn |
| Power of two choices | Two random servers, the less busy wins | Large pools, several balancers | Needs a load signal | balance random |
random two least_conn |
| Least response time | Fastest recent responses | Latency-sensitive APIs | Needs measurements | Not built in | NGINX Plus least_time |
| Source IP hash | A hash of the client address | Affinity without cookies | Clients behind one NAT pile up | balance source |
ip_hash |
| Key hash | A hash of the URI, a header or a cookie | Caches, sharded backends | Hot keys overload one server | balance uri, hdr(), hash |
hash $key consistent |
| First available | Filling servers in order | Keeping few servers busy | Uneven by design | balance first |
Not built in |
Static: turns and weights
Round robin hands requests out in order. It is cheap, predictable and fair when every request costs about the same, as with static files or simple API calls. Weighted round robin keeps the rotation but sends a server with weight 2 twice as many requests as one with weight 1, which suits a pool of mixed plan sizes.
Static methods have one blind spot: they never notice that a server is struggling. If one request in fifty runs a slow export, the server holding it keeps receiving its full share. HAProxy itself moved away from round robin as its default: up to version 3.2 a backend without a balance line used round robin, and from 3.3 it uses random with two draws.
Dynamic: looking at current load
Least connections sends each new connection to the server with the fewest open ones. It handles mixed request times well and is the usual choice for long-lived connections such as WebSockets, database proxies and remote desktop gateways. Its assumption is that connections are a fair measure of work, which fails when one connection streams a large file and another sits idle.
Power of two choices picks two servers at random and sends the request to the less busy one. It sounds like a shortcut, but it avoids a problem least connections creates when several balancer instances work in parallel: each one sees the same "least loaded" server and they all pile onto it at once. Two random candidates spread that herd while still steering away from busy servers, at almost no cost in computation.
Least response time prefers the servers that answered fastest recently. It reacts to slowness that connection counts miss, such as a server with a cold cache. In NGINX it is part of the commercial NGINX Plus; HAProxy has no direct equivalent but can reach a similar effect with adaptive weights.
Hashing: the same key goes to the same server
Hashing gives affinity: requests with the same key reach the same server, which helps caches, in-memory shards and applications that keep state locally. Source IP hashing needs no cookies, but many clients can share one address behind a corporate or mobile network. NGINX's ip_hash also uses only the first three octets of an IPv4 address, so a whole /24 maps to one server.
Plain hashing remaps most keys whenever a server is added or removed. Consistent hashing moves only the keys that belonged to the changed server, which keeps cache hit rates up during deploys. HAProxy adds bounded loads on top: hash-balance-factor 150 lets no server take more than 1.5 times the average, so a hot key spills over instead of melting one machine. Maglev hashing, from Google's network load balancer design, serves the same purpose in Envoy and in the Linux kernel's IPVS (mh scheduler).
Adaptive: weights that follow feedback
Adaptive load balancing changes how much traffic each server gets based on what the servers report or what the balancer observes. Three mechanisms are common:
- Server-reported weights. HAProxy's agent check polls a small TCP service on each server, which answers with a percentage such as
75%, ordrainto stop new traffic. The server decides from its own CPU, queue length or memory. - Slow start. A server that has just recovered starts with a fraction of its weight and ramps up, so it does not receive a full share while its caches are cold. HAProxy has
slowstart; in NGINX it is a Plus feature. - Observed errors and latency. AWS Application Load Balancer pairs weighted random routing with automatic target weights, which shift traffic away from targets returning errors. Envoy can weight round robin by load reports from the backends, and service meshes such as Linkerd combine power of two choices with a moving average of latency.
The risk is a feedback loop: traffic moves away from a slow server, it recovers, everyone returns, and it slows again. Keep a minimum weight, ramp changes gradually and alert when weights swing.
Configuration examples
A cache tier with bounded consistent hashing, and an application tier that combines least connections with slow start and an agent check:
backend be_cache
balance uri
hash-type consistent
hash-balance-factor 150
server cache-01 198.51.100.31:80 check
server cache-02 198.51.100.32:80 check
backend be_app
balance leastconn
default-server check weight 100 slowstart 30s agent-check agent-port 9999 agent-inter 5s
server app-01 198.51.100.21:8080
server app-02 198.51.100.22:8080
The agent on each application server can be a short script that turns the load average into a weight:
#!/bin/sh
# /usr/local/bin/lb-weight: answer 100% when idle, down to 10% under heavy load
awk -v c="$(nproc)" '{ w = int(100 - ($1 / c) * 50); if (w < 10) w = 10; if (w > 100) w = 100; printf "%d%%\n", w }' /proc/loadavg
Serve it with socat TCP-LISTEN:9999,reuseaddr,fork EXEC:/usr/local/bin/lb-weight, and allow port 9999 only from the balancer's address. The NGINX equivalents for the two tiers:
upstream app {
random two least_conn;
server 198.51.100.21:8080 weight=2;
server 198.51.100.22:8080;
}
upstream cache {
hash $request_uri consistent;
server 198.51.100.31:80;
server 198.51.100.32:80;
}
Pick by workload
| Workload | Start with |
|---|---|
| Short, uniform HTTP requests | Round robin, or random with two choices |
| Fast pages mixed with slow reports | Least connections |
| WebSockets, database proxies, long downloads | Least connections |
| Caches and CDN origins | Consistent hashing on the URI |
| An app that keeps sessions in local memory | Cookie stickiness, while you move sessions out |
| Servers of different sizes | Weights on any of the above |
| Load that changes with time of day or deploys | Agent-reported weights and slow start |
FAQ
Which algorithm should I start with?
Least connections for most web applications, because request times vary more than people expect. Switch to round robin or random two choices only when requests are short and uniform, and to hashing only when a cache or local state needs affinity.
Are sticky sessions the same as hashing?
Both send a client back to the same server, but stickiness usually relies on a cookie the balancer sets, while hashing computes the server from a key. Cookies survive changes in the pool better; hashing needs no state. Either one hides sessions stored locally instead of fixing them.
Can adaptive balancing make things worse?
Yes, when weights react faster than servers recover, or when a bad metric drives them. Start with slow start and conservative weights, watch how traffic shifts during a deploy, and only then let servers lower their own weights.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




