What is Load Balancing?
Load balancing distributes incoming network traffic across a cluster of servers to:- Maximize throughput: Utilize multiple servers’ computing resources
- Minimize response time: Route requests to available servers
- Avoid overload: Prevent any single server from becoming a bottleneck
- Ensure high availability: Continue serving requests even if servers fail
Load Balancing Approaches
- HTTP Redirect
- DNS Load Balancing
- Reverse Proxy
HTTP Redirect Load Balancing
Mechanism: Load balancer returns HTTP 302 redirect to selected application serverHow It Works
Simple Implementation
Simplicity vs. Practicality
This approach can be implemented in less than 10 lines of Java code, making it extremely simple. However, it’s rarely used in production due to significant drawbacks.
Advantages
Simple Design
Easy to implement with minimal code
No Proxy Overhead
Load balancer doesn’t handle response traffic
Disadvantages
Industry Practice: HTTP redirect load balancing is rarely used in production. Modern systems prefer DNS load balancing combined with internal HTTP load balancers.
Load Balancing Strategies
Algorithms for selecting which server handles each request:Round Robin
Round Robin
Pattern: Distribute requests sequentially in circular orderHow it works:✅ Pros:
- Simple to implement
- Fair distribution
- No server state required
- Doesn’t account for server capacity
- Ignores current server load
- May overload slower servers
Weighted Round Robin
Weighted Round Robin
Pattern: Round robin with different weights per serverHow it works:Configuration example:✅ Pros:
- Accounts for different server capacities
- Better resource utilization
- Flexible configuration
Random
Random
Pattern: Randomly select server for each requestHow it works:✅ Pros:
- Simple implementation
- Stateless
- Naturally distributes over time
- Uneven distribution in short term
- No capacity awareness
Least Connections
Least Connections
Pattern: Route to server with fewest active connectionsHow it works:✅ Pros:
- Dynamic load awareness
- Better for long-lived connections
- Adapts to varying request durations
- Requires connection tracking
- More complex state management
IP Hash
IP Hash
Pattern: Hash client IP to consistently route to same serverHow it works:✅ Pros:
- Session affinity without cookies
- Consistent routing per client
- Simplified session management
- Uneven distribution if client IPs cluster
- Server changes require rehashing
- Not suitable behind proxies/NAT
Least Response Time
Least Response Time
Pattern: Route to server with fastest response timeHow it works:
- Track average response time per server
- Send new requests to fastest server
- Continuously update metrics
- Performance-aware routing
- Automatically avoids slow servers
- Optimizes user experience
- Complex metric collection
- Requires health monitoring
- Can create hot spots
Comparison Matrix
| Strategy | Complexity | State Required | Distribution | Use Case |
|---|---|---|---|---|
| Round Robin | Low | None | Even | Homogeneous clusters |
| Weighted RR | Low | Weights only | Proportional | Heterogeneous clusters |
| Random | Very Low | None | Statistical | High-volume traffic |
| Least Connections | Medium | Connection counts | Dynamic | Variable request times |
| IP Hash | Medium | Hash table | IP-based | Session persistence |
| Least Response Time | High | Metrics + health | Performance-based | Geographic distribution |
Design Considerations
Health Checks
Essential for reliability:
- Active health probes
- Passive failure detection
- Automatic server removal
- Graceful re-introduction
Session Persistence
Maintain user sessions:
- Sticky sessions (cookie-based)
- IP hash routing
- Shared session storage (Redis)
- Stateless design (JWT)
SSL Termination
Handle encryption at load balancer:
- Reduce backend server load
- Centralized certificate management
- Simpler backend configuration
- May decrypt sensitive data
High Availability
Eliminate single point of failure:
- Active-passive LB pairs
- Active-active with shared VIP
- DNS-level LB failover
- Health check redundancy
Common Patterns
- Global + Regional
- Layer 4 + Layer 7
Multi-Tier Geographic Load Balancing
Benefits:- Reduced latency (geographic proximity)
- Regulatory compliance (data residency)
- Disaster recovery across regions
Best Practices
Use reverse proxy for application tier
Nginx/HAProxy to distribute to application servers with private IPs
Related Topics
Service Discovery
Dynamic service registration for automatic load balancer updates
Message Queues
Asynchronous load distribution through queuing