Skip to content
System Design Explorer
networking

Load Balancer

By Ankur NayakReviewed

A load balancer spreads incoming requests across a pool of servers so no single machine is overwhelmed, and routes around servers that fail their health checks. It is the entry point that makes horizontal scaling possible.

Load Balancer diagramComponents: Clients, Load Balancer (L7), Server A, Server B, Server C. Connections: Clients to Load Balancer; Load Balancer to Server A; Load Balancer to Server B; Load Balancer to Server C.ClientsClientLoad BalancerL7Server AServiceServer BServiceServer CService
Health-checked request distribution

How it works

Clients talk to one stable address. Behind it, the load balancer picks a healthy backend for each connection (layer 4) or for each HTTP request (layer 7), using an algorithm such as round robin, least connections or a hash of some request attribute.

Because backends sit behind it, you can add, remove or restart servers without clients noticing. Health checks take failing instances out of rotation automatically, which turns a server crash into a non-event for users.

Layer 7 balancers understand HTTP, so they can also terminate TLS, route by path or header, retry idempotent requests and add observability. Layer 4 balancers only see TCP/UDP flows, which makes them faster and protocol-agnostic, and a good fit for long-lived connections such as WebSockets.

Key points

  • L4 balances connections; L7 balances requests and can route by path, header or cookie
  • Health checks remove failing backends from rotation automatically
  • Usually terminates TLS so backends handle plain HTTP
  • Sticky sessions trade even load for simpler stateful backends

When to use it, and pitfalls

Use it for

  • Any stateless service running on more than one instance
  • In front of connection-holding gateways (chat, push), using L4 or least-connections

Watch out for

  • The load balancer itself must be redundant: an active/passive pair, anycast, or a managed multi-zone service
  • Round robin ignores request cost; one slow endpoint can overload a backend
  • Long-lived connections stay where they landed, so new servers fill slowly after a scale-out

Trade-offs

Layer 4 vs. layer 7

Use L7 for HTTP APIs and L4 for raw TCP or very high connection counts.

Pros: L7: content-based routing, retries, TLS termination; L4: lower latency, any protocol

Cons: L7: more CPU per request; L4: no visibility into requests

Used in

Frequently asked questions

What is the difference between a load balancer and an API gateway?

A load balancer spreads traffic across identical instances of one service. An API gateway sits at the edge and routes requests to many different services, adding cross-cutting concerns such as authentication, rate limiting and request shaping. Gateways are usually themselves behind a load balancer.

Which load balancing algorithm should I use?

Round robin is fine when requests cost about the same. Least connections suits requests of very different cost or long-lived connections. Hash-based routing keeps a given key on the same backend, which helps caches and stateful services.

Sources