AlgoViz

Design a Load Balancer

medium 45 min

Spread a shop's 100,000 requests a second over many servers.

Video lesson · Chapter 1 of 5

Three checkout workers share one address

0:00 / 3:39

A load balancer in 90 seconds

1:25Reel coming soon

Theory

Read or listen first. About 1 min.

What it is

A load balancer takes requests at one address and passes each to one of many servers. It stops sending work to servers that are sick, and it lets servers come and go unnoticed.

To design one, we build the data plane, which touches every request, and the control plane, which decides what the data plane knows.

Why it exists

One server has a ceiling and will fail one day. Several servers solve both, but only if something in front shares the work and notices failures.

That something now sees every request. If it is slow everything is slow, and if it stops everything stops. So the balancer needs the same care as the servers behind it.

A short history

  1. 1998

    The Linux Virtual Server project begins: layer 4 balancing on ordinary Linux machines.

  2. 2002

    The HAProxy site dates the project's use in high-traffic sites from this year.

  3. 2016

    Google publishes Maglev, a layer 4 balancer spread over many ordinary machines.

  4. August 2018

    TLS 1.3 (RFC 8446) shortens a full handshake to one round trip.

  5. April 2024

    RFC 9568 restates VRRP, the protocol with which two machines share one address.

Words to know

Virtual IP
The one address clients use. It belongs to the service, not to a machine.
Data plane
The part that handles each packet or request.
Control plane
The part that decides pools, rules and weights and tells the data plane.
Draining
No new work for a server, while the work it has is finished.
Slow start
A returning server gets a small share that grows.
Ejection
Taking a server out because real requests to it fail.
Direct server return
Replies skip the layer 4 balancer.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Arrive. Packets reach one address and stay with one proxy for the life of the connection

Understand it

The control tower
1one runway, three waiting

The controller does not fly the planes. It decides where the next one goes.

Scope

What are we building?

The balancing tier for a large shop: 100,000 requests a second at peak, HTTPS, several services behind one name. The basic ideas live in load balancing and layer 4 and layer 7. Here we put them together and size them.

  • Spread

    Each request to one healthy server with room.

  • Notice

    A failing server loses its traffic within a second or two.

  • Change

    Deploy servers, rules and certificates without dropped requests.

  • Survive

    Lose a proxy, a zone or a region.

Similar systems

SystemKindKnown for
AWS ALBManaged, layer 7Path rules, sign-in, cookies
AWS NLBManaged, layer 4Static addresses, many connections
Cloudflare, Google CloudManaged, globalBalancing across regions
NGINXSoftware, bothWeb server and proxy
HAProxySoftware, bothFast proxy with many checks
EnvoySoftware, bothService mesh, rich metrics
TraefikSoftware, layer 7Finds services by itself
F5 BIG-IPApplianceHardware in data centres

The finished system

Where the design ends up

This is the last version, in one region. The Design tab starts with one server and one database.

One region of the system

Shoppers look up the nearest region in DNS, then connect to one address held by three layer 4 nodes. These pass each connection to one of nine layer 7 proxies, which open the encryption, match rules and send requests over pooled connections to thirty shop servers. A control plane reads PostgreSQL and pushes configuration to the proxies, which report to Prometheus.
Blue is the request path, pink configuration, orange lookups and metrics. Dashed arrows do not delay a request.

Where to read what

QuestionPlace
How do I open this interview?Approach
How big is it?Numbers
How does it grow?Design
Health, failover, TLS?Deep dives
Can I practise it?Mock

Summary

The whole topic in one minute.

A load balancer is a small decision made very often: which healthy server takes this request? Proxies decide alone, using their own counts and a copy of the configuration.

The design grows because of failure. Several proxies, layer 4 in front so that a dead proxy is bypassed, ejection in a second, draining and slow start, versioned configuration and a second region each remove one named break.

  • Size for the failure: what is left after losing a zone must carry the peak.
  • Layer 4 keeps one address alive; layer 7 makes the clever choice.
  • Proxies count their own requests. Sharing counts costs more than it gives.
  • Probes find dead servers in many seconds; real failures find them in one.
  • When most servers look sick, distrust the checks and spread the load.

Sources