Mastering Load Balancing: Java Implementations of Round Robin, Random, and Weighted Algorithms
This article explains load‑balancing concepts and demonstrates Java implementations of several algorithms—including Round Robin, Random, source‑IP hash, weighted round robin, weighted random, and least‑connections—while discussing their advantages, drawbacks, and concurrency considerations.
Load balancing distributes incoming requests across multiple servers to expand bandwidth, increase throughput, strengthen data‑processing capability, and improve flexibility and availability.
The article introduces several load‑balancing algorithms and provides complete Java implementations for each, starting with a simulated IP‑to‑weight map.
IP Map Simulation
import java.util.HashMap;
public class IpMap {
// Server IP list with weights
public static HashMap<String, Integer> serverWeightMap = new HashMap<String, Integer>();
static {
serverWeightMap.put("192.168.1.100", 1);
serverWeightMap.put("192.168.1.101", 1);
serverWeightMap.put("192.168.1.102", 4);
serverWeightMap.put("192.168.1.103", 1);
serverWeightMap.put("192.168.1.104", 1);
serverWeightMap.put("192.168.1.105", 3);
serverWeightMap.put("192.168.1.106", 1);
serverWeightMap.put("192.168.1.107", 2);
serverWeightMap.put("192.168.1.108", 1);
serverWeightMap.put("192.168.1.109", 1);
serverWeightMap.put("192.168.1.110", 1);
}
}Round Robin (Stateless)
The algorithm cycles through the server list sequentially, requiring no state about previous connections.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class RoundRobin {
private static Integer pos = 0;
public static String getServer() {
Map<String, Integer> serverMap = new HashMap<String, Integer>();
serverMap.putAll(IpMap.serverWeightMap);
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList();
keyList.addAll(keySet);
String server;
synchronized (pos) {
if (pos > keySet.size() - 1) {
pos = 0;
}
server = (String) keyList.get(pos);
pos++;
}
return server;
}
}Random
Selects a server at random from the list, achieving an average distribution similar to round robin over many requests.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Random {
public static String getServer() {
Map<String, Integer> serverMap = new HashMap<String, Integer>();
serverMap.putAll(IpMap.serverWeightMap);
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList();
keyList.addAll(keySet);
java.util.Random random = new java.util.Random();
int randomPos = random.nextInt(keyList.size());
return (String) keyList.get(randomPos);
}
}Source‑IP Hash
Uses the client’s IP address hash to consistently map the same client to the same server while the server list remains unchanged.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Hash {
public static String getServer() {
Map<String, Integer> serverMap = new HashMap<String, Integer>();
serverMap.putAll(IpMap.serverWeightMap);
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList();
keyList.addAll(keySet);
String remoteIp = "127.0.0.1"; // In a real web app, obtain via HttpServletRequest.getRemoteAddr()
int hashCode = remoteIp.hashCode();
int serverPos = hashCode % keyList.size();
return (String) keyList.get(serverPos);
}
}Weighted Round Robin
Assigns a weight to each server; servers with higher weight appear more frequently in the rotation.
import java.util.*;
public class WeightRoundRobin {
private static Integer pos = 0;
public static String getServer() {
Map<String, Integer> serverMap = new HashMap<String, Integer>();
serverMap.putAll(IpMap.serverWeightMap);
Set keySet = serverMap.keySet();
Iterator iterator = keySet.iterator();
List<String> serverList = new ArrayList<String>();
while (iterator.hasNext()) {
String server = (String) iterator.next();
int weight = serverMap.get(server);
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
String selected;
synchronized (pos) {
if (pos > serverList.size() - 1) {
pos = 0;
}
selected = serverList.get(pos);
pos++;
}
return selected;
}
}Weighted Random
Builds a list where each server appears a number of times equal to its weight, then picks an entry at random.
import java.util.*;
public class WeightRandom {
public static String getServer() {
Map<String, Integer> serverMap = new HashMap<String, Integer>();
serverMap.putAll(IpMap.serverWeightMap);
Set keySet = serverMap.keySet();
Iterator iterator = keySet.iterator();
List<String> serverList = new ArrayList<String>();
while (iterator.hasNext()) {
String server = (String) iterator.next();
int weight = serverMap.get(server);
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
java.util.Random random = new java.util.Random();
int randomPos = random.nextInt(serverList.size());
return serverList.get(randomPos);
}
}Least Connections
This algorithm selects the server with the fewest active connections at the moment, offering a more dynamic and load‑aware distribution, though its implementation is more complex and is omitted here.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
