Operations 33 min read

Common Load Balancing Algorithms and Their Java Implementations

This article reviews common load balancing strategies—including round-robin, random, weighted, smooth weighted round-robin, consistent hashing, least-active and optimal-response algorithms—explains their advantages and drawbacks, and provides complete Java code examples for each method.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Common Load Balancing Algorithms and Their Java Implementations

Load balancing is a fundamental concept in high‑availability architectures, appearing in micro‑services, distributed databases, middleware, cloud computing, and big data environments.

Load‑balancing strategies are divided into static (configured once) and dynamic (adjusted based on runtime metrics such as CPU, network, or I/O load).

Basic Algorithms

Round‑robin distributes requests sequentially across servers; random selects a server at random; weighted algorithms assign a weight to each server to influence selection frequency.

public class Servers {
    public static List
SERVERS = Arrays.asList(
        "44.120.110.001:8080",
        "44.120.110.002:8081",
        "44.120.110.003:8082",
        "44.120.110.004:8083",
        "44.120.110.005:8084"
    );
}

public class RoundRobin {
    private static AtomicInteger requestIndex = new AtomicInteger(0);
    public static String getServer() {
        int index = requestIndex.getAndIncrement() % Servers.SERVERS.size();
        return Servers.SERVERS.get(index);
    }
}

The weighted version adds a map of server → weight and selects a server based on a random index within the total weight range.

public class RandomWeight {
    static Map
WEIGHT_SERVERS = new LinkedHashMap<>();
    static {
        WEIGHT_SERVERS.put("44.120.110.001:8080", 17);
        WEIGHT_SERVERS.put("44.120.110.002:8081", 11);
        WEIGHT_SERVERS.put("44.120.110.003:8082", 30);
    }
    public static String getServer() {
        int total = WEIGHT_SERVERS.values().stream().mapToInt(Integer::intValue).sum();
        int index = new Random().nextInt(total);
        for (Map.Entry
e : WEIGHT_SERVERS.entrySet()) {
            if (index < e.getValue()) return e.getKey();
            index -= e.getValue();
        }
        return null;
    }
}

Smooth Weighted Round‑Robin

This algorithm maintains a dynamic weight for each server, adding the static weight on each request and selecting the server with the highest current weight, then reducing its current weight by the total weight. It evenly distributes traffic according to configured weights.

public class RoundRobinWeight {
    private static Map
weightMap = new HashMap<>();
    private static int weightTotal = 0;
    static {
        // initialize weightMap and weightTotal
    }
    public static String getServer() {
        for (Weight w : weightMap.values()) {
            w.currentWeight += w.weight;
        }
        Weight max = Collections.max(weightMap.values(), Comparator.comparingInt(Weight::getCurrentWeight));
        max.currentWeight -= weightTotal;
        return max.server;
    }
}

Consistent Hashing

Consistent hashing maps both servers and request keys onto a virtual hash ring, ensuring that the same key (or client IP) always maps to the same server unless the server set changes. Virtual nodes are added to improve distribution and reduce hotspot risk.

public class ConsistentHash {
    private static TreeMap
virtualNodes = new TreeMap<>();
    private static final int VIRTUAL_NODES = 160;
    static {
        for (String ip : Servers.SERVERS) {
            virtualNodes.put(getHashCode(ip), ip);
            for (int i = 0; i < VIRTUAL_NODES; i++) {
                virtualNodes.put(getHashCode(ip + i), ip);
            }
        }
    }
    public static String getServer(String key) {
        int hash = getHashCode(key);
        SortedMap
tail = virtualNodes.tailMap(hash);
        int nodeKey = tail.isEmpty() ? virtualNodes.firstKey() : tail.firstKey();
        return virtualNodes.get(nodeKey);
    }
    // simple hash function omitted for brevity
}

Least‑Active Algorithm

Each server tracks an active request count; the algorithm selects the server with the smallest count, providing better load distribution under varying workloads.

public class LeastActive {
    public static String getServer() {
        int least = Integer.MAX_VALUE;
        Server chosen = null;
        for (Server s : Servers.SERVERS) {
            int active = s.getActive().get();
            if (active < least) { least = active; chosen = s; }
        }
        return chosen.getIP();
    }
}

Optimal‑Response (Fastest‑Ping) Algorithm

Before routing a request, the system pings all servers concurrently and selects the one that responds first, ensuring the lowest latency path.

public class ResponseTime {
    static ExecutorService pool = Executors.newFixedThreadPool(Servers.SERVERS.size());
    public static String getServer() throws InterruptedException {
        List
> futures = Servers.SERVERS.stream()
            .map(s -> CompletableFuture.supplyAsync(s::ping, pool))
            .collect(Collectors.toList());
        return (String) CompletableFuture.anyOf(futures.toArray(new CompletableFuture[0])).join();
    }
}

The article concludes that while advanced algorithms provide smarter distribution, the simplest round‑robin often yields the best performance under massive traffic when server capacities are uniform.

Distributed SystemsJavaLoad BalancingAlgorithmsconsistent hashingweighted round robin
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.