Load Balancer Algorithms
The algorithm determines HOW incoming requests are distributed across servers. Choosing the right one can significantly impact performance and reliability.
1The Restaurant Host Analogy
- • Round Robin: Table 1, then 2, then 3... repeat
- • Least Connections: Seat at the table with fewest people
- • Weighted: Big tables get more people than small ones
- • IP Hash: Regulars always get their favorite table
Load Balancing Algorithm is the decision-making logic that determines which backend server receives each incoming request. The choice affects response times, resource utilization, and system reliability.
2Round Robin
The simplest algorithm. Requests are distributed sequentially to each server in a loop.
server_index = 0
servers = [A, B, C]
function get_next_server():
server = servers[server_index]
server_index = (server_index + 1) % len(servers)
return server- • Dead simple to implement
- • No server state needed
- • Fair distribution with equal servers
- • Predictable behavior
- • Ignores server capacity differences
- • Ignores current server load
- • Can overload slow servers
- • No session persistence
Stateless services with identical servers and uniform request processing times. Perfect for read-heavy APIs.
3Weighted Round Robin
Like Round Robin, but servers with higher weights receive proportionally more requests.
Pattern: A → A → A → A → B → B → C → (repeat)
servers = [
{name: 'A', weight: 4}, // 4x traffic
{name: 'B', weight: 2}, // 2x traffic
{name: 'C', weight: 1} // 1x traffic
]
// Expand into: [A,A,A,A,B,B,C]
// Then round-robin through expanded listWhen you have servers with different hardware specs (CPU, RAM) or when some servers are dedicated to specific tasks.
4Least Connections
Routes each request to the server with the fewest active connections. Smart for varying request durations.
New request → Server B (fewest connections)
- • Adapts to actual load
- • Great for varying request times
- • Handles slow requests well
- • More even utilization
- • Requires tracking connections
- • More complex than Round Robin
- • May thrash with many short requests
- • Slight overhead
Long-lived connections (WebSockets), file uploads, database connections, or any workload with varying request durations.
5IP Hash (Sticky Sessions)
Uses a hash of the client's IP address to consistently route them to the same server. Enables session persistence.
192.168.1.100→hash(...) % 3 = 0→Server A10.0.0.55→hash(...) % 3 = 2→Server C192.168.1.100→hash(...) % 3 = 0→Server A (same!)- • Session persistence without cookies
- • Stateful app support
- • Cache-friendly routing
- • Simple to implement
- • Uneven distribution possible
- • Server changes break sessions
- • IP can change (mobile)
- • NAT issues (many users → 1 IP)
For session persistence, consider using external session storage (Redis) instead. This keeps your application stateless and more scalable.
6Least Response Time
Routes to the server with the fastest response time AND fewest connections. Best of both worlds.
| Server | Connections | Avg Response | Score |
|---|---|---|---|
| A | 10 | 50ms | 500 |
| B ← | 8 | 30ms | 240 |
| C | 5 | 80ms | 400 |
Score = Connections × Response Time (lower is better)
Performance-critical applications where response time matters more than even distribution.
7Algorithm Comparison
| Algorithm | Complexity | State | Best For |
|---|---|---|---|
| Round Robin | O(1) | Index only | Equal servers, uniform requests |
| Weighted RR | O(1) | Weights + Index | Different server capacities |
| Least Connections | O(n) | Connection counts | Varying request durations |
| IP Hash | O(1) | None | Session persistence |
| Least Response | O(n) | Times + Counts | Performance-critical apps |