Implementing Two Simple Load Balancing Algorithms

Implementing Two Simple Load Balancing Algorithms

Implementing Two Simple Load Balancing Algorithms

Load balancers can distribute requests to specific servers based on various load balancing techniques that use different algorithms to select servers based on a particular configuration. Load balancing algorithms can be divided into two categories:

Static load balancing algorithms

These algorithms work the same way regardless of the state of the backend server serving the requests. They are simpler and more efficient to implement, but they can lead to an uneven distribution of requests. Examples of static load balancing algorithms include round robin, weighted round robin, source IP hash, URL hash, randomized algorithm, etc.

Java example code of round robin scheduling

RoundRobinServer.java

class RoundRobinServer {
  private final String address;
  public RoundRobinServer(String address) {
    this.address = address;
  }

  public String getAddress() {
    return address;
  }
}

LoadBalancer.java

class LoadBalancer {
  private static int currentServer = 0;
  private final List<RoundRobinServer> servers = new ArrayList<>();

  public void addServer(RoundRobinServer server) {
    servers.add(server);
  }

  public void removeServer(RoundRobinServer server) {
    servers.remove(server);
  }

  public RoundRobinServer nextServer() {
    RoundRobinServer server = servers.get(currentServer);
    currentServer = (currentServer + 1) % servers.size();
    return server;
  }
}

RoundRobinClient.java

public class RoundRobinClient {
  public static void main(String[] args) {
    LoadBalancer loadBalancer = new LoadBalancer();
    loadBalancer.addServer(new RoundRobinServer("Server 1"));
    loadBalancer.addServer(new RoundRobinServer("Server 2"));
    loadBalancer.addServer(new RoundRobinServer("Server 3"));

    for (int i = 0; i < 12; i++) {
      RoundRobinServer server = loadBalancer.nextServer();
      System.out.println("Request " + (i + 1) + " directed to " + server.getAddress());
    }
  }
}

output

Request 1 directed to Server 1
Request 2 directed to Server 2
Request 3 directed to Server 3
Request 4 directed to Server 1
Request 5 directed to Server 2
Request 6 directed to Server 3
Request 7 directed to Server 1
Request 8 directed to Server 2
Request 9 directed to Server 3
Request 10 directed to Server 1
Request 11 directed to Server 2
Request 12 directed to Server 3

Benefits and drawbacks of the round robin algorithm

  • It is very easy to implement.
  • It evenly balances traffic between servers.
  • It does not consider the server's load or specifications, so there is a risk that a server with low capacity will receive a large number of requests and become overloaded.
  • It works best if every server in the load balancer list has roughly the same specification. Otherwise, a low processing server may have the same load as a high processing server.

Java example code for random selection load balancing

RandomSelectionServer.java

class RandomSelectionServer {
    private String name;

    public Server(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

RandomLoadBalancer

class RandomLoadBalancer {
    private List<RandomSelectionServer> servers = new ArrayList<>();
    private Random random = new Random();

    public void addServer(RandomSelectionServer server) {
        servers.add(server);
    }

    public RandomSelectionServer getRandomServer() {
        int index = random.nextInt(servers.size());
        return servers.get(index);
    }
}

RandomLoadBalancerClient.java

public class RandomLoadBalancerClient {
    public static void main(String[] args) {
        RandomLoadBalancer loadBalancer = new RandomLoadBalancer();
        loadBalancer.addServer(new RandomSelectionServer("Server1"));
        loadBalancer.addServer(new RandomSelectionServer("Server2"));
        loadBalancer.addServer(new RandomSelectionServer("Server3"));

        for (int i = 0; i < 10; i++) {
            RandomSelectionServer server = loadBalancer.getRandomServer();
            System.out.println("Request" + i + " assigned to " + server.getName());
        }
    }
}

output

Request 0 assigned to Server 3
Request 1 assigned to Server 1
Request 2 assigned to Server 2
Request 3 assigned to Server 1
Request 4 assigned to Server 2
Request 5 assigned to Server 2
Request 6 assigned to Server 1
Request 7 assigned to Server 1
Request 8 assigned to Server 3
Request 9 assigned to Server 1

Dynamic load balancing algorithms

These algorithms take into account the state of the backend server and consider server load when distributing requests. They require communication between the load balancers and servers, so they are slightly more complex but can distribute requests efficiently. Examples of dynamic load balancing algorithms include the least connection method, weighted least connections method, least response time method, etc.

Today the focus on this article is only looking into static load balancing algorithms, if you wish to see any examples on dynamic load balancing algorithms, leave a comment and we will schedule something in the future covering this topic.

Conclusion

Through our exploration of Round Robin and Random selection algorithms, we've gained insights into their respective strengths and limitations. Round Robin offers simplicity and ease of implementation, albeit with potential drawbacks in handling uneven loads. On the other hand, Random selection provides a straightforward approach to load balancing, suitable for scenarios where uniform distribution is not a primary concern.

In real-world deployments, organizations often employ a combination of load balancing strategies, including more sophisticated algorithms and mechanisms such as weighted distribution, health checks, and session persistence. By understanding the principles underlying basic load balancing algorithms like Round Robin and Random selection, stakeholders can make informed decisions to optimize resource utilization, enhance system scalability, and ensure high availability in distributed computing environments.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!