Load Balancing Algorithms that can be used in Java Applications

Srikanth Dannarapu
Javarevisited
Published in
8 min readMar 8, 2023

There are several load balancing algorithms that can be used in Java applications, some of which are:

Round Robin Algorithm:

This algorithm distributes requests equally across a group of servers in a cyclic manner. Each request is assigned to the next available server in the sequence.

The Round Robin Algorithm works by maintaining a list of servers in a circular order. When a request comes in, the algorithm selects the next server in the list and assigns the request to it.

The algorithm keeps track of the last server that was selected, and the next request is assigned to the server immediately following it in the sequence. When the end of the list is reached, the algorithm starts again at the beginning of the list.

For example, consider a scenario where there are three servers in a load balancing pool, labeled as Server1, Server2, and Server3, respectively. A client sends a request to the load balancer, which distributes the request to Server1. The next request will be sent to Server2, and the third request will be sent to Server3. The fourth request will then be sent back to Server1, and the cycle will continue.

The Round Robin Algorithm is simple to implement and ensures that requests are distributed equally across all servers in the pool. It also helps to prevent any one server from becoming overloaded with requests. However, it does not take into account the current load on each server, which can result in some servers receiving more requests than others. Therefore, it is not always the most effective load balancing algorithm for all scenarios.

In summary, the Round Robin Algorithm is a widely used and effective load balancing technique that helps distribute the load evenly across multiple servers in a pool. It is easy to implement, but it may not be suitable for all scenarios.

Least Connection Algorithm:

This algorithm assigns requests to the server with the fewest active connections. This helps to distribute the load evenly across the servers and prevent overload.

Here is an example of how the Least Connection Algorithm works:

Least Connection Algorithm

Suppose that there are three servers in a load balancing pool, labeled as Server1, Server2, and Server3, respectively. Each server has a different number of active connections at any given time, as shown below:

  • Server1 has 4 active connections
  • Server2 has 2 active connections
  • Server3 has 1 active connection

A new request comes in, and the load balancer checks the number of active connections on each server. The Least Connection Algorithm selects Server3, since it has the fewest active connections at the moment. The new request is sent to Server3, which processes it and returns a response.

Another request comes in a few seconds later. This time, the Least Connection Algorithm selects Server2, since it now has the fewest active connections. The request is sent to Server2, which processes it and returns a response.

This process continues for each new request that comes in. As servers receive requests and process them, their number of active connections increases. The load balancer continuously monitors the number of active connections on each server and selects the server with the least number of active connections for each new request.

The benefit of the Least Connection Algorithm is that it helps to ensure that each server in the pool is being utilized evenly, without any one server becoming overloaded with too many connections. This algorithm can be especially useful in scenarios where certain requests may require more resources from the server than others, or where the load on each server may vary at different times of the day.

IP Hash Algorithm:

This algorithm maps the client IP address to a specific server. Requests from the same client IP address are always directed to the same server. This can be useful in scenarios where sessions need to be maintained on a particular server.

This Algorithm is a load balancing technique that uses the source IP address of a client to determine which server in a pool should handle the client’s request. The algorithm hashes the IP address of the client and maps it to a specific server in the pool based on the hash value. This approach ensures that each client will be consistently routed to the same server for subsequent requests, which can be useful in scenarios where session data or other context needs to be maintained across multiple requests.

Below is a simple example of how the IP Hash Algorithm works:

IP Hash Algorithm

Suppose there are four servers in a load balancing pool, labeled as Server1, Server2, Server3, and Server4, respectively. A new client with an IP address of “192.168.1.100” sends a request to the load balancer. The IP Hash Algorithm hashes the IP address “192.168.1.100” and maps it to a specific server in the pool based on the hash value. Let’s say the hash function returns a value of 2, indicating that the request should be sent to Server2.

The load balancer then forwards the request to Server2, which processes it and sends a response back to the client. If the same client sends another request later, the IP Hash Algorithm will again hash the IP address “192.168.1.100” and map it to Server2, since that server was previously selected based on the client’s IP address.

If a different client with a different IP address sends a request to the load balancer, the IP Hash Algorithm will hash that client’s IP address and map it to a different server in the pool based on the hash value. For example, if the new client has an IP address of “192.168.1.200” and the hash function returns a value of 4, the request will be sent to Server4.

