Load refers to the capacity of a backend system to handle requests. For example, under the same conditions, a machine with 1 core CPU and 1GB memory typically has lower load capacity compared to a machine with 8 cores and 8GB of memory. Additionally, under the same configuration, a machine with a CPU utilization rate of 80% may be considered less capable than one with 50% utilization. The concept of load is crucial in determining how efficiently a system can manage incoming traffic.
Balance ensures that backend requests are distributed evenly across available servers. For instance, when multiple machines are involved, requests are allocated among them equally. In some scenarios, it's also important to assign the same user to the same machine as much as possible, which helps maintain session consistency and improves user experience.
The load balancing algorithm aims to address cross-system calls and ensure that requests are distributed fairly and efficiently based on the backend system's capacity. The following sections provide an analysis of several Java-based load balancing algorithms along with their source code for reference.
1. Polling:
The polling algorithm is a simple round-robin approach where each server is selected in sequence. Below is a sample implementation of a basic polling load balancer in Java:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Load balancing algorithm using a polling method
* @author guoy
*/
public class TestRoundRobin {
static Map
static {
serverWeightMap.put("192.168.1.12", 1);
serverWeightMap.put("192.168.1.13", 1);
serverWeightMap.put("192.168.1.14", 2);
serverWeightMap.put("192.168.1.15", 2);
serverWeightMap.put("192.168.1.16", 3);
serverWeightMap.put("192.168.1.17", 3);
serverWeightMap.put("192.168.1.18", 1);
serverWeightMap.put("192.168.1.19", 2);
}
Integer pos = 0;
public String roundRobin() {
Map
serverMap.putAll(serverWeightMap);
Set
ArrayList
keyList.addAll(keySet);
String server = null;
synchronized (pos) {
if (pos >= keySet.size()) {
pos = 0;
}
server = keyList.get(pos);
pos++;
}
return server;
}
public static void main(String[] args) {
TestRoundRobin robin = new TestRoundRobin();
for (int i = 0; i < 20; i++) {
String serverIp = robin.roundRobin();
System.out.println(serverIp);
}
}
}
2. Weighted Polling:
This algorithm assigns requests based on the weight of each server. Servers with higher weights receive more requests. Here’s a Java implementation of weighted polling:
package class2.zookeeper.loadbalance;
import java.util.*;
/**
* Weighted random load balancing algorithm
* @author guoy
*/
public class TestWeightRandom {
static Map
static {
serverWeightMap.put("192.168.1.12", 1);
serverWeightMap.put("192.168.1.13", 1);
serverWeightMap.put("192.168.1.14", 2);
serverWeightMap.put("192.168.1.15", 2);
serverWeightMap.put("192.168.1.16", 3);
serverWeightMap.put("192.168.1.17", 3);
serverWeightMap.put("192.168.1.18", 1);
serverWeightMap.put("192.168.1.19", 2);
}
public static String weightRandom() {
Map
serverMap.putAll(serverWeightMap);
Set
Iterator
List
while (it.hasNext()) {
String server = it.next();
Integer weight = serverMap.get(server);
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
Random random = new Random();
int randomPos = random.nextInt(serverList.size());
String server = serverList.get(randomPos);
return server;
}
public static void main(String[] args) {
String serverIp = weightRandom();
System.out.println(serverIp);
}
}
3. Random:
The random algorithm selects a server randomly from the list of available servers. This method ensures even distribution over time but does not account for server weights or load. Here’s a sample implementation:
package class2.zookeeper.loadbalance;
import java.util.*;
/**
* Random load balancing algorithm
* @author guoy
*/
public class TestRandom {
static Map
static {
serverWeightMap.put("192.168.1.12", 1);
serverWeightMap.put("192.168.1.13", 1);
serverWeightMap.put("192.168.1.14", 2);
serverWeightMap.put("192.168.1.15", 2);
serverWeightMap.put("192.168.1.16", 3);
serverWeightMap.put("192.168.1.17", 3);
serverWeightMap.put("192.168.1.18", 1);
serverWeightMap.put("192.168.1.19", 2);
}
public static String random() {
Map
serverMap.putAll(serverWeightMap);
Set
ArrayList
keyList.addAll(keySet);
Random random = new Random();
int randomPos = random.nextInt(keyList.size());
String server = keyList.get(randomPos);
return server;
}
public static void main(String[] args) {
String serverIp = random();
System.out.println(serverIp);
}
}
4. Weighted Random:
Similar to weighted polling, this algorithm uses weights to determine the probability of selecting a particular server. A server with a higher weight has a higher chance of being chosen. Here is the corresponding Java code:
package class2.zookeeper.loadbalance;
import java.util.*;
/**
* Weighted random load balancing algorithm
* @author guoy
*/
public class TestWeightRandom {
static Map
static {
serverWeightMap.put("192.168.1.12", 1);
serverWeightMap.put("192.168.1.13", 1);
serverWeightMap.put("192.168.1.14", 2);
serverWeightMap.put("192.168.1.15", 2);
serverWeightMap.put("192.168.1.16", 3);
serverWeightMap.put("192.168.1.17", 3);
serverWeightMap.put("192.168.1.18", 1);
serverWeightMap.put("192.168.1.19", 2);
}
public static String weightRandom() {
Map
serverMap.putAll(serverWeightMap);
Set
Iterator
List
while (it.hasNext()) {
String server = it.next();
Integer weight = serverMap.get(server);
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
Random random = new Random();
int randomPos = random.nextInt(serverList.size());
String server = serverList.get(randomPos);
return server;
}
public static void main(String[] args) {
String serverIp = weightRandom();
System.out.println(serverIp);
}
}
5. IP Hash:
The IP hash algorithm determines which server should handle a request based on the hash value of the client's IP address. This ensures that the same client always gets routed to the same server, which is useful for maintaining session state. Here is an example implementation:
package class2.zookeeper.loadbalance;
import java.util.*;
/**
* Load balancing using IP hash algorithm
* @author guoy
*/
public class TestIpHash {
static Map
static {
serverWeightMap.put("192.168.1.12", 1);
serverWeightMap.put("192.168.1.13", 1);
serverWeightMap.put("192.168.1.14", 2);
serverWeightMap.put("192.168.1.15", 2);
serverWeightMap.put("192.168.1.16", 3);
serverWeightMap.put("192.168.1.17", 3);
serverWeightMap.put("192.168.1.18", 1);
serverWeightMap.put("192.168.1.19", 2);
}
public static String ipHash(String remoteIp) {
Map
serverMap.putAll(serverWeightMap);
Set
ArrayList
keyList.addAll(keySet);
int hashCode = remoteIp.hashCode();
int serverListSize = keyList.size();
int serverPos = hashCode % serverListSize;
return keyList.get(serverPos);
}
public static void main(String[] args) {
String serverIp = ipHash("192.168.1.12");
System.out.println(serverIp);
}
}
circular connector,m series connector,m connector,m12 connector,m8 connector,circular connector m12,m8 electrical connector
Dongguan Yiyou Electronic Technology Co., Ltd. , https://www.dsubminiature.com