How to Build a Weighted Random Load Balancer in Python
A Python load balancer mock that distributes requests across servers based on configurable weights using a cumulative weighted random selection algorithm.
Python code
32 linesimport random
from collections import Counter
SERVERS = {
"server-a": 50,
"server-b": 30,
"server-c": 20,
}
def weighted_random_server(servers: dict[str, int]) -> str:
"""Select a server based on its weight (higher weight = more likely)."""
total_weight = sum(servers.values())
rand = random.uniform(0, total_weight)
cumulative = 0
for server, weight in servers.items():
cumulative += weight
if rand <= cumulative:
return server
return list(servers.keys())[-1]
if __name__ == "__main__":
random.seed(42)
num_requests = 10_000
selections = [weighted_random_server(SERVERS) for _ in range(num_requests)]
distribution = Counter(selections)
print(f"Requests: {num_requests}")
for server, weight in SERVERS.items():
actual = distribution[server] / num_requests * 100
print(f"{server}: weight={weight}%, actual={actual:.1f}%")
Output
Requests: 10000
server-a: weight=50%, actual=49.6%
server-b: weight=30%, actual=29.8%
server-c: weight=20%, actual=20.6%
How it works
The algorithm first totals all server weights, then generates a random float between 0 and that total. Each server adds its weight to a cumulative sum, and the first server whose cumulative sum surpasses the random value gets selected. This creates a direct proportion between weight and selection probability — a server with weight 50 is chosen roughly 50% of the time. Using random.seed(42) makes the output reproducible for testing.
Common mistakes
- Skipping error handling when how to build a weighted random load balancer in python can hide bad input.
- Forgetting to match the Python version if you use newer syntax.
Variations
- Wrap the core logic in a function and call it from `if __name__ == "__main__":` for reuse.
Real-world use cases
- Inside automation scripts and CLIs that need to how to build a weighted random load balancer in python as one step of a larger job.
- In data-cleaning or ETL pipelines where you how to build a weighted random load balancer in python before validating or storing records.
- In backend services and background workers that how to build a weighted random load balancer in python while processing requests or files.
Sponsored
More from System design patterns
- Build a BFF (Backend for Frontend) Mock Aggregator in Python medium
- Builder pattern for mocking complex objects in Python easy
- Circuit Breaker Pattern in Python: Closed, Open, and Half-Open States medium
- Create a Data Helper Class in Python easy
- Domain Driven Design Aggregate Root Example in Python medium
- Facade Pattern in Python with Mock Simplification medium
Keep learning
Related tutorials and quizzes for this topic.