What it is
Least connections is a load balancing algorithm that sends each new request or connection to the backend with the fewest currently in progress. At layer 4 it counts open TCP connections; at layer 7 the same idea counts outstanding HTTP requests and is often called least outstanding requests or least request.
Why it exists
Round robin and its weighted variant balance counts, using no information about what the servers are doing. When requests differ in cost, or a server slows down without failing, equal counts produce unequal load. The number of requests in flight is a cheap, live signal of load that a balancer already has: it increments when it forwards a request and decrements when the response comes back. Routing on it adapts automatically.
How it works
The counter. For each backend the balancer keeps an active count. On forward: +1. On response, error or timeout: −1. A new request goes to the backend with the minimum count; ties are broken by list order, randomly or by round robin depending on the implementation.
Why it adapts. By Little's law, requests in flight ≈ arrival rate × time in system. A server that is slow (degraded hardware, a noisy neighbour) or busy with heavy requests holds each request longer, so its count is higher and it is picked less. That is the "join the shortest queue" policy from queueing theory, which minimises waiting when the queue lengths are known.
What it cannot fix. Balancing removes waiting, not work. If one request needs two seconds of CPU, it takes two seconds on any server. Tail latency driven by heavy requests themselves stays; tail latency driven by quick requests queueing behind heavy ones goes away.
Weighted least connections. Plain least connections equalises in-flight counts regardless of capacity, so a server with twice the cores should be allowed twice the count. Weighted variants divide the count by a weight before comparing.
Power of two choices. Instead of finding the global minimum, pick two backends at random and send the request to the one with fewer in flight. This needs information about only two servers, works well when many balancers each see only their own traffic, and avoids every balancer choosing the same idle server. Its worst-case imbalance is exponentially smaller than pure random selection, which is why Envoy's least-request algorithm and many service meshes use it.
When to use it
- Request-level balancing at layer 7 for APIs whose request costs vary.
- Fleets where individual servers can slow down (shared hosts, garbage collection, background jobs).
- Mixed instance sizes, using the weighted variant.
- As a general default when you do not want to maintain weights.
When not to
- Long-lived connections with uneven activity (WebSockets, gRPC streams, database proxies): the count no longer reflects load.
- When you need key affinity for caches or sessions; use consistent hashing.
- Very many independent balancers with no shared view; prefer power of two choices.
- Extremely uniform, short requests on identical servers, where round robin is just as good and simpler.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Least connections over round robin | Adapts to slow servers and uneven requests, no weights | Per-backend counters; newcomers flooded without slow start |
| Power of two choices over global least connections | Works with local, partial information; no herding | Slightly less even than a perfect global minimum |
| Weighted least connections | Handles mixed instance sizes | Weights to maintain again |
| Counting requests (L7) over connections (L4) | Accurate with multiplexed and keep-alive connections | Must terminate and parse the protocol |
Alternatives
- Round robin / weighted round robin: simpler and predictable; fine for uniform work.
- Random: stateless, good with many balancers, but blind to load.
- Latency-based (peak EWMA): weights backends by recently observed response time, catching slowness even before it shows up as in-flight requests.
- Server-reported load: backends publish a load score (CPU, queue depth) that the balancer uses, as in some service meshes and gRPC's weighted round robin with backend metrics.
- Consistent hashing: when key locality matters more than perfect balance.
Failure modes
- Newcomer flood: a restarted or newly scaled-out server has zero in flight and receives almost every request until its count catches up. With cold caches it may slow down or crash again. Use slow start (ramp the share over 30–120 seconds).
- Fast failure looks attractive: a broken server that answers every request instantly with an error has the lowest count and attracts more traffic. Combine with outlier detection on error rate.
- Misleading counts: idle long-lived connections inflate a server's count; multiplexed HTTP/2 connections hide many requests behind one.
- Herding: many balancers with stale or local counts all send to the same apparently idle server at once.
- Unequal capacity: plain least connections gives a large and a small server the same in-flight count, overloading the small one.
In production
HAProxy (leastconn), nginx (least_conn), Envoy (LEAST_REQUEST, power of two choices by default) and cloud layer-7 balancers all provide it. Enable slow start where available, set outlier detection to eject backends with high error rates, and prefer counting requests over connections for HTTP/2 and gRPC. For long-lived connections, cap connections per backend and rebalance by closing a fraction gracefully during deploys or when imbalance grows. Monitor per-backend in-flight counts and latency together; a backend with low count and high error rate is the classic "fast failure" trap.
Common misconceptions
- "Least connections measures CPU." It counts requests or connections in flight; slowness shows up only indirectly.
- "It fixes tail latency." It fixes queueing behind slow work, not slow work itself.
- "The newest server is the best choice." It has the fewest connections, not the warmest caches.
- "More information is always better, so global least connections beats two choices." With many balancers, stale global information causes herding; two random samples with fresh local data often do better.
Interview angle
When an interviewer asks "which algorithm?", least connections (or least outstanding requests) is a strong default for request-level balancing, but justify it: uneven request costs and servers that slow down. Then show depth: slow start for new instances, outlier detection for fast-failing ones, why WebSockets need a different signal, and power of two choices when many balancers or clients do the picking.
Model assumptions
The simulation is a simplified model, not a benchmark. Three servers have 6 workers each, an 80 ms mean service time and room for 12 waiting requests; "variable request sizes" uses a lognormal service time with cv 2.5 instead of 0.35, at the same mean. The balancer counts requests in flight (least outstanding requests) and breaks ties by list order. A degraded server takes four times as long and still passes health checks (every 500 ms; 3 successes restore a server). The simulator has no power-of-two-choices or slow-start mode; the lesson compares least connections with random and describes the others. Arrivals are random (Poisson); network hops take a fixed 5–25 ms. Numbers show behaviour and orders of magnitude only.