Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
How does a Load Balancer Works?
Next article icon

Load Balancing Algorithms

Last Updated : 11 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To control traffic across servers in a network, load-balancing algorithms are important. By spreading requests evenly, load balancers make sure that no single server is overloaded when several people visit an application. Various techniques, such as IP hash, Least Connections, and Round Robin, are employed, depending on the traffic patterns and application type.

lba

Table of Content

  • What are Load Balancing Algorithms?
  • Static Load Balancing Algorithms
    • Round Robin Load Balancing Algorithm
    • Weighted Round Robin Load Balancing Algorithm
    • Source IP Hash Load Balancing Algorithm
  • Dynamic Load Balancing Algorithms
    • Least Connection Method Load Balancing Algorithm
    • Least Response Time Method Load Balancing Algorithm
    • Resource-based Load Balancing Algorithm

What are Load Balancing Algorithms?

Algorithms for load balancing are strategies for effectively allocating workloads among several servers or resources. Consider that you have a group of employees and numerous jobs to finish. To keep everyone active and complete the work efficiently, you should split the tasks evenly rather than assigning one person to perform all the work while others sit around doing nothing. In computing, load balancing accomplishes exactly that.

Load balancing algorithms can be broadly categorized into two types: Dynamic load balancing and Static load balancing.

llba

Static Load Balancing Algorithms

Static load balancing involves predetermined assignment of tasks or resources without considering real-time variations in the system. This approach relies on a fixed allocation of workloads to servers or resources, and it doesn’t adapt to changes during runtime.

Types of Static Load Balancing Algorithms are:

1. Round Robin Load Balancing Algorithm

The Round Robin algorithm is a simple static load balancing approach in which requests are distributed across the servers in a sequential or rotational manner. It is easy to implement but it doesn’t consider the load already on a server so there is a risk that one of the servers receives a lot of requests and becomes overloaded.

For example:

Lets say you have a group of friends, and you want to share a bag of candies equally with all of them. You give one candy to each friend in a circle, and then you start over. This is like Round Robin – making sure everyone gets a fair share.

Round-Robin-(1)

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:

