Stage 4 · Provision
Load Balancing & Traffic Management
Load Balancing Algorithms
Round-robin, least connections, consistent hashing, and when to use each strategy.
Why Load Balancing?
Load balancing distributes incoming traffic across multiple backend servers. Without it, all traffic hits a single server, creating a bottleneck and single point of failure. The load balancing algorithm determines which server receives each request.
Round Robin
Round robin cycles through servers in order, sending each request to the next server. It is the simplest algorithm and works well when all servers have equal capacity and all requests have equal cost.
Request 1 ──► Server A
Request 2 ──► Server B
Request 3 ──► Server C
Request 4 ──► Server A
Request 5 ──► Server B
Request 6 ──► Server CRound robin is predictable and fast but ignores server load. If one server is slower or has more connections, it will receive the same number of requests as faster servers.
Least Connections
Least connections routes each request to the server with the fewest active connections. This adapts to real-time server load and is ideal for workloads where request duration varies significantly.
- Best for long-lived connections (WebSockets, database connections).
- Requires tracking connection count per server.
- More overhead than round robin but much more accurate load distribution.
- Can cause hot servers if connections are short and bursty.
IP Hash
IP hash uses the client IP address to determine which server receives the request. This provides session affinity — the same client always hits the same server. Useful for applications that store session state locally.
Consistent Hashing
Consistent hashing maps both servers and request keys onto a hash ring. When a server is added or removed, only the keys that mapped to that server are redistributed. This minimizes cache invalidation during scaling events.
Hash Ring
┌─────────────────┐
│ Server A │
│ ●──────────────┤
│ │ │
│ Server B Server C
│ ●──────────● │
│ │
└─────────────────┘
Key "user:123" ──hash──► lands between A and B
→ routed to Server B
Add Server D between B and C:
Only keys between B and C move to D
All other keys stay putConsistent hashing is used by Cassandra, DynamoDB, Memcached, and CDNs. It ensures that scaling does not cause a complete cache flush.
Plain consistent hashing can cause uneven distribution. Virtual nodes (vnodes) assign multiple positions on the ring to each server, improving balance. Cassandra uses 256 vnodes per node by default.
Weighted Algorithms
Weighted algorithms assign different proportions of traffic based on server capacity. A server with 2x the capacity receives 2x the traffic. This is useful when your fleet has heterogeneous hardware.
Most production load balancers use layered algorithms: least connections within a region, consistent hashing for cache affinity, and weighted distribution for capacity management. Choose the algorithm that matches your specific constraint.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.