What is a Load Balancer?

Anshuman Pattnaik
5 min readSep 27, 2022

Introduction

Load balancing is a technique to distribute tasks and resources across multiple servers and is generally used to improve the performance of the web application, databases and other services. The load balancer keeps track of the health status of each resource while broadcasting requests to each server. If a server cannot respond or raises an error rate, then Load Balancer stops sending traffic to that server.

A load balancer acts as a reverse proxy and sits between client and server, receives incoming traffic and distributes the traffic across clusters of servers using various industry standard load balancing algorithms. Load balancing is an efficient technique to reduce the load of an individual server, improve the application availability, and mitigate the application server from becoming a single point of failure.

Load Balancer Architecture

Load balancers are typically grouped into two types: Layer 4 and 7. Layer 4 load balancer uses only a TCP connection from a client to the server, while Layer 7 load balancer uses two TCP connections from the client to the server. Layer 7 load balances are based on the data’s content, and Layer 4 load balances are based on its inbuilt software algorithm. Layer 7 is an excellent choice for microservices. However, Layer 4 does not apply to microservices.

Load balancers traffic

There are four main types of traffic the administrators create as forwarding rules.

  1. HTTP
  2. HTTPS
  3. TCP
  4. UDP

1. HTTP

The Load Balancer sets the X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-Port headers to provide the backends information regarding the initial request.

2. HTTPS

Standard HTTPS balances load using encryption, and it is handled in two techniques with SSL passthrough or an SSL termination.

2.1 SSL passthrough

SSL passthrough is the act of handing data via a load balancer to a server without decrypting. The data is passed along to a web server as a simple HTTP, and the decryption or SSL termination occurs at the load balancer.

2.2 SSL termination

SSL termination happens at the load balancer because the decryption process is a very CPU-intensive task. If the load balancer takes the load for decryption, the server spends time processing power on application tasks that helps improve the application performance.

3. TCP

A TCP load balancer uses a transmission control protocol which operates at layer 4, and it’s used to route the traffic to a database cluster distributed across all of the servers.

4. UDP

A UDP load balancer operates at layer 4 in the OSI model and recently added support to core internet protocols such as DNS and syslogd.

Backend Server Health Checks

Healthy backend servers consistently receive incoming traffic from the load balancers. The Load balancer regularly connects with the backend server and checks the health status. If a server fails on a health check and cannot respond to the load balancer, it automatically stops forwarding the traffic to the corresponding server.

Load Balancing Algorithms

A Load balancer uses load balancing algorithms to distribute network traffic between servers. There are two types of load balancing algorithms:

  1. Dynamic load balancing algorithms
  2. Static load balancing algorithms

Dynamic load balancing algorithms

Dynamic load balancing algorithms use each server’s current state and distribute the traffic accordingly. It is divided into four different types.

  1. Least connection
  2. Weighted least connection
  3. Weighted response time
  4. Resource-based

1. Least connection

Load balancers check which server has the fewest connections open and send traffic to those servers, which means all the connections require equal processing power.

2. Weighted least connection

It gives the privilege to the administrator to assign different weights to each server by the assumption that some servers can handle more connections.

3. Weighted response time

The average response time of each server and the number of open connections specify where to send traffic. The algorithm sends traffic to the server with the fastest response time, ensuring quicker service to the end users.

4. Resource-based

Specialized software called agent runs on each server that measures the server’s available CPU and memory. The load balancers query the agent to check what resources are available on each server while distributing the traffic to that server.

Static load balancing algorithms

Static load balancing algorithms send an equal amount of traffic to each server; either it can be random or in a specified order. It is divided into three different types.

  1. Round robin
  2. Weighted round-robin
  3. IP hash

1. Round robin

Round robin load balancing algorithm distributes traffic to clusters of servers in a cycle using DNS. The load balancer selects the first server from the list and then moves down to the second until it reaches the end.

2. Weighted round-robin

It gives the privilege to the administrators to assign different weights to each server so that the servers can handle more traffic. Using DNS records can configure the weighting.

3. IP hash

The combination of incoming traffic’s source and destination IP addresses generates a hash using a mathematical function. The connection is assigned to a specific server by using the generated hash.

How to handle Load Balancers failure?

There are many ways a load balancer can fail within a system. If one load balancer fails, the second load balancer should connect immediately and can become active. The two load balancers can be connected to form a cluster, where each can monitor the status of the other’s heartbeat; each load balancer has equal capability to detect failure and recovery.

Load balancing in a cluster

When the primary load balancer fails, the DNS should be able to route the application to the second load balancer. But to make changes to the DNS, It will take a minimum of 24 hours to reflect on the internet. To make this process automatic, system administrators use systems like Reserved IPs that allow for elastic IP address mapping. The IP address with the domain name remains the same but is moved between servers.

Conclusion

I hope you enjoyed reading this article, which gave you an insight into Load Balancers. If you think this article helped you in anyways then feel free to share it with your friends. If you think of anything I’ve missed, let me know with a comment below.

--

--

Python | Application Security | Web Security | Cybersecurity | Software Development