Load Balancing
Overview
A load balancer is a component that distributes incoming requests across a pool of backend servers so that no single server is overwhelmed. It also improves availability by routing traffic only to healthy servers, and it is a prerequisite for scaling a stateless service horizontally.
Key Concepts
A load balancer exposes a single virtual endpoint to clients and forwards each request to one backend from its pool. It continuously runs health checks and removes failing servers from rotation, which is what turns a set of interchangeable servers into a fault-tolerant tier. The choice of which backend receives a request is made by a load balancing algorithm.
Layer 4 load balancing
A Layer 4 load balancer operates at the transport layer and routes by IP address and TCP/UDP port without inspecting the payload. Because it forwards packets or connections without parsing application data, it is fast and protocol-agnostic, but it cannot make decisions based on the content of a request such as its URL path.
Layer 7 load balancing
A Layer 7 load balancer operates at the application layer and can read HTTP details such as the path, headers, and cookies. This enables content-based routing, TLS termination, sticky sessions, and request rewriting, at the cost of more processing per request because it parses the full application message.
Difference between Layer 4 and Layer 7
| Aspect | Layer 4 | Layer 7 |
|---|---|---|
| OSI layer | Transport (TCP/UDP) | Application (HTTP/HTTPS) |
| Routes by | IP address and port | URL path, headers, cookies |
| Payload inspection | No | Yes |
| Throughput | Higher, less overhead | Lower, more processing |
| Capabilities | Fast connection forwarding | Content routing, TLS termination, sticky sessions |
| Typical use | Raw TCP/UDP or max throughput | HTTP APIs & websites needing smart routing |
A Layer 7 load balancer is a specialized reverse proxy: both sit in front of servers and forward client requests, but a load balancer's defining job is distributing load across a pool.
Trade-offs
The trade-off is speed versus intelligence. Layer 4 forwards with minimal overhead and works for any protocol, making it ideal when raw throughput matters or when the traffic is not HTTP. Layer 7 costs more per request but unlocks routing decisions that a modern web system depends on, such as sending /api and /images to different backend pools or terminating TLS centrally. Many architectures combine both: an L4 balancer at the edge for volume, fronting L7 balancers for smart routing.
Interview Tips
- State whether you need Layer 4 or Layer 7 and back it with a concrete need, such as path-based routing or TLS termination for Layer 7.
- Mention health checks explicitly, because removing unhealthy servers is central to availability.
- Avoid making the load balancer a single point of failure: note that it is typically deployed as a redundant pair or a managed, replicated service.
Summary
- A load balancer spreads requests across a pool of servers and routes around failures.
- Layer 4 routes by IP and port without inspecting the payload, favoring throughput.
- Layer 7 routes on HTTP content, enabling smart routing and TLS termination.
- The backend for each request is picked by a load balancing algorithm.
- Deploy the load balancer redundantly so it is not itself a single point of failure.