Load Balancing Algorithms
Distributing Traffic for High Availability
The Role of the Load Balancer
A load balancer acts as a reverse proxy, distributing incoming network or application traffic across a pool of servers. This prevents any single server from becoming a bottleneck and ensures system Reliability.
Common Algorithms
1. Round Robin
Distributes server requests in sequential order. Simple and effective for pools where all servers have identical hardware.
2. Least Connections
Sends traffic to the server with the fewest active sessions. Ideal for applications where session duration varies significantly.
3. IP Hash
The client's IP address is used to calculate a hash which determines which server receives the request. This provides 'natural' persistence without requiring cookies.
4. Weighted Least Connections
Similar to Least Connections, but accounts for the relative power (weight) of each server. A server with a weight of 10 will receive twice the connections of a server with a weight of 5.
L4 vs. L7 Balancing
Layer 4 (Transport): Decisions are based on IP and Port numbers. Fast, but blind to the content of the request.
Layer 7 (Application): Decisions are based on URL paths, HTTP Headers, or Cookie data. High CPU overhead, but allows for advanced routing (e.g., sending /images to one pool and /api to another).
Conclusion
Choosing the right algorithm depends on the application's nature. Stateless REST APIs thrive on Round Robin, while stateful legacy applications often require IP Hash or cookie-based persistence.