Skip to main content

Networking and the web

What is a Load Balancer?

Definition

A load balancer spreads incoming requests across several backend servers, so no one server is overwhelmed and a failed one can be taken out of rotation.

Also known as: LB

Load Balancer in more detail

One server can only do so much, and one server is also a single point of failure. A load balancer solves both at once: clients connect to it, and it forwards each request to one of a pool of identical backends. Adding capacity becomes a matter of starting another instance and registering it, and losing an instance becomes a non-event rather than an outage.

Balancers work at one of two layers. A layer 4 balancer forwards TCP or UDP connections by address and port; it is fast and protocol-agnostic but cannot see inside the request. A layer 7 balancer terminates HTTP, so it can route by hostname, path, header or cookie, retry idempotent requests on another backend, and merge many client connections into a few pooled backend ones. Layer 7 costs a little latency for a lot of control.

Which backend gets a request is decided by an algorithm — round robin, least connections, weighted variants, or a hash of the client address — and by health checks that continuously probe each instance and remove the ones failing. The awkward case is state: if a user's session lives in one server's memory, requests must stick to that server, which undermines the whole point. The clean fix is to keep sessions in a shared store so any instance can serve any request.

Load Balancer example

                  ┌────────────┐
   clients ─────▶ │   load     │ ──▶ app-1  (healthy)
                  │  balancer  │ ──▶ app-2  (healthy)
                  └────────────┘ ──▶ app-3  (failing health
                                        check -> removed)

Health check
  GET /healthz every 5s, 2 failures -> out of rotation,
  2 successes -> back in

Algorithms
  round robin       each backend in turn
  least connections send to the least busy backend
  ip hash           same client -> same backend (sticky)
  weighted          bigger machines take a larger share

Load Balancer — common questions

What is the difference between layer 4 and layer 7 load balancing?
Layer 4 balances TCP or UDP connections without reading them, which is fast and works for any protocol. Layer 7 understands HTTP, so it can route on path, host, headers or cookies and retry failed requests, at the cost of terminating and re-issuing the connection.
What are sticky sessions?
A rule that keeps one client on one backend, usually via a cookie or a hash of the client IP. They are a workaround for state held in server memory; storing sessions in Redis or a database removes the need and lets any instance serve any request.
Is the load balancer itself a single point of failure?
It would be if you ran only one. Production setups use a redundant pair with failover, or a managed cloud balancer whose redundancy is handled by the provider and addressed through DNS or anycast.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary