WikiGalaxy

Personalize

Introduction to Load Balancing

What is Load Balancing?

Load balancing is the process of distributing network or application traffic across multiple servers. It ensures no single server becomes overwhelmed, improving responsiveness and availability.

Why is Load Balancing Important?

Load balancing is crucial for maintaining high availability and reliability of applications. It helps in managing sudden spikes in traffic and ensures that resources are utilized optimally.

Types of Load Balancers

  • Hardware Load Balancers: Dedicated hardware devices used for load distribution.
  • Software Load Balancers: Software-based solutions that can be installed on standard servers.
  • Cloud Load Balancers: Load balancing services offered by cloud providers.

Load Balancing Algorithms

  • Round Robin: Distributes requests sequentially across the server pool.
  • Least Connections: Directs traffic to the server with the fewest active connections.
  • IP Hash: Assigns a client to a specific server based on their IP address.

Example: Round Robin Load Balancing

Scenario

A web application receives requests from users. To efficiently manage these requests, a round-robin algorithm is used to distribute them evenly across three servers.


      import java.util.LinkedList;
      import java.util.Queue;

      class RoundRobinLoadBalancer {
          private Queue servers = new LinkedList<>();

          public RoundRobinLoadBalancer() {
              servers.add("Server1");
              servers.add("Server2");
              servers.add("Server3");
          }

          public String getNextServer() {
              String server = servers.poll();
              servers.offer(server);
              return server;
          }

          public static void main(String[] args) {
              RoundRobinLoadBalancer lb = new RoundRobinLoadBalancer();
              for (int i = 0; i < 6; i++) {
                  System.out.println("Request " + i + " directed to " + lb.getNextServer());
              }
          }
      }
    

Explanation

In this example, a simple round-robin load balancer is implemented using a queue. Requests are distributed evenly across three servers, ensuring balanced load distribution.

Console Output:

Request 0 directed to Server1

Request 1 directed to Server2

Request 2 directed to Server3

Request 3 directed to Server1

Request 4 directed to Server2

Request 5 directed to Server3

Example: Least Connections Load Balancing

Scenario

A web service needs to handle multiple client requests efficiently. The least connections algorithm is used to route requests to the server with the fewest active connections.


      import java.util.HashMap;
      import java.util.Map;

      class LeastConnectionsLoadBalancer {
          private Map serverConnections = new HashMap<>();

          public LeastConnectionsLoadBalancer() {
              serverConnections.put("Server1", 0);
              serverConnections.put("Server2", 0);
              serverConnections.put("Server3", 0);
          }

          public String getServerWithLeastConnections() {
              return serverConnections.entrySet()
                      .stream()
                      .min(Map.Entry.comparingByValue())
                      .get()
                      .getKey();
          }

          public void addConnection(String server) {
              serverConnections.put(server, serverConnections.get(server) + 1);
          }

          public static void main(String[] args) {
              LeastConnectionsLoadBalancer lb = new LeastConnectionsLoadBalancer();
              for (int i = 0; i < 6; i++) {
                  String server = lb.getServerWithLeastConnections();
                  System.out.println("Request " + i + " directed to " + server);
                  lb.addConnection(server);
              }
          }
      }
    

Explanation

In this example, the least connections load balancer selects the server with the fewest active connections for each incoming request. This approach helps in distributing the load more evenly, especially when certain servers are more capable of handling traffic than others.

Console Output:

Request 0 directed to Server1

Request 1 directed to Server2

Request 2 directed to Server3

Request 3 directed to Server1

Request 4 directed to Server2

Request 5 directed to Server3

Example: IP Hash Load Balancing

Scenario

A web application uses IP hash load balancing to direct requests from a specific client IP to the same server, maintaining session persistence.


      import java.util.HashMap;
      import java.util.Map;

      class IPHashLoadBalancer {
          private Map ipToServerMap = new HashMap<>();
          private String[] servers = {"Server1", "Server2", "Server3"};

          public String getServerForIP(String ip) {
              if (!ipToServerMap.containsKey(ip)) {
                  int hash = ip.hashCode();
                  int serverIndex = Math.abs(hash % servers.length);
                  ipToServerMap.put(ip, servers[serverIndex]);
              }
              return ipToServerMap.get(ip);
          }

          public static void main(String[] args) {
              IPHashLoadBalancer lb = new IPHashLoadBalancer();
              String[] clientIPs = {"192.168.1.1", "192.168.1.2", "192.168.1.3"};
              for (String ip : clientIPs) {
                  System.out.println("Client IP " + ip + " directed to " + lb.getServerForIP(ip));
              }
          }
      }
    

Explanation

This example demonstrates IP hash load balancing, where each client IP is hashed to determine the server it should connect to. This method ensures that requests from the same client IP are always directed to the same server, providing session persistence.

Console Output:

Client IP 192.168.1.1 directed to Server1

Client IP 192.168.1.2 directed to Server2

Client IP 192.168.1.3 directed to Server3

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025