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.
Load Distribution Engine
Visualize how incoming traffic is distributed across backend servers.
Round Robin guarantees an equal number of requests sent to each server over time. However, it blindly sends traffic without considering the actual load (active connections) on the servers, which can lead to imbalance if some requests take longer to process than others.
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).
Direct Server Return (DSR) Implementation
In a traditional load balancer, all traffic passes through the balancer in both directions. In DSR, only the request goes through the load balancer. The server responds directly to the client, bypassing the balancer for the return path.
To implement DSR, the backend servers must be configured with a Loopback interface that shares the same IP address as the Load Balancer's VIP (Virtual IP). The server accepts the packet (which is still addressed to the VIP) and sends the response back with the VIP as the source address.
Health Check Depth: L3 vs L4 vs L7
A load balancer is only as good as its health checks. Engineers must choose the level of verification:
- L3 (ICMP): Checks if the server is reachable. It doesn't mean the service is running.
- L4 (TCP Handshake): Checks if the port is open. It doesn't mean the application is healthy (it could be returning 500 errors).
- L7 (HTTP Content): The gold standard. The balancer fetches a specific URL and looks for a 200 OK status and a specific string in the body.
Anycast Load Balancing
Anycast is a routing methodology where the same IP address is advertised from multiple geographic locations. Routers at the BGP level send packets to the "nearest" location (based on hop count or latency).
- Edge Optimization: Used by CDNs (Cloudflare, Akamai) to ensure users reach the closest data center.
- DDoS Mitigation: In an attack, traffic is naturally distributed (and absorbed) across many global nodes rather than hitting a single target.
Global Server Load Balancing (GSLB)
GSLB uses DNS to intelligentlly direct traffic across different data centers. Unlike Anycast (which is BGP-based), GSLB relies on the DNS resolver to return different IP addresses based on the user's location or server health.
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.