Free preview

Load Balancing Algorithms

Why this matters: the algorithm is the load balancer's actual decision. Pick one that ignores server state and a single slow machine silently degrades a fraction of every user's requests.

Key takeaway

Load balancers use specific algorithms to distribute client requests. The important division is static versus dynamic — whether the choice accounts for the current state of the servers. In practice dynamic algorithms give superior results, and the added complexity is usually justified.

The six algorithms

AlgorithmHow it decidesReach for it when
Round robinForwards requests to servers sequentially in a repeating loopServers are identical and requests are uniform
Weighted round robinEach node is assigned a weight; higher-weighted servers receive a larger proportion of requestsServers have varying capacities
Least connectionsAssigns each new request to the server with the fewest active connectionsRequest processing times vary significantly
Least response timeDirects traffic to the server with the lowest active response timeLatency is the priority — performance-sensitive services
IP hashHashes the client's IP address to map them to a specific serverYou need session stickiness
URL hashDistributes requests based on the URLDifferent clusters serve distinct workloads

Other variations exist, such as randomized and weighted least connections.

The two that need elaborating

Least connections matters more than it first appears. When request processing times vary significantly — some requests return in 5 ms, others run for 30 seconds — round robin keeps handing new work to a server already tied up with long-running tasks. Least connections prevents that server from becoming overwhelmed, because its high in-flight count makes it unattractive until it drains.

URL hash is about routing to specialized clusters, not balancing across identical ones. It routes traffic to specific server clusters handling distinct services — separating video serving from text, for instance. The servers behind different URL paths are genuinely different fleets with different hardware profiles.

Static versus dynamic

Algorithms are categorized by whether they consider the machine's state:

StaticDynamic
ExampleRound robinLeast connections
Basis for the decisionFixed configurationCurrent state of the servers
OverheadLow — simple and cheapHigher — complexity plus communication
Accounts for health and load
Behavior with a slow serverKeeps sending it trafficRoutes around it

Static algorithms distribute tasks based on fixed configurations. They are simple and low-overhead but do not account for real-time server health or load.

Dynamic algorithms monitor the current state of servers. They add complexity and communication overhead, but they improve reliability by routing around overloaded or unhealthy machines. Dynamic algorithms require load balancers to exchange information, which makes them modular but complex — and gives them better forwarding decisions through active health monitoring.

The distributed-state problem

Dynamic algorithms have a subtlety worth raising, because interviewers probe it.

"Least connections" is exact when one load balancer sees all traffic. With several load balancers in front of the same pool, each knows only its own connections — so all of them may independently conclude the same server is least loaded and send it a simultaneous burst. This is a herd effect created by the balancing algorithm itself.

Choosing one

Workload shapeAlgorithmWhy
Identical servers, uniform short requestsRound robinCheapest thing that works
Mixed server capacitiesWeighted round robinWeights encode the capacity difference
Highly variable request durationLeast connectionsIn-flight count tracks real occupancy
Latency-sensitive, heterogeneous fleetLeast response timeDirectly optimizes the metric you care about
Sticky sessions with no shared storeIP hashDeterministic client-to-server mapping
Distinct workloads behind one domainURL hashRoutes each path to its specialized cluster
Many LBs, large poolPower of two choicesNear-optimal without global state or herding

Key takeaway

Start with round robin, move to a dynamic algorithm the moment request durations vary or servers stop being interchangeable — which, at any real scale, is immediately.

Interview signal by level

LevelWhat a strong answer sounds like
L4Names algorithms: "round robin, or least connections."
L5Matches to the workload: "request durations vary a lot here, so least connections — round robin would pile work onto a server already running a long job."
Staff+Handles the distributed case: "with several LBs, strict least-connections herds because each has a stale local view. Power-of-two-choices gets near-optimal balance with no shared state. And I'd rather make servers stateless than use IP hash for stickiness."

Next: what happens when the load balancer has to remember something.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue