Layer 4 vs Layer 7 Load Balancing
Load balancers are often described by the network layer they work at. A Layer 4 load balancer moves connections and never reads what is inside them. A Layer 7 load balancer opens each HTTP request and can route it by URL, header or cookie.
The difference affects speed, features, security and cost, so it comes up in almost every system design discussion. This tutorial compares both with diagrams and real configurations. It assumes you know the basics from Load Balancing and Load Balancing Algorithms.
1. What Are Layer 4 and Layer 7 Load Balancing?
The names come from the OSI model. Layer 4 is the transport layer (TCP and UDP). A Layer 4 load balancer chooses a backend using the source and destination IP addresses and ports, then forwards the packets or proxies the TCP stream unchanged.
Layer 7 is the application layer (HTTP, HTTPS, gRPC). A Layer 7 load balancer terminates the client connection, parses each request and decides where to send it based on its content, for example the path /api/orders or a Host header.
2. Architecture: L4 and L7 Side by Side
Both sit between clients and servers, but they see different things. The Layer 4 balancer sees only addresses and ports, so all requests on one connection go to the same server. The Layer 7 balancer sees every request and can split one website across many services.
3. How Each Type Works
Layer 4 flow
- The client opens a TCP connection to the balancer's IP on port 443.
- The balancer picks a server using the algorithm, for example a hash of the client IP and port.
- It forwards the connection. With TLS passthrough the encrypted bytes reach the server untouched, and the server decrypts them.
- Every request on that connection follows the same path until the connection closes.
Layer 7 flow
- The client opens a connection, and the balancer completes the TLS handshake using the site certificate.
- The balancer reads the request line and headers, such as
GET /api/orderswithHost: shop.example.com. - Routing rules match
/api/*to the order service pool, and the algorithm picks one instance. - The balancer sends the request over its own connection to that instance, often adding
X-Forwarded-ForandX-Forwarded-Protoheaders, and returns the response.
So a Layer 7 balancer actually runs two connections, client-to-balancer and balancer-to-server. That is what lets it inspect, change and route individual requests.
4. Key Concepts
TLS termination vs passthrough
Layer 7 balancers usually terminate TLS, which centralises certificates and enables content routing. Layer 4 balancers can pass TLS through, which keeps traffic encrypted end to end but hides the request from the balancer.
Connection vs request balancing
Layer 4 balances connections. With long-lived HTTP/2 or gRPC connections, many requests can pile onto one server. Layer 7 balances each request, which spreads multiplexed traffic evenly.
Protocol support
Layer 4 works for any TCP or UDP protocol: databases, MQTT, DNS, game servers. Layer 7 understands specific protocols, usually HTTP/1.1, HTTP/2, gRPC and WebSockets.
Client IP visibility
A proxying Layer 4 balancer hides the client IP unless you enable the PROXY protocol. A Layer 7 balancer adds it to the X-Forwarded-For or standard Forwarded header.
5. Practical Implementation
Nginx: Layer 4 with the stream module
The stream block proxies raw TCP. Here it balances PostgreSQL connections, which an HTTP balancer cannot understand.
stream {
upstream postgres_replicas {
least_conn;
server 10.0.3.11:5432;
server 10.0.3.12:5432;
}
server {
listen 5432;
proxy_pass postgres_replicas;
proxy_connect_timeout 3s;
}
}
Nginx: Layer 7 with path-based routing
http {
upstream api_service { server 10.0.1.21:8080; server 10.0.1.22:8080; }
upstream image_service { server 10.0.1.31:8080; }
server {
listen 443 ssl;
server_name shop.example.com;
ssl_certificate /etc/nginx/certs/shop.crt;
ssl_certificate_key /etc/nginx/certs/shop.key;
location /api/ {
proxy_pass http://api_service;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /images/ {
proxy_pass http://image_service;
}
}
}
Kubernetes: Service (L4) and Ingress (L7)
A Service of type LoadBalancer balances TCP or UDP connections to pods. An Ingress handled by an ingress controller routes HTTP requests by host and path.
apiVersion: v1
kind: Service
metadata:
name: orders-tcp
spec:
type: LoadBalancer # Layer 4: TCP to the pods
selector:
app: orders
ports:
- port: 443
targetPort: 8443
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
spec:
ingressClassName: nginx # Layer 7: HTTP routing
rules:
- host: shop.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: orders
port:
number: 8080
- path: /images
pathType: Prefix
backend:
service:
name: images
port:
number: 8080
6. Advantages and Disadvantages
Layer 4
- Advantages: very fast, low latency, works with any TCP/UDP protocol, can keep TLS end to end.
- Disadvantages: no content-based routing, no request-level retries, uneven spread with long-lived connections.
Layer 7
- Advantages: routing by path, host or header, TLS termination, caching, compression, retries, request logging and security filters.
- Disadvantages: more CPU per request, adds latency, must hold certificates, supports only the protocols it understands.
7. Layer 4 vs Layer 7 Comparison
| Aspect | Layer 4 | Layer 7 |
|---|---|---|
| Works on | IP addresses and ports (TCP/UDP) | HTTP requests: paths, headers, cookies |
| Reads request content | No | Yes |
| Routing options | By address, port or hash | By host, path, header, cookie, method |
| TLS | Usually passthrough | Usually terminated at the balancer |
| Balances | Connections | Individual requests |
| Performance | Higher throughput, lower latency | More processing per request |
| Typical products | AWS NLB, Nginx stream, HAProxy (mode tcp), Kubernetes Service | AWS ALB, Nginx http, HAProxy (mode http), Kubernetes Ingress |
8. Real-World Use Cases
- Layer 4: database read replicas, message brokers, MQTT for IoT devices, game servers, and very high-throughput edges that only spread TCP connections.
- Layer 7: websites and APIs that split paths across microservices, A/B testing by header or cookie, and blocking malicious requests with a web application firewall.
- Both together: large platforms often place a Layer 4 tier at the edge for raw capacity and a Layer 7 tier behind it for routing.
9. Best Practices
- Default to Layer 7 for HTTP APIs unless measurements show the balancer is a bottleneck.
- Use Layer 4 for non-HTTP protocols and where TLS must not be decrypted in the middle.
- Preserve the client IP: enable PROXY protocol on Layer 4, and only trust
X-Forwarded-Forfrom your own proxies on Layer 7. - Re-encrypt sensitive traffic from a Layer 7 balancer to the servers when compliance requires encryption in transit.
- Set timeouts and limits such as connect, read and idle timeouts on both layers.
- Watch long-lived connections: for gRPC behind Layer 4, balance per request at Layer 7 or on the client side.
10. Summary
- Layer 4 balancers route connections by IP and port without reading them.
- Layer 7 balancers terminate HTTP and route each request by its content.
- Layer 4 is faster and protocol-agnostic; Layer 7 is smarter and HTTP-aware.
- Nginx uses the
streamblock for Layer 4 and thehttpblock for Layer 7. - In Kubernetes, a
Serviceworks at Layer 4 and anIngressworks at Layer 7.
Layer 7 balancers are a kind of reverse proxy. Continue with Reverse Proxy.
Layer 4 vs Layer 7 Load Balancing: Top Interview Questions
These are the Layer 4 and Layer 7 load balancing questions most often asked in system design interviews, with short answers you can explain in your own words.
Q1. What is the main difference between Layer 4 and Layer 7 load balancing?
A Layer 4 balancer routes TCP or UDP connections using only IP addresses and ports. A Layer 7 balancer reads each HTTP request and routes it by content such as the path, host, headers or cookies.
Q2. Which is faster, and why?
Layer 4 is usually faster because it does not decrypt or parse requests; it only forwards connections. Layer 7 does more work per request, but modern Layer 7 proxies are fast enough for most applications.
Q3. Why can Layer 4 balancing be uneven for gRPC?
gRPC uses long-lived HTTP/2 connections that carry many requests. A Layer 4 balancer only balances the connection, so all requests on it hit one server. Layer 7 or client-side balancing spreads the individual requests.
Q4. How does the server learn the real client IP?
Behind a Layer 7 proxy, the client IP is added to X-Forwarded-For or the standard Forwarded header. Behind a proxying Layer 4 balancer, enable the PROXY protocol. Servers should trust these values only from their own proxies.
Q5. Can you use Layer 4 and Layer 7 together?
Yes. A common design puts a Layer 4 balancer at the edge to spread TCP connections across several Layer 7 proxies, which then route individual HTTP requests to services.