Load Balancing Algorithms
how to pick which instance gets the requestGiven a pool of healthy instances, the load balancer picks one per request. The algorithm choice matters when requests aren't uniform โ some are slow, some instances are bigger, some clients benefit from landing on the same node twice.
| Algorithm | How it works | Best for |
|---|---|---|
| Round robin | Next instance in rotation | Uniform requests, equal instances |
| Weighted | Round robin biased by capacity | Mixed instance sizes |
| Least connections | Fewest active connections wins | Long-lived or uneven-duration requests |
| Least response time | Fastest-responding instance wins | Latency-sensitive, variable backends |
| IP / consistent hash | Hash the key to a stable instance | Session affinity, cache locality |
L4 vs L7
how deep the balancer looksA load balancer can operate at two layers. L4 works at the transport level โ it sees IPs and ports, forwards packets, and never opens the payload. L7 works at the application level โ it terminates the connection, reads the HTTP request, and can route on path, header, or cookie.
L4 (transport) L7 (application) sees: IP : port sees: full HTTP request โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ forward the โ โโโถ backend โ GET /api/* โโโถ api pool โ โ TCP packets โ (blind to โ GET /img/* โโโถ cdn pool โ โ fast, cheap โ content) โ header: beta โโโถ canary โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ can't route by URL, routing, TLS termination, can't retry a request retries, rewrites โ at a cost
| L4 | L7 | |
|---|---|---|
| Sees | IP, port, TCP | Full HTTP: path, headers, cookies |
| Can do | Fast forwarding, any protocol | Path routing, TLS termination, retries, rewrites |
| Cost | Minimal overhead | Terminates and parses โ more CPU |
| Use when | Raw throughput, non-HTTP traffic | Smart routing on HTTP semantics |
Health Checks
knowing which instances can take trafficThe balancer only routes to instances it believes are healthy, so it constantly probes them. Active checks send a periodic request and watch for a good response. Passive checks infer health from real traffic โ a burst of errors or timeouts marks an instance down without a separate probe.
Checks also vary in depth. A shallow check confirms the process is up ("return 200 on /health"). A deep check verifies dependencies too โ can it reach the database, the cache, the downstream service.
Service Discovery
finding instances that come and goIn a dynamic fleet, instances start, stop, crash, and autoscale constantly. Nobody can hard-code addresses. A service registry is the live directory: instances register on startup and send heartbeats; they deregister on shutdown, or the registry evicts them when heartbeats stop. Callers ask the registry where a service lives.
The client queries the registry, gets the list of instances, and load-balances itself. Fewer hops and full control, but every client needs the discovery logic and a fresh view of the registry.
The client just calls a stable load balancer, which consults the registry and forwards. Clients stay dumb; the routing tier owns discovery. One more hop, one more thing to run.
API Gateway
edge concerns in one placeAn API gateway is the single front door to a system of services. It handles the cross-cutting edge concerns you don't want scattered across every service: authentication, rate limiting, routing, request shaping, and response aggregation. One place to enforce policy, one place clients target.
clients
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API GATEWAY โ
โ authn ยท rate limit ยท routing ยท shaping โ
โโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โผ โผ โผ
user svc order svc search svc (internal services stay focused
on business logic, not edge policy)
The terms blur on purpose. A reverse proxy forwards requests to backends. A load balancer spreads them across instances. An API gateway adds application-aware policy on top. In practice one component often does all three, which is why the words get used interchangeably โ the distinction is the responsibility, not the box (Security Basics, Rate Limiter).
Global Load Balancing
routing users across regionsEverything above balances within one region. Across regions, two mechanisms send a user to the right one. GeoDNS answers a DNS query with the nearest region's address based on the resolver's location. Anycast advertises one IP from many locations, and the network routes packets to the closest โ routing at the network layer, so it reacts faster than DNS and needs no TTL wait.
Quick Reference
algorithms and layers on one card| Algorithm | How it works | Best for |
|---|---|---|
| Round robin | Rotate through instances | Uniform requests โ the default |
| Weighted | Biased by capacity | Mixed instance sizes |
| Least connections | Fewest active wins | Variable request durations |
| Least response time | Fastest wins | Latency-sensitive backends |
| Consistent hash | Key โ stable instance | Session affinity, cache locality |
| L4 | L7 | |
|---|---|---|
| Sees | IP, port | Full HTTP |
| Can do | Fast forwarding | Path routing, TLS term, retries |
| Use when | Throughput, any protocol | Smart HTTP routing |