Routing Requests through Load Balancers

Let us first understand what load balancers are

Assume a system works with multiple machines or servers, and as the requests arrive, they need to be routed to any one of the servers so that no machine is overloaded and every server gets an equal amount of requests. The device that is used to do the said work is known as Load Balancer. A load balancer is just a layer between the incoming requests from the clients and the network of machines used in the system.

One should design the system in such a way that no single server gets overloaded when other machines are idle. Numerous algorithms are designed to ensure the even distribution of user traffic.

Let us now look at the Hashing Algorithm that can be used to direct the user traffic towards the multiple servers evenly.

Let us suppose that the number of total servers or machines in the system is assigned to the variable server_count, and a load_balancer function is used to distribute the traffic uniformly on the system.

Whenever a user interacts with the system and makes a request, the request Id is stored in a variable named reqID. Before the request reaches any server, it encounters the load_balancer placed at the mid layer. The load balancer then diverts the request to its respective server.

To decide the destination server for each unique request. The Hashing approach is used to direct the requests.

Let us understand the implementation of this approach and then discuss the key points to remember about this algorithm.

Java Code for the implementation of Load Balancer:

class Main{
    public static int hash_function(int reqID)
        // hash the received reqID with any appropriate hashing technique 
        // for the sake of clear understanding, we have skipped writing the hashing algorithm
        // instead, we have hard-coded a value to demonstrate the load balancing approach
        int hashed_id = 112;
        return hashed_id;
    public static void routeRequest(int destination_server)
        System.out.println( " The recived request will be routed to the serverId: "+ destination_server ) ;
    public static int reqID = 32; // Request ID for the incoming user interaction
    public static int server_count = 10; // Let us assume the total online machines in the system are 10
    public static void main(String args[])
        int hashedId = hash_function (reqID) ;
        // Call the hash function on reqID
        int destination_server = hashedId % server_count ;
        // Destination Server is computed by finding the modulus between hashedID and total numbers of servers
        // Call the route request method to direct the given req towards the destination server

Now, try to comprehend the basic idea behind this technique.

Identifiers used in the algorithm:

  • reqID: ID for every request that needs to be sent to any server.
  • hash_function: method to calculate the hash value for each reqID received evenly.
  • hashedID: Unique Id for each request, received after hashing the reqID.
  • server_count: Total number of machines in the system
  • destination_server: ID of the server where the request would be routed.

Let the request we received have the reqId =32 

When the load balancer encounters the user request, it diverts it towards the hash function to receive the hashedID assume that the hashedId be equal to 112.

To know which server should be used to entertain the user request, we find the modulus between the hashedID and the server count. The result from the same would be the ID of the server where the request would be routed.

Here in this example, server_count =10, and the hashedID = 112 hence the destination server would be = 2.

You can even optimise the load balancing by checking the current traffic on each server and prioritising the server with the least number of active requests.

This is an optimised approach for implementing Load Balancing in your system. This approach is flexible and lets you increase or decrease the number of active servers in your system. The only problem that may arise is when we would be storing the cache of requests on the destination server itself, and on updating the server_count, the destination server changes, and the cache may no longer be accessible; apart from this, Hashing bad load balancing is the best way to route requests evenly.