Topic 092 / Core
Load balancing and its algorithms
Chapter 6.3 · Scaling Services
What and why
Once a service is stateless (→ 91) and running as multiple instances (→ 90), something must decide, for each incoming request, which instance handles it, and keep that decision fair and healthy as instances come and go. A load balancer sits in front of the instances, distributing requests by an algorithm and continuously checking that each instance it sends traffic to can actually serve it. Builds on: → 91 (stateless and stateful services).
Diagram
┌─► instance A (healthy)
client ─► LB ────┼─► instance B (healthy)
└─► instance C (failing health check — removed from rotation)
| Algorithm | Decides by | Good when |
|---|---|---|
| Round robin | next instance in a fixed rotation | instances and requests are roughly equal cost |
| Least connections | the instance with fewest active requests | request cost varies, so an even rotation isn't an even load |
| Consistent hashing (→ 74) | a hash of a request key (e.g. user ID) | the same key should keep landing on the same instance |
How it works
- Every registered instance is health-checked on an interval; a failing instance is removed from rotation before a human intervenes.
- For each request, the algorithm picks a healthy instance: round robin cycles through them in order; least connections picks whichever has the fewest in-flight requests; consistent hashing (→ 74) routes by a hash of some request attribute so the same value keeps landing on the same instance.
- The load balancer forwards the request, returns the response, and from the client's view there is one address, not many instances.
- The load balancer itself must not be a single point of failure: run at least two, with a mechanism above them (DNS, Anycast → 94, or a floating IP) deciding which is live.
- Load balancing composes with auto scaling (→ 95): new instances register as they come up and are removed as they're torn down — the balancer's view of "healthy instances" is the actual, current fleet, not a static list.
Practical example
(numbers illustrative). Three instances behind round robin receive 300 requests — 100 each, regardless of cost. If one request type on instance B happens to take 5× longer than average, round robin still sends B its even 1-in-3 share, and B's queue backs up. Switching to least connections, the same 300 requests redistribute toward whichever instance has fewer requests in flight — A and C, which finish faster, progressively receive more of the 300 than B, straightening the queue depths back out with no change to B's actual processing time.
When to use it
- Use round robin when request cost is roughly uniform — simplest to reason about.
- Use least connections when request cost varies meaningfully.
- Use consistent hashing (→ 74) when the same key must reliably reach the same instance.
- Avoid a single load balancer instance in production, and avoid picking an algorithm without first checking whether request cost is actually uniform.
- Failure scenarios: health checks that pass while the instance is actually failing real
requests (checking a static
/healththat never touches the real dependency); the load balancer itself becoming the bottleneck or single point of failure it was meant to remove; hash-based routing concentrating load on one instance when the key distribution is skewed (→ 88, the same problem one layer up). - Simpler alternative: for a genuinely single-instance service, no load balancer at all — add one only once there is more than one instance to balance across.
Practice
(mechanism kind — trace by hand, then simulate; 20 min, any language).
- By hand, trace round robin and least connections for the practical example's numbers (3 instances, 300 requests, instance B 5× costlier) and sketch how each algorithm's queue depths diverge over time.
- Write a short script simulating 3 workers and 100 tasks with randomly varying costs; compare total drain time under round robin vs. least connections.
- Design a health check for a real dependency (e.g., "can reach the database") rather than a static OK, and explain what failure mode the static version would miss.
Done when your simulation shows least connections finishing the skewed workload faster than round robin, matching the by-hand prediction.
Key takeaway
A load balancer turns many stateless instances (→ 91) into one addressable service, continuously routing around unhealthy ones; the algorithm should match whether request cost is uniform. Done when you can: pick an algorithm from a given cost distribution, and design a health check that verifies the dependency that actually matters. Used next in → 93 (L4 vs. L7, where in the stack the balancer operates), → 98 (graceful shutdown, how an instance leaves rotation cleanly); Project 4 (no single gateway instance).
References
None — this topic makes no external claims.
Source directory