In the previous article, we got an insight into the load balancer, what is a load balancer, how it works,different types of load balancer, and the companies which provide the load balancer.
If you haven't read that article
You can check it here : - Load Balancer
In this article, we will delve into the different types of load balancing algorithms. The different types of Load Balancing algorithms are:
- Weighted Round Robin
- Least Connections
- Hashing Methods
- URL Hash Method
- Source IP Hash Method
- Random Algorithm
- Least Response Time Method
Why do we need Load balancing algorithms
Using a load balancing algorithm, we can efficiently distribute the request to the pool of servers. Depending on the different situations,like server capacity, client requests, current connection time, IP address we can determine which load balancing algorithm should be used. A load balancer must be efficient, this ensures the higher availability of web services and maximizes the responsiveness.
Let's see the different types of load balancing algorithms:
In round robin ,the requests from the client is distributed in the cyclic manner.What is that mean,we will see with the help of diagram
In the above diagram, let's say we have three servers A, B, and C and requests from the clients 1,2,3,4,5,6 come to load balancer. The load balancer will forward the request from client's 1 to server A, client's 2 requests to server B, client's 3 requests to server C.Now, for client's 4 requests, the load balancer will forward back to server A, client's 5 requests to server B and the same cycle repeats.
Loads are evenly distributed which increases the responsiveness of the servers.
But what will happen, if the server B will have higher RAM,CPU and other specs than the server A and C.In that case ,server A and C may get overloaded and fail quickly, while server B sit idle.
This method can be preferred where the servers configuration are same.
2) Weighted Round Robin
Dealing with different configurations of the servers, the administrator can assign the weight or ratio to the server, depending on the request it can handle. Let say, server A can take 3 requests per second, server B can take 2 requests per second on an average, and server C can take 1 requests per second.
So the load balancer will assign a weight to the server A=3,B=2,C=1.You can see the diagram below
Now, if the request comes from the clients, the load balancer will forward the first three request to server A, then client's 4 and 5 request to server B and client's 6 request to server C.After this, if there will be 7,8,9 requests would be there, the same cycle will be repeated like round-robin.
3) Least Connections
In round-robin and the weighted round-robin, we can see that the load balancer is not taking consideration of the current load connections on each server.
Let say we are taking first case where all the servers have same configuration.See the diagam,
On server A the client's 1 and 2 requests are not disconnected yet
On server B client's 3 request are not disconnected. But the client's 4,5 & 6 requests are already disconnected. Now if the new request comes in so according to the round-robin algorithm, it will be forwarded to server A, then server B, and then server C.Now from here, we can see loads on server A to pile up and server A resources may be exhausted quickly.
So, here least connection method can play a major role. The least connection algorithm takes consideration of the current load on each server, so for new upcoming requests, the load balancer will forward that request to the server which has the least connections.
If the servers have different configuration?
In that case , weighted least connection algorithm will play a part.Weighted least connection will do same thing ,like what the weighted round-robin does for round-robin.In this method ,two things will be taken into consideration first least connected servers and the weights assign to each servers.
4) Hashing Methods
Hashing algorithms are used in the case of persistant connections(means stick a client to the specific server).This may be due to wide range content is serving to the clients like videos.Cache to be served,this reduces the response latency,better cpu utilization.
Different hashing methods can be used like:
- URL Hash method
- Souch IP Hash method
The below diagram is shown the how requests from the client is hash based on it's url or ip address,forwarded to the respective server.
URL Hash Method
Load balancer generates the hash value based on the HTTP URL present in requests coming from the clients.Based on hash value,requests will be forwarded to servers.So if the same request coming for the same url,it will be send to the same server.
Source IP Hash Method
In this method ,the client(or source) and server(or destination)
IP address is used to generate the hash key.Now ,according to this key,load balancer will allot the client to particular server.
This can be useful to connect the client to the same server after disconnection.Like storing the items to the cart.
5) Random Algorithm
As the name suggest, the request will be forwarded to random servers.
For forwarding the request,load balancer use random number generator.
6) Least Response Time Method
Server is selected based on the least reponse time, let say server A is responding in 1 second
server B is responding in 2 seconds and server C is responding in 3 seconds.Then if the more requests come to load balancer,load balancer will forward the requests to the server which has least response time ,like server A.
We have covered the Load balancing algorithms in detail.There are more load balancer algorithms like global server load balancing ,layer 7 Content switching .
This is all about load balancing algorithms.Thankyou for reading this article at OpenGenus.