Load balancers can receive and distribute requests to a particular server based on various load balancing techniques which use different load balancing algorithms to select servers based on a specific configuration. Load balancing algorithms are categorized into two parts:
Round robin is one of the simplest load balancing algorithms, ensuring client requests to a different server based on a rotating list. Here load balancer maintains a list of available servers and directs the incoming request in a round-robin fashion: 1st request to the 1st server, 2nd request to the 2nd server, so on. When the load balancer reaches the end of the list, it goes to the list's beginning and starts from the first server again.
The weighted round-robin load-balancing algorithm is an advanced version of the simple round-robin algorithm. It distributes the incoming request based on the weighted score of the servers. Here weight can be an integer that can vary according to the server's processing power or specification. So, It considers server specifications and distributes the traffic based on that.
This algorithm randomly maps requests to the server using some random number generator. Whenever a load balancer receives requests, a randomized algorithm distributes the requests evenly to the servers. So like Round Robin, this algorithm also works well for the group of servers with similar configurations.
Here server is selected based on a unique hash key. It combines the source and destination IP address to generate a unique hash key and allocate the request to a particular server. The key can be regenerated if the session is broken and the client request is directed to the same server it was using previously. In other words, this is useful when a dropped connection needs to be returned to the same server initially handling it.
Load balancer generates the hash value based on the URL present in requests coming from the clients. Based on hash value, requests will be forwarded to servers. The load balancer caches the hashed value of the URL, and subsequent requests that use the same URL make a cache hit and are forwarded to the same server.
This algorithm considers the current load on a server and delivers better performance. Here load balancer sends requests to the server with the least number of active connections.
In weighted least connections, the load distribution is based on both the factors – the number of current and active connections to each server and the relative capacity of the server.
This algorithm is a little advanced form of the least connection method, where a request is forwarded to the server with the fewest active connections and the least average response time.
Note: The interesting thing about all this is that it might make sense to have multiple load balancers that use different server selection strategies in a system.
Enjoy learning, Enjoy system design!
Subscribe to get weekly content on data structure and algorithms, machine learning, system design and oops.