← System Design
Core Concepts

What is a Load Balancer?

A load balancer distributes incoming requests across a pool of servers so no single server is overloaded, and stops sending traffic to servers that are unhealthy. It is what lets you run ten identical app servers behind one address and add or remove servers without clients noticing. Examples: AWS ALB/NLB, Google Cloud Load Balancing, HAProxy, Nginx, Envoy.

In simple words

Like the person at a bank who points each customer to the next free counter, and stops sending people to a counter whose clerk has gone home. No single counter gets a huge queue while others sit idle.

Load balancer with health checksClientsLoad balancerServer 1Server 2Server 3(unhealthy)requestrequesthealth checkfails
Traffic is spread over healthy servers only; a failed health check takes a server out of rotation.

Why It Exists

How It Works

             +------------------+
clients ---> |  load balancer   | -- health checks every few seconds
             +------------------+
               |      |      |
             app1   app2   app3 (unhealthy -> removed from rotation)

Algorithms for picking a server:

Types: Layer 4 vs Layer 7

Layer 4 (transport)Layer 7 (application)
SeesIPs and ports (TCP/UDP)HTTP: path, host, headers, cookies
Routingper connectionper request (/api vs /images, canary by header)
Speedvery fast, little processingmore CPU (parses HTTP, often terminates TLS)
ExamplesAWS NLB, LVSAWS ALB, Nginx, Envoy

When to Use It (and What to Watch)

Key Pointers

Follow-up Questions

How does the load balancer know a server is unhealthy?
Active health checks (it calls /health every few seconds and removes a server after N failures) and often passive checks (it notices errors or timeouts on real traffic).
Why might round robin overload one server?
If some requests are much heavier (a report export vs a ping), a server can get several heavy ones in a row while others idle. Least-connections or least-outstanding-requests adapts to actual load.
What is connection draining?
When a server is removed (deploy, scale-in), the balancer stops sending it new requests but lets in-flight ones finish, so users don't see errors.

Related