The benefit of the IP Hash Algorithm is that it ensures that each client will be consistently routed to the same server for subsequent requests, which can be useful in scenarios where session data or other context needs to be maintained across multiple requests. However, it’s important to note that this algorithm can lead to uneven load distribution if some clients have IP addresses that consistently hash to the same server. In such cases, other load balancing algorithms may be more appropriate.

Weighted Round Robin Algorithm:

The Weighted Round Robin (WRR) Algorithm is a load balancing technique that distributes traffic across a pool of servers based on assigned weights. In this algorithm, each server is assigned a weight value that corresponds to its processing capacity, with higher-weighted servers handling more requests than lower-weighted servers. The algorithm works by assigning each server a “token” that represents a unit of work, and then cycling through the tokens in a round-robin fashion, with servers receiving tokens based on their assigned weights.

below is a simple example of how the Weighted Round Robin Algorithm works:

Suppose there are three servers in a load balancing pool, labeled as Server1, Server2, and Server3, respectively. Each server is assigned a weight value that corresponds to its processing capacity, with Server1 having a weight of 3, Server2 having a weight of 2, and Server3 having a weight of 1.

The algorithm starts by assigning each server a number of tokens proportional to its weight. In this case, Server1 gets 3 tokens, Server2 gets 2 tokens, and Server3 gets 1 token. These tokens represent units of work that the servers will process.

The load balancer then cycles through the tokens in a round-robin fashion, with each server receiving a token based on its weight. In this example, the first token is assigned to Server1, the second token is assigned to Server2, the third token is assigned to Server3, the fourth token is assigned to Server1 again, and so on. This process continues until all tokens have been processed.

The benefit of the Weighted Round Robin Algorithm is that it can ensure that requests are distributed more evenly across servers based on their processing capacity, which can help avoid overloading certain servers while leaving others underutilized. In the example above, Server1 will handle three times as many requests as Server3, since it has three times the weight. Meanwhile, Server2 will handle twice as many requests as Server3, since it has twice the weight.

below table that illustrates the distribution of requests across the servers in the above example:

Weighted Round Robin

In this example, Server1 handles 8 out of 15 requests, Server2 handles 4 out of 15 requests, and Server3 handles 3 out of 15 requests, which corresponds to their assigned weights.

Least Response Time Algorithm:

The Least Response Time (LRT) Algorithm is a load balancing technique that distributes traffic across a pool of servers based on their response time. The algorithm works by continuously monitoring the response times of each server in the pool and directing traffic to the server with the lowest response time.

below is a simple example of how the Least Response Time Algorithm works:

Suppose there are three servers in a load balancing pool, labeled as Server1, Server2, and Server3, respectively. Each server is initially idle and has no requests to process.

A client sends a request to the load balancer, which then selects the server with the lowest response time to handle the request. Since all servers are currently idle, the load balancer selects Server1 to handle the request.

Server1 receives the request, processes it, and sends a response back to the client. The load balancer records the response time of Server1 and updates its internal statistics.

Another client sends a request to the load balancer. Since Server1 has just handled a request and may still be busy processing it, the load balancer decides to select the server with the next lowest response time, which is Server2.

Server2 receives the request, processes it, and sends a response back to the client. The load balancer records the response time of Server2 and updates its internal statistics.

The process continues as more requests are sent to the load balancer. The load balancer continuously monitors the response times of all servers in the pool and directs traffic to the server with the lowest response time.

The benefit of the Least Response Time Algorithm is that it can ensure that requests are directed to the server with the lowest response time, which can help improve overall performance and reduce latency. However, the algorithm requires continuous monitoring of server response times and may not be suitable for all applications.

Overall, the Least Response Time Algorithm can be a useful technique for load balancing in scenarios where response time is a critical factor, such as in high-traffic web applications or real-time systems.

Conclusion:

Each algorithm has its own advantages and disadvantages, and the choice of algorithm will depend on the specific needs of the system being load balanced. For example, the Round Robin Algorithm is useful when servers are similar in terms of processing power and resources, while the Least Connection Algorithm may be more suitable when servers have varying processing power and capacity.

Overall, load balancing algorithms are essential for ensuring high availability, reliability, and performance in distributed systems that handle a large volume of traffic.

Thanks, before you go:

  • 👏 Please clap for the story and follow the author 👉
  • Please share your questions or insights in the comments section below. Let’s help each other and become better Java developers.
  • Let’s connect on LinkedIn

--

--