×

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

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.

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.

Halstead's Software Metrics

Halstead’s software science metrics According to Halstead’s software metrics, a program is the implementation of an algorithm. These steps are considered as a number of tokens, and tokens are classified as...

6 minutes read.

Constructive Cost Model (COCOMO)

The constructive cost model (COCOMO) is one of the most widely used software cost estimation models. This model is developed by B.W.Boehm in 1981. COCOMO model is based on LOC, i.e., the number...

5 minutes read.

Twitter - System Design

You might have heard of Twitter, a widely used social media platform. Everybody tends to check it every few minutes. Imagine what you would do if you were asked to...

8 minutes read.

RAD Model in Software Engineering

RAD Model is an incremental process model and was developed by IBM in the 1980s and described in the book of James Martin entitled Rapid Application Development. Hence, user involvement is essential from...

3 minutes read.

Iterative Model in Software Engineering

This model consists of the same phases as the waterfall model, but with fewer restrictions. Generally, the phases occur in the same order as in the waterfall model, but these may be conducted...

2 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.

Project Management Tools

Project Management Tools Risk and uncertainty increase in the size of the project, even when the project is developed according to the prescribed methods. We use project management tools to manage project management systems properly...

3 minutes read.

Software Metrics

Introduction Software metrics is the term used in software engineering. Software engineering is the detailed study of the step-by-step making of user-interactive software that fulfills users' needs. Let's discuss two terms...

4 minutes read.

Incremental Model in Software Engineering

Incremental Model is a software development process where requirements are divided into several stand-alone software development modules. In this example, each module passes through the requirement, design, development, implementation, and testing phases. That subsequent...

2 minutes read.

SDLC in Software Engineering

SDLC SDLC stands for the Software Development life cycle. It is a pictorial and diagrammatic representation of the software life cycle. A life cycle model represents all the techniques required to integrate software into the...

3 minutes read.

Some Important Quality Metrics

Important Quality Metrics The best quality of software has to pass through various software quality measures to attain standard in quality. The quality executive should consider these metrics to monitor the...

5 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.

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.

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.

Metrics for testing

There are different types of metrics used in software testing to measure different parameters. So, for different purposes, different metrics are used by the developer. For example: The type of metrics...

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.

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.

Waterfall Model in Software Engineering

The most popular model is the waterfall model. This model has five phases: Requirement analysis and specification DesignImplementation & unit testing Integration and system testing phaseOperation and maintenance These phases often occur one...

3 minutes read.