Load Balancing Algorithms Explained
When one server can no longer handle all the traffic your application receives, you add more servers. Then a new question appears: how does each incoming request find its way to one of them? The component that answers it is a load balancer, and its decisions follow a set of rules called load balancing algorithms.
Choosing the right algorithm affects response times, resource use and how gracefully your system handles uneven or failing servers. This article explains the common options in plain terms.
What a Load Balancer Does
A load balancer sits in front of a group of servers, often called a pool or backend set. Clients connect to the load balancer's address. For each new connection or request, it picks one server from the pool and forwards the traffic there.
Beyond distribution, a load balancer usually performs health checks. It periodically tests each server, and if one stops responding properly it is taken out of rotation until it recovers. This is a major reason to use one: users keep getting answers even when individual servers fail.
Load balancers work at different layers. A layer 4 balancer routes based on network information such as addresses and ports. A layer 7 balancer understands HTTP and can make choices based on paths, headers or cookies.
Round Robin
The simplest method sends requests to servers in a repeating order: first server, second server, third server, then back to the first.
It is easy to understand and needs no information about server state. It works well when servers are identical and requests take roughly equal effort. The weakness is that it ignores reality. If one request is very heavy or one server is slower, round robin keeps sending the same share of work anyway.
Weighted Round Robin
Weighted round robin adds a number to each server representing its capacity. A server with weight three receives three requests for every one sent to a server with weight one.
This suits pools of mixed hardware, such as a newer machine with more memory beside an older one. You can also use weights to send a small share of traffic to a freshly deployed version during a gradual rollout.
Least Connections
This algorithm sends each new request to the server currently handling the fewest active connections.
It adapts to real conditions. If some requests take a long time, the busy server accumulates open connections and receives fewer new ones. It is a strong choice for workloads where request duration varies a lot, such as long-lived connections, file uploads or streaming responses. A weighted version also exists, which accounts for different server sizes.
Least Response Time
Some balancers combine the number of active connections with how quickly each server has been answering. The request goes to the server that is both lightly loaded and fast.
This reacts to hidden problems, like a server slowed by a noisy neighbor or a failing disk, which connection counts alone would not show. It needs more measurement, so it is usually found in more capable balancers.
IP Hash
IP hash computes a value from the client's IP address and uses it to choose a server. The same client address consistently lands on the same server.
That property is called session affinity, or sticky sessions. It helps when a server keeps user state in its own memory. The drawbacks are uneven distribution, since many users may share one address behind a corporate network, and disruption when servers join or leave the pool, because the mapping changes. A refinement called consistent hashing reduces that disruption.
Random and Power of Two Choices
Plain random selection works surprisingly well with many servers, since the law of averages spreads the load. A smarter version picks two servers at random and sends the request to the less busy of the two. This simple trick avoids most of the overload that pure random choice can cause, and it needs little shared information, which is valuable in large distributed setups.
Choosing an Algorithm
Match the method to your traffic pattern.
- Identical servers and uniform requests: round robin is enough.
- Mixed server sizes: use weighted variants.
- Requests with very different durations: least connections or least response time.
- State stored on the server: sticky sessions through IP hash or cookies, though shared session storage is usually a better long-term answer.
- Very large pools: random or two-choice methods.
Practical Advice
- Keep applications stateless where you can, so any server can handle any request and the algorithm matters less.
- Set sensible health checks, including a path that tests real dependencies, not just whether the port is open.
- Plan for graceful removal. Let a server finish current requests before shutting down, a step known as connection draining.
- Watch metrics per server. An uneven spread is a clue that the algorithm or the servers need attention.
- Remember that the load balancer itself can fail, so run more than one.
Frequently Asked Questions
Which load balancing algorithm is best?
None is best for everything. Round robin is a fine default for uniform traffic, while least connections handles uneven request times better. Pick based on your workload and measure the result.
What are sticky sessions and should I use them?
Sticky sessions send a user's requests to the same server each time. They are a quick fix for servers that store session data locally, but a shared session store lets any server handle any user and is usually more resilient.
How does a load balancer know a server is down?
It runs health checks, sending a small test request at regular intervals. If a server fails several checks in a row, the balancer stops sending traffic to it until checks succeed again.