Try
↑ ↓ move↵ openesc close

Interview prep / Load balancing

Junior · Load balancing · System design interview question

Requests for /api should go to one pool of servers and requests for /images to another, and TLS must end at the edge. Which kind of load balancer do you need?

Sketch your own answer first, then compare it with the one below.

A strong answer

You need a layer 7 balancer, one that understands the application protocol. A layer 4 balancer works on connections and sees only addresses, ports and the transport protocol. It can pick a backend for each connection, but it cannot read the URL path, so it cannot split /api from /images.

A layer 7 proxy ends the client connection, parses the HTTP request, and chooses a backend for each request using the path, host, headers or cookies. That also lets it terminate TLS, retry idempotent requests on another backend, and spread the many requests that arrive on one HTTP/2 connection across several servers. A layer 4 balancer would pin the whole connection to one backend.

The cost is CPU. Parsing, and often re-encrypting, every request takes far more work than forwarding packets, so a layer 7 tier needs more machines for the same traffic.

Large sites combine the two. A thin layer 4 tier spreads connections over a fleet of layer 7 proxies, and the proxies make the per-request decisions. For a small service, one layer 7 proxy pair is simpler. Plain layer 4 is the right pick for protocols the proxy does not understand.

Sources

Learn it by running it

The load balancing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms