How to Mock a Server-Side Load Balancer in Python

A simple Python class that mimics a server-side load balancer with round-robin, random, and least-connections selection strategies.

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

Python code

34 lines
Python 3.9+
import itertools
import random

class LoadBalancer:
    def __init__(self, servers=None):
        self.servers = servers if servers else ["server1", "server2", "server3"]
        self.counter = itertools.count(1)

    def round_robin(self):
        return next(self.counter) % len(self.servers)

    def random_selection(self):
        return random.randint(0, len(self.servers) - 1)

    def least_connections(self):
        return self.connections.index(min(self.connections))

    def get_server(self, algorithm="round_robin"):
        if algorithm == "round_robin":
            index = self.round_robin()
        elif algorithm == "random":
            index = self.random_selection()
        elif algorithm == "least_connections":
            index = self.least_connections()
        else:
            raise ValueError(f"Unknown algorithm: {algorithm}")
        return self.servers[index]


if __name__ == "__main__":
    lb = LoadBalancer()
    print("Round robin:", lb.get_server())
    print("Round robin:", lb.get_server())
    print("Random:", lb.get_server())

Output

stdout
Round robin: server1
Round robin: server2
Random: server3

How it works

The LoadBalancer class uses an itertools.count iterator for round-robin selection, ensuring a cyclic distribution across servers. The random_selection method picks a random index for unbiased load distribution. The least_connections method assumes a connections attribute exists; you would typically track active connection counts per server and select the one with the fewest. The get_server method dispatches to the chosen algorithm and returns the corresponding server name. This mock demonstrates the core logic of a load balancer without actual network traffic.

Common mistakes

  • Forgetting to add a `connections` list when using the least-connections algorithm
  • Not resetting the round-robin counter when server list changes
  • Using random selection when deterministic behavior is needed for testing

Variations

  1. Use `random.choice(self.servers)` for simpler random selection
  2. Implement weighted round-robin by assigning each server a weight

Real-world use cases

  • Simulating load distribution across backend instances in unit tests for microservices.
  • Prototyping a custom load balancer strategy before integrating with a cloud provider.
  • Balancing requests in a local development environment across multiple service replicas.

Sponsored

Run this sample

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

Open editor

More from Microservices patterns

Related tutorials and quizzes for this topic.