Full answer
What it does
A load balancer sits between clients and a pool of servers. It:
- Accepts the connection on a public address.
- Picks a backend using an algorithm.
- Forwards traffic and returns the response.
- Runs health checks (for example
GET /healthzevery 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) | |
|---|---|---|
| Sees | IP addresses, TCP/UDP ports | Full HTTP request: path, headers, cookies |
| Routing | Per connection | Per request |
| TLS | Usually passes it through | Often terminates it |
| Cost | Very cheap, high throughput | More CPU per request |
| Features | Simple forwarding | Path 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