0% completed
Load Balancing Algorithms
On This Page
Round Robin
Weighted Round Robin
Least Connections
Weighted Least Connections
Least Response Time
Least Bandwidth
IP Hash
Random, and Random of Two
Custom Rules
How to Choose
Key Takeaways
The load balancer receives a request and must send it to one server. The rule it uses to pick that server is called the load balancing algorithm.
Before the rules, one thing is true for all of them. The algorithm only chooses between servers that are currently healthy. A broken server is taken out by the health check, not by the algorithm. So "it does not check server health" is never the weakness of an algorithm. The real difference between them is whether they look at how busy each server is.
That gives two groups.
Static rules decide without looking at the current load. They are cheap and predictable.
Dynamic rules look at the state of the servers right now. They adapt better, and they need the load balancer to keep and update numbers.
Round Robin
Send the first request to server 1, the second to server 2, the third to server 3, then start again.
This is the simplest rule and the most common one. It needs no state and no configuration.
Its weakness is that it counts requests, not work. One request may take 5 milliseconds and the next may take 2 seconds. Round robin gives every server the same number of requests, not the same amount of work.
Use it when the servers are the same size and the requests cost about the same.
Weighted Round Robin
Give each server a number, called its weight. A server with weight 3 receives three requests for every one request that a server with weight 1 receives.
This is the answer when your servers are not the same size. The cost is that you set the weights by hand, and you have to change them when the hardware changes.
Least Connections
Send the next request to the server that has the fewest open connections at this moment.
This rule reacts to real load, and it needs no configuration from you. If one server is stuck with slow requests, its connection count stays high, and it stops receiving new work.
It has one important weakness. It counts connections, not capacity. A small server and a large server that both hold 10 connections look the same to it. The small server then gets an equal share and runs out of resources first.
It works best when connections stay open for a while and their length varies, such as WebSocket traffic or long API calls.
Weighted Least Connections
Divide each server's connection count by its weight, and pick the smallest result.
This fixes the weakness above. It looks at the current load like Least Connections, and it knows the size of each server like Weighted Round Robin. The cost is that you again have to set and maintain the weights.
Least Response Time
Send the request to the server that has been answering fastest in the recent past.
This sounds like the best rule, and it has two traps.
The first trap is that response times move quickly. A short slow moment can push all new traffic to another server, and the choice can jump around.
The second trap is that failing is fast. A server that returns an error in 2 milliseconds looks faster than a healthy server that does real work in 50 milliseconds. Without a health check that looks at the status code, the broken server attracts all the traffic.
Least Bandwidth
Send the request to the server that is currently sending the fewest megabits per second.
This is about the size of the responses, not about speed. It only makes sense when responses are large, for example video or file downloads. For normal API traffic it tells you almost nothing.
IP Hash
Take the client's IP address, run it through a hash function, and use the result to choose the server. The same client address then always reaches the same server.
The purpose of this rule is stickiness. It keeps one user on one server without any shared session storage.
Two things about it are often stated wrongly.
It does not send users to a server near them. The hash of an IP address has no connection to the location of that address. Sending users to a nearby data center is done with DNS or anycast routing, not with IP hash.
It also does not spread the load evenly. Many users can share one address, for example a whole office or a mobile network, and they all land on the same server.
The bigger problem is what happens when the number of servers changes. The choice is normally hash(IP) % number_of_servers. Add one server to a group of three, and that number changes from 3 to 4. Almost every client then moves to a different server. Every session built on stickiness is lost at the same moment.
Consistent hashing is the fix. It is a different way of mapping keys to servers. When you add or remove one server, only a small share of the keys move. It is covered in Consistent Hashing.
Random, and Random of Two
Pick a server at random. With enough requests the spread is even, and the rule costs nothing.
There is a better version of it. Pick two servers at random, then send the request to whichever of the two has fewer connections. This gets very close to the result of Least Connections, and the load balancer only has to compare two numbers instead of tracking every server. NGINX offers it as random two.
Custom Rules
Most cloud load balancers and modern proxies let you write your own rule, using CPU, memory, queue length, or a number your application reports.
This is powerful, and it moves the responsibility to you. You now own the monitoring, and a wrong metric sends traffic to the wrong place.
How to Choose
For most systems the honest answer is that round robin or least connections is enough.
- Servers are the same size and requests are short: round robin.
- Servers are different sizes: weighted round robin.
- Requests take very different amounts of time, or connections stay open: least connections.
- Different sizes and uneven requests: weighted least connections.
- Responses are large files or video: least bandwidth.
- The same user must reach the same server: IP hash. It is usually better to move the session into a shared cache instead, and remove the need for stickiness.
In an interview, name the algorithm, then say what it does not know. That second part is what shows you understand it.
Key Takeaways
- The algorithm only picks between healthy servers. Health checks are a separate mechanism, so "ignores server health" is not a weakness of any algorithm.
- Round robin is simple and counts requests, not work. Weighted round robin adds server size, which you must configure by hand.
- Least connections reacts to real load with no configuration, but it is blind to server capacity. Weighted least connections fixes that.
- Least response time can be fooled by a server that fails quickly, because an error is fast.
- Least bandwidth measures the size of responses, not speed. It is for large downloads and video.
- IP hash gives stickiness. It does not route by location, it spreads load unevenly, and adding a server moves almost every client. Consistent hashing solves the last problem.
- Random of two choices is nearly as good as least connections and much cheaper to run.
The algorithm matters less than most people expect. Uneven load usually comes from somewhere else. Sessions kept in server memory, one very popular item, or requests that cost very different amounts. Fix those first.
reagankm
· 3 years ago
They have the same pros and cons in the list provided here. I had to look up on the internet to try and find a reason to prefer one over the other and it said the random algorithm can be better for security since bad actors don't have a predictable sequence they can use for targeting specific servers.
It might be good to add some mention like this to the list of pros for Random. Or, if my info is false, then add some other descriptor to explain why these algorithms suit different needs.
Raj Kumar Nishad
· 2 years ago
As for stateful application all the requests for a session should go to single server ?
Sid D
· a year ago
An IP can map to any server from the pool. Its not possible that an IP from a specific region maps to a specific server that is geographically close to it. Then why is it mentioned that IP hash LB is useful in geographically distributed clients?
Shishir
· 2 years ago
Some API may need more time to process than others, so I can see how servers can have differing workloads, but wouldn't that have a direct impact on the server's capacity? Is it fair to say that these are essentially the same metric since they are directly related to each other? I'm relatively new to system design, so please let me know if I'm missing some key difference here. Thanks!
Dimitri P.
· 2 years ago
You mention the following use-case for Least Bandwidth algorithm:
Real-Time Applications: Suitable for real-time applications where maintaining low latency is critical.
Can you please elaborate since latency is different from bandwidth?
Himanshu Gupta
· 2 years ago
I believe this might not be the most efficient algorithm in terms of resource utilization. Reason being, it checks the server with the fewest active connections, however, a server with the fewest active connections might be the one with the most capacity ??
Is my reasoning correct, or am I missing something ??
Sid D
· a year ago
CDN's don't specifically have any high bandwidth demands. Its more application specific. So what makes LBW approach specifically useful for CDN use case? Can you expand further on this?
ali ahmed
· 2 years ago
what does the Weighted keyword means here and what is the difference between it and the connections count in least conenctions does it means the resources of the server capacity or how to calculate the weight of a server
anas.beit
· 2 years ago
there is common statement across the topic that No consideration for server health or response time.
However, LB balancer have capability to do periodic health probe of the server and verifying time out from the server, it health probe fail or there will be time out from a specific then LB will not send traffic to divert to the given server.
This point is misleading in the cause
Sandeep Verma
· 2 years ago
Is LB single or multiple machine? If so ho is load distributed across these and who distributes this ( DNS distribution is understood, except for that how it works)
On This Page
Round Robin
Weighted Round Robin
Least Connections
Weighted Least Connections
Least Response Time
Least Bandwidth
IP Hash
Random, and Random of Two
Custom Rules
How to Choose
Key Takeaways