Java
import java.util.ArrayList; import java.util.List;  class LoadBalancer {     private List<String> servers;     private int currentIndex;      public LoadBalancer(List<String> servers) {         this.servers = new ArrayList<>(servers);         this.currentIndex = 0;     }      public String getNextServer() {         String nextServer = servers.get(currentIndex);         currentIndex = (currentIndex + 1) % servers.size();         return nextServer;     } }  public class RoundRobinExample {     public static void main(String[] args) {         // Sample list of servers         List<String> serverList = new ArrayList<>();         serverList.add("Server1");         serverList.add("Server2");         serverList.add("Server3");          // Create a load balancer with the server list         LoadBalancer loadBalancer = new LoadBalancer(serverList);          // Simulate requests to the load balancer         for (int i = 0; i < 10; i++) {             String nextServer = loadBalancer.getNextServer();             System.out.println("Request " + (i + 1) + ": Routed to " + nextServer);         }     } }      
Output
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.

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%).

Weighted-Round-Robin-(1)

Below is the implementation of the Weighted Round Robin Load Balancing Algorithm:

Java
import java.util.ArrayList; import java.util.List; import java.util.Random;  class WeightedRoundRobinBalancer {     private List<Server> servers;     private int[] cumulativeWeights;     private int totalWeight;     private int currentIndex;     private Random random;      public WeightedRoundRobinBalancer(List<Server> servers) {         this.servers = new ArrayList<>(servers);         this.totalWeight = calculateTotalWeight(servers);         this.cumulativeWeights = calculateCumulativeWeights(servers);         this.currentIndex = 0;         this.random = new Random();     }      private int calculateTotalWeight(List<Server> servers) {         int totalWeight = 0;         for (Server server : servers) {             totalWeight += server.getWeight();         }         return totalWeight;     }      private int[] calculateCumulativeWeights(List<Server> servers) {         int[] cumulativeWeights = new int[servers.size()];         cumulativeWeights[0] = servers.get(0).getWeight();         for (int i = 1; i < servers.size(); i++) {             cumulativeWeights[i] = cumulativeWeights[i - 1] + servers.get(i).getWeight();         }         return cumulativeWeights;     }      public Server getNextServer() {         int randomValue = random.nextInt(totalWeight);         for (int i = 0; i < cumulativeWeights.length; i++) {             if (randomValue < cumulativeWeights[i]) {                 currentIndex = i;                 break;             }         }         return servers.get(currentIndex);     }      // Inner class representing a server with a weight     static class Server {         private String name;         private int weight;          public Server(String name, int weight) {             this.name = name;             this.weight = weight;         }          public String getName() {             return name;         }          public int getWeight() {             return weight;         }     } }  public class WeightedRoundRobinExample {     public static void main(String[] args) {         // Sample list of servers with weights         List<WeightedRoundRobinBalancer.Server> serverList = new ArrayList<>();         serverList.add(new WeightedRoundRobinBalancer.Server("Server1", 3));         serverList.add(new WeightedRoundRobinBalancer.Server("Server2", 2));         serverList.add(new WeightedRoundRobinBalancer.Server("Server3", 1));          // Create a weighted round-robin load balancer with the server list         WeightedRoundRobinBalancer balancer = new WeightedRoundRobinBalancer(serverList);          // Simulate requests to the load balancer         for (int i = 0; i < 10; i++) {             WeightedRoundRobinBalancer.Server nextServer = balancer.getNextServer();             System.out.println("Request " + (i + 1) + ": Routed to " + nextServer.getName());         }     } } 
Output
Request 1: Routed to Server1 Request 2: Routed to Server2 Request 3: Routed to Server2 Request 4: Routed to Server3 Request 5: Routed to Server3 Request 6: Routed to Server3 Request 7: Routed to Serve... 

Here is the explanation of the above code:

  • WeightedRoundRobinBalancer Class:
    • Manages a list of servers with weights.
    • Calculates total and cumulative weights during initialization.
    • Provides a method (getNextServer) to retrieve the next server based on weighted round-robin.
    • Contains an inner class (Server) representing a server with a name and weight.
  • WeightedRoundRobinExample Class:
    • Demonstrates the usage of the WeightedRoundRobinBalancer.
    • Creates a sample list of servers with weights.
    • Instantiates a WeightedRoundRobinBalancer object.
    • Simulates requests and prints the server to which each request is routed.

When to use Weighted Round Robin Load Balancing Algorithm?

  • When servers have different capacities or performance levels.
  • Ideal for environments where servers vary in resources (CPU, memory, etc.).
  • Useful when you want to maximize resource utilization across all servers.
  • Helps in preventing smaller servers from overloading while efficiently using larger servers.

Benefits and Drawbacks of Weighted Round Robin Load Balancing Algorithm

  • Benefits:
    • Capacity Consideration: Accounts for different server capacities by assigning weights.
    • Flexibility: Can be adjusted to handle varying workloads effectively.
  • Drawbacks:
    • Complexity: More complex than simple Round Robin.
    • Maintenance: Requires adjusting weights as server capacities change.

3. Source IP Hash Load Balancing Algorithm

The Source IP Hash Load Balancing Algorithm is a method used in network load balancing to distribute incoming requests among a set of servers based on the hash value of the source IP address. This algorithm aims to ensure that requests originating from the same source IP address are consistently directed to the same server.

If the load balancer is configured for session persistence, it ensures that subsequent requests from the same source IP address are consistently directed to the same server. This is beneficial for applications that require maintaining session information or state.

For example:

Think of your friends coming to your house, and you want to remember who gets which toy every time they visit. IP Hash is like remembering which friend played with which toy last time, so you always give them the same one.

Source-IP-hash--(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:

Java
import java.util.HashMap; import java.util.Map;  class SourceIpHashLoadBalancer {     private Map<String, String> ipToServerMap;      public SourceIpHashLoadBalancer() {         this.ipToServerMap = new HashMap<>();     }      public void addServer(String serverName) {         // Add server to the mapping         ipToServerMap.put(serverName, serverName);     }      public String getServerForIp(String sourceIp) {         // Calculate hash of the source IP         int hash = sourceIp.hashCode();          // Get the list of available servers         String[] servers = ipToServerMap.keySet().toArray(new String[0]);          // Map the hash value to a server index         int serverIndex = Math.abs(hash) % servers.length;          // Return the selected server         return servers[serverIndex];     } }  public class SourceIpHashLoadBalancerExample {     public static void main(String[] args) {         // Create a source IP hash load balancer         SourceIpHashLoadBalancer loadBalancer = new SourceIpHashLoadBalancer();          // Add servers to the load balancer         loadBalancer.addServer("Server1");         loadBalancer.addServer("Server2");         loadBalancer.addServer("Server3");          // Simulate requests with different source IPs         String[] sourceIps = {"192.168.1.1", "10.0.0.1", "172.16.0.1"};          for (String sourceIp : sourceIps) {             String selectedServer = loadBalancer.getServerForIp(sourceIp);             System.out.println("Request from " + sourceIp + " routed to " + selectedServer);         }     } } 
Output
Request from 192.168.1.1 routed to Server2 Request from 10.0.0.1 routed to Server3 Request from 172.16.0.1 routed to Server1 

Here is the explanation of the above code:

  • SourceIpHashLoadBalancer Class:
    • Fields:
      • ipToServerMap: A mapping of server names to server names (used for consistent hashing).
    • Methods:
      • addServer: Adds a server to the load balancer.
      • getServerForIp: Calculates the hash of the source IP and determines the server to handle the request.
  • SourceIpHashLoadBalancerExample Class:
    • Demonstrates the usage of the SourceIpHashLoadBalancer.
    • Creates an instance of the load balancer, adds servers, and simulates requests from different source IPs.

When to use Source IP Hash Load Balancing Algorithm?

  • Ideal for applications needing session consistency, like online banking, where the same user must connect to the same server throughout a session.
  • Useful when users from specific regions should connect to dedicated servers for better performance or compliance.
  • Effective when a few IPs generate most of the traffic, ensuring balanced load distribution without random switching.

Benefits and Drawbacks of Source IP Hash Load Balancing Algorithm:

  • Benefits:
    • Consistency: Ensures requests from the same source IP always go to the same server, maintaining session state.
    • Predictability: Useful when connection persistence is critical.
  • Drawbacks:
    • Limited Distribution: May lead to uneven load distribution if certain source IPs are more active.
    • Scaling Challenges: Adding or removing servers may disrupt session persistence.

Dynamic Load Balancing Algorithms

Dynamic load balancing involves making real-time decisions about how to distribute incoming network traffic or computational workload across multiple servers or resources. This approach adapts to the changing conditions of the system, such as variations in server load, network traffic, or resource availability.

The choice between dynamic and static load balancing depends on the characteristics of the system, the nature of the workload, and the desired level of adaptability. Dynamic load balancing is often favored in dynamic, high-traffic environments, while static load balancing may be suitable for more predictable scenarios.

1. Least Connection Method Load Balancing Algorithm

The Least Connections algorithm is a dynamic load balancing approach that assigns new requests to the server with the fewest active connections. The idea is to distribute incoming workloads in a way that minimizes the current load on each server, aiming for a balanced distribution of connections across all available resources. 

  • To do this load balancer needs to do some additional computing to identify the server with the least number of connections. 
  • This may be a little bit costlier compared to the round-robin method but the evaluation is based on the current load on the server. 

For example:

Lets say you're at a playground, and some kids are playing on different swings. You want to join the swing with the fewest kids so that it's not too crowded. Least Connection is like choosing the swing with the least number of kids already on it.

Least-Connection-(1)

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:

Java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;  class LeastConnectionLoadBalancer {     private Map<String, Integer> serverConnections;      public LeastConnectionLoadBalancer() {         this.serverConnections = new HashMap<>();     }      public void addServer(String serverName) {         // Add a server to the load balancer with 0 initial connections         serverConnections.put(serverName, 0);     }      public String getServerWithLeastConnections() {         // Find the server with the least active connections         int minConnections = Integer.MAX_VALUE;         String selectedServer = null;          for (Map.Entry<String, Integer> entry : serverConnections.entrySet()) {             if (entry.getValue() < minConnections) {                 minConnections = entry.getValue();                 selectedServer = entry.getKey();             }         }          // Increment the connection count for the selected server         if (selectedServer != null) {             serverConnections.put(selectedServer, minConnections + 1);         }          return selectedServer;     } }  public class LeastConnectionLoadBalancerExample {     public static void main(String[] args) {         // Create a Least Connection load balancer         LeastConnectionLoadBalancer loadBalancer = new LeastConnectionLoadBalancer();          // Add servers to the load balancer         loadBalancer.addServer("Server1");         loadBalancer.addServer("Server2");         loadBalancer.addServer("Server3");          // Simulate requests and print the server to which each request is routed         for (int i = 0; i < 10; i++) {             String selectedServer = loadBalancer.getServerWithLeastConnections();             System.out.println("Request " + (i + 1) + ": Routed to " + selectedServer);         }     } } 
Output
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:

  • LeastConnectionLoadBalancer Class:
    • Fields:
      • serverConnections: A map that tracks the number of active connections for each server.
    • Methods:
      • addServer: Adds a server to the load balancer with an initial connection count of 0.
      • getServerWithLeastConnections: Determines the server with the least active connections and increments its connection count.
  • LeastConnectionLoadBalancerExample Class:
    • Main Method:
      • Creates an instance of the LeastConnectionLoadBalancer.
      • Adds servers to the load balancer.
      • Simulates requests and prints the server to which each request is routed based on the least connection algorithm.

When to use Least Connection Load Balancing Algorithm?

  • Ideal for applications where some requests take longer to process than others (e.g., video streaming or large file uploads).
  • Useful when some connections stay active longer, as it ensures new requests go to servers with fewer active connections.
  • Great for systems with fluctuating traffic, as it balances based on real-time server load rather than just counting requests.

Benefits and Drawbacks of Least Connection Load Balancing Algorithm:

  • Benefits:
    • Balanced Load: Distributes traffic to servers with the fewest active connections, preventing overloading.
    • Dynamic: Adapts to changing server workloads.
  • Drawbacks:
    • Ignored Capacities: Ignores server capacities; a server with fewer connections may still have less capacity.
    • Sticky Sessions: May not be suitable for scenarios requiring session persistence.

2. Least Response Time Method Load Balancing Algorithm

The Least Response method is a dynamic load balancing approach that aims to minimize response times by directing new requests to the server with the quickest response time. 

  • It considers the historical performance of servers to make decisions about where to route incoming requests, optimizing for faster processing.
  • The dynamic aspect comes from the continuous monitoring of server response times and the adaptive nature of the algorithm to route incoming requests to the server with the historically lowest response time.

For example:

Picture yourself at a snack bar where you can order food from different servers. You notice that some servers are faster than others. You choose the server that seems to serve food the quickest each time you go. Least Response Time is like picking the server with the shortest line.

Least-Response-(2)

We need to implement a Load Balancing Algorithm that distribute incoming requests across a set of servers and should aim to minimize the response time by directing new requests to the server with the least accumulated response time. This ensures a balanced distribution of the workload and helps optimize the overall system performance.

Below is the implementation of the Least Response Time Load Balancing Algorithms:

Java
import java.util.HashMap; import java.util.Map;  class LeastResponseLoadBalancer {     private Map<String, Long> serverResponseTimes;      public LeastResponseLoadBalancer() {         this.serverResponseTimes = new HashMap<>();     }      public void addServer(String serverName) {         // Add a server to the load balancer with 0 initial response time         serverResponseTimes.put(serverName, 0L);     }      public String getServerWithLeastResponseTime() {         // Find the server with the least accumulated response time         long minResponseTime = Long.MAX_VALUE;         String selectedServer = null;          for (Map.Entry<String, Long> entry : serverResponseTimes.entrySet()) {             if (entry.getValue() < minResponseTime) {                 minResponseTime = entry.getValue();                 selectedServer = entry.getKey();             }         }          // Increment the response time for the selected server         if (selectedServer != null) {             serverResponseTimes.put(selectedServer, minResponseTime + 1);         }          return selectedServer;     } }  public class LeastResponseLoadBalancerExample {     public static void main(String[] args) {         // Create a Least Response load balancer         LeastResponseLoadBalancer loadBalancer = new LeastResponseLoadBalancer();          // Add servers to the load balancer         loadBalancer.addServer("Server1");         loadBalancer.addServer("Server2");         loadBalancer.addServer("Server3");          // Simulate requests and print the server to which each request is routed         for (int i = 0; i < 10; i++) {             String selectedServer = loadBalancer.getServerWithLeastResponseTime();             System.out.println("Request " + (i + 1) + ": Routed to " + selectedServer);         }     } } 
Output
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:

  1. LeastResponseLoadBalancer Class:
    • Fields:
      • serverResponseTimes: A map that tracks the accumulated response time for each server.
    • Methods:
      • addServer: Adds a server to the load balancer with an initial response time of 0.
      • getServerWithLeastResponseTime: Determines the server with the least accumulated response time and increments its response time.
  2. LeastResponseLoadBalancerExample Class:
    • Main Method:
      • Creates an instance of the LeastResponseLoadBalancer.
      • Adds servers to the load balancer.
      • Simulates requests and prints the server to which each request is routed based on the least response time algorithm.

When to use Least Response Time Load Balancing Algorithm?

  • Ideal for applications with heavy, fluctuating user traffic where response time matters.
  • Great for apps like e-commerce sites or streaming services, where a quick response improves user experience.
  • Works well when servers have different load levels, as it directs traffic to the server that’s both available and responds the fastest.

Benefits and Drawbacks of Least Response Time Load Balancing Algorithm:

  • Benefits:
    • Optimized Performance: Directs traffic to servers with the quickest response times, optimizing overall system performance.
    • Adaptable: Adjusts to changes in server responsiveness over time.
  • Drawbacks:
    • Historical Bias: Heavily influenced by past response times, may not always reflect current server capabilities.
    • Complex Implementation: Requires tracking and managing historical response times.

3. Resource-based Load Balancing Algorithm

The Resource-Based Load Balancing algorithm distributes incoming requests based on the current resource availability of each server, such as CPU usage, memory, or network bandwidth. Rather than just routing traffic equally or based on past performance, this algorithm evaluates the current "resource health" of each server to decide where new requests should go.

resource-based-load-balancing-algorithm
Resource-based Load Balancing Algorithm

For example:

Imagine it like assigning tasks in an office based on each employee’s workload at the moment—some are busy, while others are free. Resource-Based Load Balancing directs requests to the server with the most available resources.

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.

Java
import java.util.HashMap; import java.util.Map;  class Server {     String name;     double cpuLoad;  // Represents the current CPU load percentage of the server          public Server(String name) {         this.name = name;         this.cpuLoad = 0.0;     }      // Simulate updating the CPU load for the server     public void updateCpuLoad(double load) {         this.cpuLoad = load;     }          public double getCpuLoad() {         return this.cpuLoad;     }          public String getName() {         return this.name;     } }  class ResourceBasedLoadBalancer {     private Map<String, Server> servers;      public ResourceBasedLoadBalancer() {         this.servers = new HashMap<>();     }      // Adds a server to the load balancer     public void addServer(Server server) {         servers.put(server.getName(), server);     }      // Finds the server with the lowest CPU load and assigns the request to it     public Server getServerWithMostResources() {         Server bestServer = null;         double lowestLoad = Double.MAX_VALUE;          for (Server server : servers.values()) {             if (server.getCpuLoad() < lowestLoad) {                 lowestLoad = server.getCpuLoad();                 bestServer = server;             }         }         return bestServer;     }          // Simulate handling a request     public void handleRequest() {         Server bestServer = getServerWithMostResources();         if (bestServer != null) {             System.out.println("Routing request to server: " + bestServer.getName() + " with current CPU load: " + bestServer.getCpuLoad() + "%");             // Here, you might update the server's load after handling a request         } else {             System.out.println("No servers available.");         }     } }  public class ResourceBasedLoadBalancerExample {     public static void main(String[] args) {         ResourceBasedLoadBalancer loadBalancer = new ResourceBasedLoadBalancer();                  // Create servers and add them to the load balancer         Server server1 = new Server("Server1");         Server server2 = new Server("Server2");         Server server3 = new Server("Server3");                  loadBalancer.addServer(server1);         loadBalancer.addServer(server2);         loadBalancer.addServer(server3);                  // Simulate updating CPU load for each server         server1.updateCpuLoad(30.0);         server2.updateCpuLoad(50.0);         server3.updateCpuLoad(20.0);                  // Route requests based on current CPU load         loadBalancer.handleRequest();  // This should route to Server3, as it has the lowest CPU load     } } 

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 in the load balancer with attributes like name and cpuLoad. The updateCpuLoad method allows the CPU load of each server to be updated, simulating the monitoring of real-time load.
  • ResourceBasedLoadBalancer Class: Manages a list of servers and routes requests based on available resources.
    • addServer: Adds a server to the load balancer.
    • getServerWithMostResources: Iterates through the list of servers to find the one with the lowest CPU load, which has the most available resources.
    • handleRequest: Simulates handling a request by routing it to the server with the lowest CPU load.
  • ResourceBasedLoadBalancerExample Class: This main class sets up the load balancer, adds servers, updates their 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 itself.



Next Article
How does a Load Balancer Works?

S

sanketsay9qs
Improve
Article Tags :
  • System Design
  • Load Balancer

Similar Reads

    What is Load Balancer & How Load Balancing works?
    A load balancer is a crucial component in system design that distributes incoming network traffic across multiple servers. Its main purpose is to ensure that no single server is overburdened with too many requests, which helps improve the performance, reliability, and availability of applications.Ta
    9 min read
    Load Balancer - System Design Interview Question
    When a website becomes extremely popular, the traffic on that website increases, and the load on a single server also increases. The concurrent traffic overloads the single server and the website becomes slower for the users. To meet the request of these high volumes of data and to return the correc
    7 min read
    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 balan
    4 min read
    Load Balancing Algorithms
    To control traffic across servers in a network, load-balancing algorithms are important. By spreading requests evenly, load balancers make sure that no single server is overloaded when several people visit an application. Various techniques, such as IP hash, Least Connections, and Round Robin, are e
    15+ min read
    How does a Load Balancer Works?
    A load balancer is a crucial component in system design, ensuring that incoming network traffic is efficiently distributed across multiple servers or resources. The primary goal is to optimize resource utilization, enhance system performance, and ensure high availability and fault tolerance. The fun
    3 min read
    Load Balancing Approach in Distributed System
    A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load adjusting is the approach to conveying load units (i.e., occupations/assignments) across the organization which is associated with the distributed system. Load adj
    3 min read
    How to Create a Load Balancer?
    Creating a load balancer typically involves using dedicated hardware or software solutions. Below are general steps for setting up a basic load balancer in a traditional, non-cloud environment: To create a Classic Load Balancer:Table of Content Step 1: Choose a Load Balancer SolutionStep 2: Design Y
    3 min read
    Reverse Proxy Vs. Load Balancer
    In the realm of system design, confusion often arises regarding the distinct roles of reverse proxies and load balancers. Despite their critical importance in managing and optimizing web traffic, these components serve unique purposes and possess different functionalities. In this article, we will d
    4 min read
    Layer 4 Load Balancing vs. Layer 7 Load Balancing
    Load balancing is the process of distributing incoming network traffic or computational workloads across multiple servers, resources, or processes in a network. The primary goal of load balancing is to optimize resource utilization, maximize throughput, minimize response time, and avoid overload on
    5 min read
    Load Balancing using AWS
    Load balancing is a critical component in ensuring the seamless functioning and high availability of web applications. As cloud computing continues to dominate the modern tech landscape, Amazon Web Services (AWS) has emerged as a leading cloud platform, offering an array of robust load-balancing ser
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences