Load Balancer

Why do we need Network Load Balancer?

Let’s say you have two computers running some service. All the user requests are being processed by 1st computer and not by 2nd computer. Assuming requests can increase significantly, causing 1st computer to stuck and leading to downtime. But wait why can’t we use 2nd computer? That is when the idea of a Load Balancer comes into the picture.

What is a Load Balancer?

A load balancer can be a device or software application that distributes network traffic(user requests) across multiple servers or resources so that no single server or resource is overburdened with heavy traffic. For instance, if a machine is not working as expected then the load balancer can direct the traffic to another machine, thereby reducing the chances of system downtime. So, it is one of the crucial components of a system design.

load balancer
Load Balancing

How does Load Balancer work?

  1. A load balancer resides between the client and the server. When a client requests a resource, such as a webpage, the request is sent to the load balancer.
  2. The load balancer estimates the best-suited server for a request based on predefined rules such as server capacity, response time, location, current server load, or server health.
  3. Once a server is selected, the load balancer forwards the request to that server. It processes the request and sends the response back to the load balancer.
  4. The load balancer then sends the response back to the client.
  5. If the load balancer detects that a server is not responding or is down(by performing certain health checks), it automatically redirects the traffic to a healthy server.

The load balancing technique is commonly used where traffic can fluctuate rapidly and unpredictably. By using load balancers, a system can scale horizontally by adding more resources as needed to handle increased traffic, rather than relying on a single server to handle all traffic.

Load balancing helps to increase the availability and scalability of a system by distributing the load evenly across multiple resources.

To sum up, the network load balancer’s crucial functions are

  • Distributes the traffic evenly across the nodes of a cluster
  • Ensures high availability and reliability by sending requests to the server which are online(i.e, functioning well)
  • Provides flexibility to add additional machines by scaling a system horizontally.

Why different Algorithms are used in a load balancer?

Load Balancing algorithms help in the
1. Even distribution of workload
2. Fault tolerance – Detects and redirects traffic to a healthier server.
3. Scalability – Algorithms are designed to handle large traffic and scale up or down based on demand.
4. Customisation – Different load-balancing algorithms are designed to optimize different aspects of a system, such as throughput, response time, or fairness.

Load Balancing Algorithms

  • Round Robin
    In this algorithm, requests are distributed equally to a group of servers in a cyclic manner. Each server receives an equal number of requests, one at a time.

    Round Robin is simple and easy to implement, and it works well for applications with a relatively uniform workload and server capacity.
  • Least Connections
    Requests are directed to the server possessing the least number of active connections. It helps in distributing traffic evenly and avoids overloading a single machine.

    Least Connections is suitable for applications with long-lived connections, as it ensures that new connections are routed to the least loaded server.
  • Least Time
    It directs the request to a server having a minimum average response time. It improves overall performance and reduces latency.

    This algorithm can be implemented in real-time applications, and system requiring high availability and faster response time.
  • IP Hash
    A hash value is calculated ( using some function f(IPAddress) -> returns a value, i.e, server number ) and as per the value, the request is directed to some particular server.

    This can be useful in scenarios where clients need to maintain a persistent connection, as it ensures that all requests from the same client are routed to the same server.
  • Weighted Round Robin
    This algorithm is similar to Round Robin, but it assigns a weight to each server based on its processing power or other criteria. This allows more traffic to be sent to more powerful servers.
  • Random
    A request is randomly assigned to a server. It is generally implemented in cases when all the resources (servers) have the same computational power.

    A random algorithm is useful for applications with a high degree of randomness in their workload or for scenarios where a simple load-balancing algorithm is needed.

How to decide on which load-balancing algorithm to use?

The choice of load balancing depends on specific needs and system requirements. You need to carefully consider tradeoffs between algorithms and if possible test them in a real environment and select the best-suited one.