Load Balancing Strategies
[!NOTE] This module explores the core principles of Load Balancing Strategies, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. What is a Load Balancer?
A load balancer acts as a “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests. This ensures no single server is overworked and improves availability.
2. Layer 4 vs. Layer 7
The “Layer” refers to the OSI model layer at which the load balancer makes its decisions.
| Feature | Layer 4 (Transport) | Layer 7 (Application) |
|---|---|---|
| Visibility | IP and Port only. | HTTP Headers, Cookies, URL path. |
| Speed | Extremely fast (less processing). | Slower (requires TLS decryption). |
| Logic | Simple: “Send to Port 80.” | Smart: “Send /images to Server A.” |
| Standard | NLB (Network Load Balancer). | ALB (Application Load Balancer). |
3. Common Algorithms
- Round Robin: Requests are distributed sequentially (Server 1, then 2, then 3).
- Least Connections: Sends the next request to the server with the fewest active sessions. Great for long-lived tasks.
- IP Hash: Uses a hash of the client’s IP address to ensure a specific user always hits the same server (Session Stickiness).
4. Health Checks
A load balancer must know if a server is actually alive.
- TCP Check: “Can I open a connection on port 80?”
- HTTP Check: “Does a request to
/healthreturn a200 OK?” If a server fails a health check, it is removed from the rotation until it recovers.
5. Interactive: Balancing Algorithms
Watch how traffic flows based on the strategy.
6. SSL Offloading
Decrypting HTTPS traffic requires significant CPU power. A load balancer can “offload” this work by decrypting the SSL traffic at the balancer level and sending plain HTTP to the internal servers. This keeps the internal servers fast and simplifies certificate management.