Services

Resources

Company

Load balancing

Load balancing spreads incoming requests across several servers or containers that run the same service, so no single one is overloaded and the service keeps answering when one of them fails. A load balancer sits in front of the pool, accepts each connection or request, and forwards it to a healthy backend chosen by an algorithm.

How it works

Common algorithms are round robin (each backend in turn), least connections (the backend with the fewest open connections) and hashing on a key such as the client IP, so the same client keeps landing on the same backend. Health checks take failed backends out of rotation and put them back when they recover. A layer 4 (L4) load balancer forwards TCP or UDP connections using only addresses and ports; a layer 7 (L7) load balancer understands HTTP, so it can route by host, path or header, retry failed requests and terminate TLS.

In the SRE bootcamp's bare-metal exercise, Nginx listens on port 8080 and balances between two API containers with upstream api { server api1:8080; server api2:8080; } and proxy_pass http://api;. With the default round robin, 1,000 requests a minute become about 500 for each container. If api2 stops responding, open source Nginx marks it unavailable after a failed attempt (max_fails=1 by default), sends every request to api1 for the next 10 seconds (fail_timeout=10s), then tries api2 again. While api2 stays down, api1 carries all 1,000 requests a minute.

Why it matters in production

A load balancer only helps if the remaining backends can carry the load when one fails. Two containers each at 70% CPU cannot cover for each other, because the survivor would need 140%. Size the pool so it can lose one backend, keep backends stateless or store sessions outside them, and monitor the load balancer itself, since it is now on the path of every request and needs its own high availability.

See also

On the One2N blog

References

  1. Using nginx as HTTP load balancer (nginx documentation)

Four-panel comic about load balancing: one API container is drowning in 1,000 requests a minute; a load balancer in front of two containers picks a healthy one for each request, and L4 sees ports while L7 sees HTTP; Nginx on port 8080 sends about 500 a minute to each, and when api2 fails all 1,000 go to api1; Asha sizes the pool so one container can carry the load alone.