Understanding Load Balancers and Their Functions
Understanding Load Balancers and Their Functions
A load balancer is a networking device or software application that distributes and balances the
incoming traffic among the servers to provide high availability, efficient utilization of servers and
high performance.
LoadBalancer
Works as a "traffic cop" routing client requests across all servers
Ensures that no single server bears too many requests, which helps improve the performance,
reliability and availability of applications.
Highly used in cloud computing domains, data centers and large-scale web applications where traffic
flow needs to be managed.
Issues without Load Balancer?
Several problem will occur without the load balancer, these are:
without-load-balancing
Single Point of Failure: If the server goes down or something happens to the server the whole
application will be interrupted and it will become unavailable for the users for a certain period. It will
create a bad experience for users which is unacceptable for service providers.
Overloaded Servers: There will be a limitation on the number of requests that a web server can
handle. If the business grows and the number of requests increases the server will be overloaded.
Limited Scalability: Without a load balancer, adding more servers to share the traffic is complicated.
All requests are stuck with one server and adding new servers won’t automatically solve the load
issue.
Note: These are the problems that are resolved with Load Balancer.
with-load-balancing
Key characteristics of Load Balancers
Traffic Distribution: To keep any one server from becoming overburdened, load balancers divide
incoming requests evenly among several servers.
High Availability: Applications' reliability and availability are improved by load balancers, which
divide traffic among several servers. The load balancer reroutes traffic to servers that are in good
condition in the event that one fails.
Scalability: By making it simple to add servers or resources to meet growing traffic demands, load
balancers enable horizontal scaling.
Optimization: Load balancers optimize resource utilization, ensuring efficient use of server capacity
and preventing bottlenecks.
Health Monitoring: Load balancers often monitor the health of servers, directing traffic away from
servers experiencing issues or downtime.
SSL Termination: Some load balancers can handle SSL/TLS encryption and decryption, offloading
this resource-intensive task from servers.
How Load Balancer Works?
How-Load-Balancer-works
Receives Incoming Requests: When users try to access a website or application, their requests first go
to the load balancer instead of directly to a server.
Checks Server Health: The load balancer continuously monitors the status of all servers. It checks
which servers are healthy and ready to handle requests.
Distributes Traffic: Based on factors like server load, response time or proximity, the load balancer
forwards each request to the most appropriate server. This helps avoid any server getting overloaded.
Handles Server Failures: If a server goes down or becomes unresponsive, the load balancer
automatically stops sending traffic to that server and redirects it to others that are still functioning
properly.
Optimizes Performance: By spreading traffic efficiently and using healthy servers, load balancers
improve overall performance and reduce delays.
Benefits of Load Balancer
Better Performance - Distributes traffic across servers so no single server gets overloaded, reducing
downtime and improving speed.
Scalability - Works with auto-scaling to add more servers during high traffic and remove them when
traffic is low.
Failure Handling - Detects unhealthy servers and redirects traffic to healthy ones, keeping the system
available.
Prevents Bottlenecks - Handles sudden spikes in traffic smoothly by spreading requests evenly.
Efficient Resource Use - Ensures all servers share the workload fairly.
Session Persistence - Can maintain user sessions so apps that need continuous sessions (like shopping
carts) work properly
Challenges of Load Balancer
Single Point of Failure - If the load balancer itself goes down, it can disrupt traffic flow unless a
backup exists.
Cost and Complexity - Good load balancing solutions can be expensive and require proper setup and
management.
Configuration Issues - Setting up correctly can be tricky, especially for complex applications.
Extra Overhead - Adds a small delay since every request passes through the load balancer.
SSL Management - Handling encryption (SSL termination) at the balancer can make end-to-end
security more complicated.
Types of Load Balancer
Load Balancers distribute incoming network traffic across multiple servers to ensure optimal resource
utilization, minimize response time, and prevent server overload.
When it comes to load balancing, three primary types exist : software load balancers, hardware load
balancers, and virtual load balancers.
Types of Load Balancer - Based on Configurations
These load balancers are categorized according to how they are set up and managed in a system. They
define whether traffic distribution is handled by hardware, software, or cloud-based configurations.
Software Load Balancers
Software load balancers are applications or components that run on general-purpose servers. They are
implemented in software, making them flexible and adaptable to various environments.
The application chooses the first one in the list and requests data from the server.
If any failure occurs persistently (after a configurable number of retries) and the server
becomes unavailable, it discards that server and chooses the other one from the list to
continue the process.
This is one of the cheapest ways to implement load balancing.
Hardware Load Balancers
As the name suggests we use a physical appliance to distribute the traffic across the cluster of network
servers. These load balancers are also known as Layer 4-7 Routers and these are capable of handling
all kinds of HTTP, HTTPS, TCP, and UDP traffic.
Hardware load balancers are dedicated devices designed for the sole purpose of managing network
traffic. They often come as standalone appliances or modules within networking hardware.
HLBs can handle a large volume of traffic but it comes with a hefty price tag and it also has
limited flexibility.
If any of the servers don’t produce the desired response, it immediately stops sending the
traffic to the servers.
These load balancers are expensive to acquire and configure, which is the reason a lot of
service providers use them only as the first entry point for user requests.
Later the internal software load balancers are used to redirect the data behind the
infrastructure wall.
Virtual Load Balancers
A virtual load balancer is a type of load balancing solution implemented as a virtual machine (VM) or
software instance within a virtualized environment ,such as data centers utilizing virtualization
technologies like VMware, Hyper-V, or KVM. It plays a crucial role in distributing incoming network
traffic across multiple servers or resources to ensure efficient utilization of resources, improve
response times, and prevent server overload.
Types of Load Balancer - Based on Functions
These load balancers are classified by the way they manage and distribute network traffic. They
operate at different layers (network, transport, or application) to ensure efficient request handling and
high availability.
Layer 4 (L4) Load Balancer/Network Load Balancer
Layer-4 load balancers operate at the transport layer of the OSI model. They make forwarding
decisions based on information available in network layer protocols (such as IP addresses and port
numbers).
Key Features of Layer-4(L4) Load Balancer:
Transport Layer: Operates at the transport layer (TCP/UDP).
Basic Load Balancing: Distributes traffic based on IP addresses and port numbers.
Efficiency: Faster processing as it doesn’t inspect the content of the data packets.
Network Address Translation (NAT): Can perform basic NAT to hide server addresses.
Layer 7 (L7) Load Balancer/Application Load Balancer
Layer-7 load balancers operate at the application layer of the OSI model. They can make load
balancing decisions based on content, including information such as URLs, HTTP headers, or
cookies.
Key Features of Layer-7(L7) Load Balancer
Application Layer: Operates at the application layer (HTTP, HTTPS).
Content-Based Routing: Distributes traffic based on content-specific information.
Advanced Routing: Can make intelligent routing decisions based on application-specific
data.
SSL Termination: Capable of terminating SSL connections.
GSLB (Global Server Load Balancer) a.k.a. Multi-site Load Balancer
GSLB stands for Global Server Load Balancer. This type of load balancer goes beyond the
traditional local load balancing and is designed for distributing traffic across multiple data centers or
geographically distributed servers.
A GSLB load balancer is concerned with global or wide-area load balancing.
It takes into account factors such as server proximity, server health, and geographic location
to intelligently distribute traffic across multiple locations.
We need to implement a basic Round Robin load balancing algorithm. The goal is to distribute
incoming requests evenly among a list of servers. The first request goes to the first server, the
second one goes to the second server, the third request goes to the third server and it continues
further for all the requests.
Below is the implementation of the Round Robin Load Balancing Algorithm:
#include <iostream>
#include <vector>
#include <string>
class LoadBalancer {
private:
std::vector<std::string> servers;
int currentIndex;
public:
LoadBalancer(std::vector<std::string> servers) : servers(servers), currentIndex(0) {}
std::string getNextServer() {
std::string nextServer = servers[currentIndex];
currentIndex = (currentIndex + 1) % [Link]();
return nextServer;
}
};
int main() {
// Sample list of servers
std::vector<std::string> serverList = {"Server1", "Server2", "Server3"};
return 0;
}
Request 1: Routed to Server1
Request 2: Routed to Server2
Request 3: Routed to Server3
Request 4: Routed to Server1
Request 5: Routed to Server2
Request 6: Routed to Server3
Request 7: Routed to Serve...
Here is the explanation of the above code:
Load Balancer Class (LoadBalancer):
Maintain a list of server names.
Implement a method (getNextServer) that returns the next server in a round-robin fashion.
Keep track of the current index to determine the next server.
Main Class (RoundRobinExample):
Create a list of server names to be used in the load balancer.
Instantiate a LoadBalancer object with the list of servers.
Simulate a series of requests by repeatedly calling the getNextServer method.
Print the server to which each request is routed.
Main Class (RoundRobinExample):
Create a list of server names to be used in the load balancer.
Instantiate a LoadBalancer object with the list of servers.
Simulate a series of requests by repeatedly calling the getNextServer method.
Print the server to which each request is routed.
When to use Round Robin Load Balancing Algorithm
Ideal for applications where all servers have similar capacity and performance.
Works well for evenly distributed workloads, such as basic web requests.
Best suited for simple environments without complex resource needs.
Useful in setups where request order matters less than balanced distribution across
servers.
Benefits and Drawbacks of Round Robin Load Balancing Algorithm
Benefits:
Simplicity: Easy to implement and understand.
Fairness: Ensures that each server gets an equal share of the load.
Drawbacks:
Unequal Capacities: Doesn't consider the varying capacities of servers; treats all servers
equally.
Predictability: May not be optimal for scenarios with heterogeneous server capacities.
2. Weighted Round Robin Load Balancing Algorithm
The Weighted Round Robin algorithm is also a static load balancing approach which is much
similar to the round-robin technique. The only difference is, that each of the resources in a list is
provided a weighted score. Depending on the weighted score the request is distributed to these
servers.
Servers with higher weights are given a larger proportion of the requests.
The distribution is cyclic, similar to the round-robin technique, but with each server
receiving a number of requests proportional to its weight.
If a server reaches its processing capacity, it may start rejecting or queuing additional
requests, depending on the server's specific behavior.
For example:
let's say your friends have different levels of candy cravings. You want to be fair, so you give more
candies to the friend who loves them the most. Weighted Round Robin does something similar – it
gives more tasks to the friends who can handle them better.
Let's say you have three servers with weights: Server1 (weight 0.3), Server2 (weight 0.2),
and Server3 (weight 0.1). The total weight is 0.3 + 0.2 + 0.1 = 0.6. During each
cycle, Server1 would receive 0.3/0.6 (50%) of the requests, Server2 would receive 0.2/0.6
(33.33%), and Server3 would receive 0.1/0.6 (16.67%).
Below is the implementation of the Weighted Round Robin Load Balancing Algorithm:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
class Server {
public:
std::string name;
int weight;
class WeightedRoundRobinBalancer {
private:
std::vector<Server> servers;
std::vector<int> cumulativeWeights;
int totalWeight;
int currentIndex;
Server getNextServer() {
int randomValue = rand() % totalWeight;
for (size_t i = 0; i < [Link](); i++) {
if (randomValue < cumulativeWeights[i]) {
currentIndex = i;
break;
}
}
return servers[currentIndex];
}
};
int main() {
// Sample list of servers with weights
std::vector<Server> serverList = {
Server("Server1", 3),
Server("Server2", 2),
Server("Server3", 1)
};
We need to implement a load balancing algorithm that distributes incoming requests across a set of
servers based on the hash of the source IP address. The goal is to ensure that requests coming from
the same source IP address are consistently routed to the same server.
Below is the implementation of the Source IP Hash Load Balancing Algorithms:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <cstdlib>
class SourceIpHashLoadBalancer {
private:
std::unordered_map<std::string, std::string> ipToServerMap;
public:
SourceIpHashLoadBalancer() {}
int main() {
// Create a source IP hash load balancer
SourceIpHashLoadBalancer loadBalancer;
return 0;
}
We need to implement a Load Balancing Algorithm that distribute incoming requests across a set of
servers and should aim to minimize the number of active connections on each server by directing
new requests to the server with the fewest active connections. This ensures a balanced distribution
of the workload and prevents overload on individual servers.
Below is the implementation of the Least Connection Method Load Balancing Algorithms:
#include <map>
#include <string>
#include <limits>
#include <iostream>
class LeastConnectionLoadBalancer {
private:
std::map<std::string, int> serverConnections;
public:
LeastConnectionLoadBalancer() {}
std::string getServerWithLeastConnections() {
int minConnections = std::numeric_limits<int>::max();
std::string selectedServer;
if (![Link]()) {
serverConnections[selectedServer] = minConnections + 1;
}
return selectedServer;
}
};
int main() {
LeastConnectionLoadBalancer loadBalancer;
[Link]("Server1");
[Link]("Server2");
[Link]("Server3");
class LeastResponseLoadBalancer {
private:
std::map<std::string, long long> serverResponseTimes;
public:
LeastResponseLoadBalancer() {}
std::string getServerWithLeastResponseTime() {
long long minResponseTime = std::numeric_limits<long long>::max();
std::string selectedServer;
if (![Link]()) {
serverResponseTimes[selectedServer] = minResponseTime + 1;
}
return selectedServer;
}
};
int main() {
LeastResponseLoadBalancer loadBalancer;
[Link]("Server1");
[Link]("Server2");
[Link]("Server3");
return 0;
}
To implement Resource-Based Load Balancing, you need to track each server’s current resources,
then route each new request to the server that can handle it best based on real-time data. Here’s an
example implementation of a Resource-Based Load Balancing Algorithm. This simple
implementation uses CPU load as a metric to decide which server should handle each request.
#include <iostream>
#include <map>
#include <limits>
class Server {
public:
std::string name;
double cpuLoad; // Represents the current CPU load percentage of the server
Server(std::string name) {
this->name = name;
this->cpuLoad = 0.0;
}
std::string getName() {
return this->name;
}
};
class ResourceBasedLoadBalancer {
private:
std::map<std::string, Server> servers;
public:
ResourceBasedLoadBalancer() {}
// Finds the server with the lowest CPU load and assigns the request to it
Server getServerWithMostResources() {
Server bestServer;
double lowestLoad = std::numeric_limits<double>::max();
[Link](server1);
[Link](server2);
[Link](server3);
Output
Routing request to server: Server3 with current CPU load: 20.0%
Below is the explanation of the above code:
Server Class:
Represents a server with attributes like name and cpuLoad. The updateCpuLoad method
updates the CPU load to simulate real-time monitoring.
ResourceBasedLoadBalancer Class:
Manages servers and routes requests based on available resources. addServer adds a
server. getServerWithMostResources finds the server with the lowest CPU
load. handleRequest routes requests to the server with the most available resources..
ResourceBasedLoadBalancerExample Class:
Sets up the load balancer, adds servers, updates CPU loads, and simulates routing
requests.
When to Use Resource-Based Load Balancing Algorithm?
Useful for applications that perform CPU-intensive or memory-heavy tasks.
Works well when servers have different resource levels, as the algorithm adapts to each
server’s real-time capacity.
Ensures availability by routing requests to the least overloaded servers, reducing
downtime risks.
Benefits and Drawbacks of Resource-Based Load Balancing Algorithm
Benefits:
Resource Optimization: Balances workloads based on real-time resource data,
improving system efficiency.
Adaptability: Adjusts dynamically to the current state of each server’s resources.
Drawbacks:
Complex Implementation: Requires continuous monitoring of server resources, which
can add complexity.
Higher Overhead: Real-time tracking of resources may consume additional system
resources.