×

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
  
        routeRequest(destination_server);
        // 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.


Related Topics

Dropbox - System Design

While trying to upload or share any file, whether images, pdfs or documents, you would have come across the dropbox file posting service. Dropbox keeps all your data secure and...

9 minutes read.

Caching in System Design

While surfing the web, you have usually wondered why the sites you have visited once load faster than the new ones. You would have also noticed how the texts and labels...

10 minutes read.

Online Library Management System ER Diagram in Software Engineering

Description of an ER Diagram The relationship between entity sets that are kept in a database is depicted in a diagram known as an entity relationship diagram, or ER Diagram. Alternatively,...

10 minutes read.

Umbrella Activities in Software Engineering

The various stages of software engineering are tied to one another. Various software process models have different ways of expressing or making these processes available. To manage the development, quality,...

3 minutes read.

Software Quality Assurance (SQA)

Software Quality Assurance (SQA) is a process that ensures the quality of software while developing software. It also ensures that all processes, procedures, and activities are monitored and properly implemented. It works along...

3 minutes read.

Object Oriented Metrices in Software Engineering

Metrics are used to measure the quality of software on different parameters. It provides acomputed accurate estimation of projects that help in developing software with minimal fault. “Metric is a set...

7 minutes read.

Software Engineering Tutorial

The software has become critical to advancement in almost all areas of human endeavor. The art of programming is no longer enough to construct large programs. There are serious problems with the...

8 minutes read.

Project Management

Project Management What is a software project? The project is a set of tasks that need to be completed to reach a clear result. The project is also defined as the set of inputs...

2 minutes read.

Information Flow Metrics

This metric is purely based on information. Information flow metric is different from size metric, and data structure metric as size metric is based on the final size of program...

4 minutes read.

Software Project Planning

Project planning is a management process that focuses on the activities required to complete a project successfully. Project planning helps to prevent problems that occur during the project development, such as a...

2 minutes read.

Data Structure Metrics

In the size metric, we have learned to measure/ determine the size of a program by various software metrics such as line of code, Function count, and Token Count. A program...

6 minutes read.

Structured Analysis and Design (SA/SD)

When the software systems keep getting bigger and more complex, you need a way to represent the model systematically. Structured Design and Structured Analysis, often referred to as SA/SD, is...

4 minutes read.

Load Balancer – System Design

As software engineering students or IT enthusiasts, most of us have wondered how these huge IT applications handle the millions of simultaneous requests on their servers. The sheer hard work...

9 minutes read.

Software Configuration Management

Software Configuration Management The process of Software Configuration Management contains operations such as controlling, managing, and organizing the source-code, documents, and other essential components during the Software Development Life Cycle.SCM is essential for all project...

3 minutes read.

Spiral Model

The problem with traditional software process models is that they do not adequately deal with uncertainty, which is inherent to software projects. Major software projects had failed because project risks were neglected, and...

3 minutes read.

Project management activities

Project management activities Software project management involves many activities, including project planning, software product scope, cost estimation in various terms, scheduling of tasks and events, and resource management. Project management activities may include: Project PlanningScope ManagementProject...

2 minutes read.

Scope of software metrics

Scope, Benefit, and Cycle of Software Metrics Software metrics have tremendous scope in software development to understand and fulfill various aspects of the project process. Software metrics help in estimation of...

6 minutes read.

Big-Bang Model in Software Engineering

In the Big-Bang Model, developers do not adopt any specific process. Development begins only with the necessary funds and efforts as input, and output is developed software that may or may not be...

1 minute read.

Project Monitoring and Control

Project Monitoring and Control Project monitoring and control process includes procedures which are performed to observe project performance so that potential problems are identified, and appropriate action can be taken to meet the desired performance...

3 minutes read.

What is System Design

System design is simply the systematic process used to design the basic architecture of an electronic control system by defining essential elements like modules or components and their interfaces. This...

3 minutes read.