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:
Round robin is a simple load balancing algorithm that directs client requests to different servers based on a rotating list. The load balancer maintains a list of available servers and directs incoming requests in a round-robin fashion: the first request goes to the first server, the second request goes to the second server, and so on. When the load balancer reaches the end of the list, it goes back to the beginning and starts from the first server again.
Java example code of round robin scheduling
class Server {
private final String address;
public Server(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
class LoadBalancer {
private static int currentServer = 0;
private final List<Server> servers = new ArrayList<>();
public void addServer(Server server) {
servers.add(server);
}
public void removeServer(Server server) {
servers.remove(server);
}
public Server nextServer() {
Server server = servers.get(currentServer);
currentServer = (currentServer + 1) % servers.size();
return server;
}
}
public class Main {
public static void main(String[] args) {
LoadBalancer loadBalancer = new LoadBalancer();
loadBalancer.addServer(new Server("Server 1"));
loadBalancer.addServer(new Server("Server 2"));
loadBalancer.addServer(new Server("Server 3"));
for (int i = 0; i < 11; i++) {
Server 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
Benefits and drawbacks of the round robin algorithm include:
The weighted round-robin load-balancing algorithm is a more advanced version of the simple round-robin algorithm. It distributes incoming requests based on the weighted score of the servers. The weight can be an integer that reflects the server's processing power or specifications. This allows the algorithm to consider server specifications when distributing traffic.
Some benefits and drawbacks of the weighted round-robin algorithm include:
Java example code for random load balancing
class Server {
private String name;
public Server(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class LoadBalancer {
private List<Server> servers = new ArrayList<>();
private Random random = new Random();
public void addServer(Server server) {
servers.add(server);
}
public Server getRandomServer() {
int index = random.nextInt(servers.size());
return servers.get(index);
}
}
public class Main {
public static void main(String[] args) {
LoadBalancer loadBalancer = new LoadBalancer();
loadBalancer.addServer(new Server("Server1"));
loadBalancer.addServer(new Server("Server2"));
loadBalancer.addServer(new Server("Server3"));
for (int i = 0; i < 10; i++) {
Server server = loadBalancer.getRandomServer();
System.out.println("Request" + i + " assigned to " + server.getName());
}
}
}
The source IP hash load-balancing algorithm selects a server based on a unique hash key. It generates the hash key by combining the source and destination IP addresses and allocates the request to a specific server. If the session is broken, the key can be regenerated and the client request can be directed back to the same server that was originally handling it. In other words, this algorithm is useful when a dropped connection needs to be returned to the same server.
The URL hash load-balancing algorithm generates a hash value based on the URL present in client requests. The requests are forwarded to servers based on the hash value. The load balancer caches the hashed value of the URL, so subsequent requests that use the same URL result in a cache hit and are forwarded to the same server.
URL hash algorithm is useful when load-balanced servers serve mostly unique content per server. In other words, all requests related to one process (e.g., running code) will go to one server, and all requests related to another process (e.g., payments) will go to another server, and so on.
The least connection load-balancing algorithm considers the current load on a server and aims to improve performance. It does this by sending requests to the server with the least number of active connections.
Some benefits and drawbacks of the least connection algorithm include:
The weighted least connections load-balancing algorithm distributes load based on both the number of current and active connections to each server and the relative capacity of the server.
The least response time load-balancing algorithm is a more advanced version of the least connection method. It forwards requests to the server with the fewest active connections and the least average response time.
Note: Our system might have multiple load balancers that use different server selection strategies. Enjoy learning, enjoy system design!
Subscribe to get well designed content on data structure and algorithms, machine learning, system design, object orientd programming and math.