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.
Python code
34 linesimport 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
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
- Use `random.choice(self.servers)` for simpler random selection
- 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
More from Microservices patterns
- BFF aggregation pattern: combine multiple service responses in Python easy
- Backward Compatible Schema Evolution in Python medium
- Bulkhead Thread Pool per Service Mock in Python medium
- CQRS with Separate Read and Write Repositories in Python medium
- Cache-Aside Pattern in Python: Per-Service Mock easy
- Consumer Driven Contract Pact Mock in Python medium
Keep learning
Related tutorials and quizzes for this topic.