← Back to System Design, page 1

system-design-0006 · Medium fresher

How does a load balancer work? Compare Layer 4 and Layer 7 load balancing and common balancing algorithms.

A load balancer accepts client traffic and forwards each request or connection to one healthy backend server. Layer 4 balancers route by IP and port without reading the request; Layer 7 balancers read HTTP and can route by path, header or cookie. Common algorithms are round robin, weighted round robin, least connections and hashing.

Full answer

What it does

A load balancer sits between clients and a pool of servers. It:

  1. Accepts the connection on a public address.
  2. Picks a backend using an algorithm.
  3. Forwards traffic and returns the response.
  4. Runs health checks (for example GET /healthz every 5 seconds) and stops sending traffic to servers that fail.

This gives horizontal scaling and also hides individual server failures from users.

Layer 4 versus Layer 7

Layer 4 (transport)Layer 7 (application)
SeesIP addresses, TCP/UDP portsFull HTTP request: path, headers, cookies
RoutingPer connectionPer request
TLSUsually passes it throughOften terminates it
CostVery cheap, high throughputMore CPU per request
FeaturesSimple forwardingPath routing (/api vs /static), header rules, retries, compression, rate limits

Use L4 for raw TCP services (databases, game servers, MQTT) or as a very fast first layer. Use L7 for web APIs when you need routing rules or want to spread HTTP/2 or gRPC requests that share one long-lived connection. With L4 those requests would all go to one server.

Algorithms

  • Round robin: server 1, 2, 3, 1, 2, 3. Good when servers and requests are similar.
  • Weighted round robin: a bigger server gets more turns.
  • Least connections: send to the server with the fewest active connections. Better when request times vary a lot, such as report generation mixed with simple reads.
  • Hash-based (client IP or a key): the same client keeps going to the same server. Useful for caches; consistent hashing limits reshuffling when servers change.
  • Power of two choices: pick two random servers, use the less loaded one. Nearly as good as least connections with less shared state.

Avoiding a new single point of failure

Load balancers run as a redundant pair or a managed cluster, often with DNS or anycast spreading traffic across several of them.

By level

Fresher: Explain what a load balancer does, what a health check is, and describe round robin and least connections. Mentioning that the load balancer itself needs a backup earns credit.

Mid: Compare L4 and L7 with real use cases, explain TLS termination, connection draining, and why long-lived connections need request-level balancing. Expect a clear reason why gRPC needs request-level balancing.

Senior: Discuss global load balancing (DNS, anycast), failover between regions, overload protection, outlier ejection, and how balancing interacts with autoscaling and slow-start for new instances. The answer should cover what happens to traffic when a whole zone fails.

Key points

  • Load balancers spread traffic and remove unhealthy servers using health checks
  • L4 routes connections by IP and port; L7 routes requests by HTTP content
  • L7 is needed to balance HTTP/2 and gRPC requests well
  • Least connections suits uneven request cost; hashing gives affinity
  • The load balancer itself must be redundant

Common mistakes

  • Forgetting health checks
  • Saying L4 can route by URL path
  • Using sticky sessions as a default instead of making servers stateless
  • Leaving the load balancer as a single point of failure

Follow-ups

  • Why can gRPC traffic end up unbalanced behind an L4 load balancer?
  • How would you drain a server before deploying to it?
  • When are sticky sessions acceptable?

Tips

  • Start with the request path from client to server in four steps
  • Draw the L4 versus L7 difference as "what it can read"
  • Match each algorithm to a workload

Evaluation rubric

  • Explains role and health checks
  • Correct L4 versus L7 distinction
  • Describes at least three algorithms with fitting use cases
  • Addresses redundancy of the balancer itself