A load balancer has one deceptively simple job: find a healthy place for the next request. The layer it operates on changes how much of that request it can understand.
L4 vs L7 Load Balancing
Layer 4 (Transport)
- Works at TCP/UDP level
- Fast, low overhead
- Can't inspect HTTP content
Layer 7 (Application)
- Works at HTTP level
- Can route based on URL, headers, cookies
- Supports advanced features like authentication
Common Algorithms
- Round Robin: Simple rotation through servers
- Weighted Round Robin: Some servers get more traffic
- Least Connections: Send to server with fewest active connections
- IP Hash: Consistent routing based on client IP
Health Checks
Always configure health checks:
# HTTP health check example
GET /health HTTP/1.1
Host: backend-server
# Expected response
HTTP/1.1 200 OK
{"status": "healthy"}
Remove unhealthy instances from rotation, but alert on failures!