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.

Easy Python 3.9+ Aug 9, 2026 System design patterns 13 views 0 copies

Python code

32 lines
Python 3.9+
import 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

stdout
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

  1. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from System design patterns

Related tutorials and quizzes for this topic.