Load Balancing Strategies Explained
Load balancing distributes traffic across multiple servers. Here’s how different strategies work and when to use them.
Why Load Balance?
- Scalability: Handle more traffic
- Availability: Survive server failures
- Performance: Route to fastest server
Algorithms
Round Robin
Distribute requests sequentially:
Request 1 → Server A
Request 2 → Server B
Request 3 → Server C
Request 4 → Server A (cycle repeats)
|
|
Best for: Homogeneous servers, stateless applications.
Weighted Round Robin
Account for server capacity:
|
|
Best for: Servers with different capacities.
Least Connections
Route to server with fewest active connections:
|
|
Best for: Long-lived connections, varying request complexity.
IP Hash
Same client always hits same server:
|
|
Best for: Session affinity without external session storage.
Least Response Time
Route to fastest responding server:
|
|
Best for: Performance-critical applications.
Health Checks
Don’t send traffic to dead servers:
|
|
Active health checks (Nginx Plus/HAProxy):
|
|
Layer 4 vs Layer 7
Layer 4 (Transport)
Routes based on IP and port:
Client → L4 LB → Server (TCP connection passed through)
- Faster (no packet inspection)
- Protocol agnostic
- Less intelligent routing
Layer 7 (Application)
Routes based on HTTP content:
Client → L7 LB → Server (HTTP request inspected)
|
|
- Content-based routing
- SSL termination
- Request modification
SSL Termination
Offload encryption to load balancer:
|
|
Common Setups
Nginx
|
|
HAProxy
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /health
server app1 192.168.1.1:8080 check
server app2 192.168.1.2:8080 check
Cloud Load Balancers
- AWS: ALB (Layer 7), NLB (Layer 4)
- GCP: HTTP(S) LB, TCP/UDP LB
- Azure: Application Gateway, Load Balancer
Choose based on your traffic patterns and requirements.
Comments