Load balancers can operate at different layers of the network stack. The two important types for system design are Layer 4 and Layer 7 load balancing.
1. Layer 4 Load Balancing
Layer 4 operates at the Transport Layer. It primarily uses information such as:
- Source IP
- Destination IP
- Source port
- Destination port
- TCP / UDP
flowchart LR
Client["Client"]
L4["Layer 4 Load Balancer"]
Server1["Server 1"]
Server2["Server 2"]
Client --> L4
L4 --> Server1
L4 --> Server2
Layer 4 does not need to understand the application-level details of an HTTP request.
2. Layer 7 Load Balancing
Layer 7 operates at the Application Layer. It can understand application-level information such as:
- HTTP method
- URL path
- Hostname
- HTTP headers
- Cookies
flowchart TD
Client["Client"]
L7["Layer 7 Load Balancer"]
Order["Order Service"]
Payment["Payment Service"]
User["User Service"]
Client --> L7
L7 -->|/orders| Order
L7 -->|/payments| Payment
L7 -->|/users| User
3. Example
Suppose the client sends:
GET /orders/1001
A Layer 7 load balancer can inspect the URL path and route the request specifically to the Order Service.
flowchart LR
Client["Client"]
L7["Layer 7 Load Balancer"]
Order["Order Service"]
Payment["Payment Service"]
Client --> L7
L7 -->|/orders| Order
L7 -->|/payments| Payment
A Layer 4 load balancer does not make routing decisions based on the HTTP URL path.
4. Layer 4 vs Layer 7
| Layer 4 | Layer 7 |
|---|---|
| Transport Layer | Application Layer |
| TCP / UDP | HTTP / HTTPS |
| IP and port based | Application-aware |
| Does not normally inspect HTTP content | Can inspect HTTP requests |
| Generally lower-level | Generally higher-level |
| Useful for generic TCP/UDP traffic | Useful for HTTP-aware routing |
5. Layer 4 Example
Suppose an application is listening on port 443.
flowchart LR
Client["Client"]
L4["Layer 4 Load Balancer :443"]
Server1["Server 1 :443"]
Server2["Server 2 :443"]
Client --> L4
L4 --> Server1
L4 --> Server2
The load balancer can distribute connections without needing to understand the HTTP URL.
6. Layer 7 Example
flowchart TD
Client["Client"]
L7["Layer 7 Load Balancer"]
Client --> L7
L7 -->|/orders/*| Order["Order Service"]
L7 -->|/payments/*| Payment["Payment Service"]
L7 -->|/customers/*| Customer["Customer Service"]
The load balancer can make routing decisions based on the HTTP request.
7. Layer 7 Host-Based Routing
Layer 7 load balancing can also route requests based on the hostname.
flowchart TD
Client["Client"]
L7["Layer 7 Load Balancer"]
Client --> L7
L7 -->|api.example.com| API["API Service"]
L7 -->|admin.example.com| Admin["Admin Service"]
L7 -->|shop.example.com| Shop["Shop Service"]
8. Layer 7 and HTTPS
To make application-level routing decisions for encrypted HTTPS traffic, the load balancer may terminate TLS and inspect the decrypted HTTP request.
flowchart LR
Client["Client"]
LB["Layer 7 Load Balancer"]
API["API Service"]
Client -->|HTTPS| LB
LB -->|HTTP / HTTPS| API
The exact TLS architecture depends on whether encryption is terminated at the load balancer or passed through to the backend.
9. Layer 4 vs Layer 7 in Microservices
flowchart TD
Client["Client"]
LB["Layer 7 Load Balancer"]
Client --> LB
LB --> Order["Order Service"]
LB --> Payment["Payment Service"]
LB --> User["User Service"]
Layer 7 routing is useful when different services need to be exposed through a common HTTP endpoint.
10. Important Interview Question
When would you choose Layer 4 vs Layer 7?
Choose Layer 4 when you primarily need transport-level connection distribution and do not need application-aware routing.
Choose Layer 7 when you need HTTP-aware features such as path-based routing, host-based routing, or header-based routing.
11. Important Interview Questions
- What is Layer 4 load balancing?
- What is Layer 7 load balancing?
- What is the difference between Layer 4 and Layer 7?
- Can Layer 7 route based on URL path?
- Can Layer 7 route based on hostname?
- How does HTTPS affect Layer 7 routing?
- When would you choose Layer 4?
- When would you choose Layer 7?
- What is the difference between TCP-level and HTTP-level load balancing?
Key Takeaways
- Layer 4 operates at the transport level.
- Layer 4 uses information such as IP addresses and ports.
- Layer 7 operates at the application level.
- Layer 7 can understand HTTP requests.
- Layer 7 can perform path-based and host-based routing.
- HTTPS may require TLS termination for HTTP-aware inspection.
Conclusion
Layer 4 → IP + Port + TCP/UDP
Layer 7 → HTTP + URL + Host + Headers
The key interview distinction is simple: Layer 4 distributes network connections, while Layer 7 can make routing decisions using application-level information.