Common Load Balancing Algorithms and Their Java Implementations
This article provides a comprehensive overview of various load balancing strategies—including round‑robin, random, weighted, smooth weighted round‑robin, consistent hashing, least‑active, and optimal‑response algorithms—explaining their principles, advantages, disadvantages, use‑cases, and offering complete Java code examples for each.
Load balancing is a fundamental concept in high‑availability systems, appearing in micro‑services, databases, middleware, cloud computing, and big data environments. The article categorizes load‑balancing strategies into static (e.g., round‑robin, random, weighted) and dynamic algorithms (e.g., smooth weighted round‑robin, consistent hashing, least‑active, optimal‑response).
Basic Algorithms
Round‑robin distributes requests sequentially across servers; random selects a server at random; weighted algorithms assign more requests to servers with higher weights. Java implementations are provided with Servers, RoundRobin, Random, and Randomweight classes.
Smooth Weighted Round‑Robin
This algorithm solves the uneven distribution problem of simple weighted round‑robin by maintaining a dynamic weight for each server. The Java code defines a Weight class with static and current weights and a RoundRobinWeight class that updates and selects servers based on these weights.
Consistent Hashing
Consistent hashing maps both servers and request keys onto a hash ring, ensuring that the same key always routes to the same server unless the cluster changes. Virtual nodes are used to mitigate hash‑ring skew. The Java implementation uses a TreeMap to represent the ring and provides getServer and getHashCode methods.
Least‑Active Algorithm
This dynamic algorithm selects the server with the fewest active requests, adjusting for real‑time load. The Java example includes a Server class with an AtomicInteger for active count and a LeastActive selector that periodically attenuates activity counters.
Optimal‑Response Algorithm
By pinging all servers concurrently and choosing the one with the fastest response, this algorithm ensures the best performance under varying conditions. The implementation uses CompletableFuture to issue parallel ping calls and selects the first completed result.
The article concludes with a summary of each algorithm’s trade‑offs, emphasizing that simpler static algorithms like round‑robin may be preferable under extreme load when all nodes have identical resources, while more sophisticated strategies provide better distribution and resilience in heterogeneous environments.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
