What is Load Balancer in System Design: Explanation & Example
load balancer is a system component that distributes incoming network or application traffic across multiple servers to ensure no single server is overwhelmed. It improves reliability, scalability, and performance by balancing the workload evenly.How It Works
Imagine a busy restaurant with many customers arriving at the same time. Instead of all customers going to one waiter, the host directs each customer to different waiters to keep service smooth and fast. A load balancer works similarly by directing incoming requests to multiple servers.
It sits between users and servers, checking which servers are available and healthy. Then it sends each request to the best server based on rules like round-robin (taking turns), least connections, or server health. This way, no single server gets overloaded, and the system stays responsive.
Example
This simple Python example simulates a load balancer distributing requests to three servers using round-robin scheduling.
class LoadBalancer: def __init__(self, servers): self.servers = servers self.index = 0 def get_server(self): server = self.servers[self.index] self.index = (self.index + 1) % len(self.servers) return server # List of servers servers = ['Server1', 'Server2', 'Server3'] lb = LoadBalancer(servers) # Simulate 6 incoming requests for i in range(6): print(f'Request {i+1} sent to {lb.get_server()}')
When to Use
Use a load balancer when your system needs to handle many users or requests at the same time. It helps avoid slowdowns or crashes by spreading work across servers.
Common real-world cases include websites with high traffic, online stores during sales, or apps that must stay available 24/7. Load balancers also help when you want to add or remove servers without stopping the service.
Key Points
- A load balancer distributes traffic to multiple servers to improve performance and reliability.
- It uses algorithms like round-robin or least connections to decide where to send requests.
- It monitors server health and avoids sending requests to down servers.
- Load balancers enable scaling and high availability in systems.