00:00

Layer 4 vs Layer 7 Load Balancing

Last updated:

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

Layer 4 and Layer 7 load balancing compared Top lane: a Layer 4 load balancer receives a TCP connection and forwards it to a server using only the IP address and port, without reading the HTTP request. Bottom lane: a Layer 7 load balancer terminates the connection, reads the HTTP path, and sends requests for /api to the API service and requests for /images to the image service. LAYER 4 · TRANSPORT ClientTCP :443 L4 Load Balancersees IP + port only Server 1gets the raw stream LAYER 7 · APPLICATION ClientGET /api/orders L7 Load Balancerreads path, headers,cookies API service/api/* Image service/images/*
Layer 4 forwards connections by IP and port. Layer 7 reads each HTTP request and routes by its content.

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

  1. The client opens a TCP connection to the balancer's IP on port 443.
  2. The balancer picks a server using the algorithm, for example a hash of the client IP and port.
  3. It forwards the connection. With TLS passthrough the encrypted bytes reach the server untouched, and the server decrypts them.
  4. Every request on that connection follows the same path until the connection closes.

Layer 7 flow

  1. The client opens a connection, and the balancer completes the TLS handshake using the site certificate.
  2. The balancer reads the request line and headers, such as GET /api/orders with Host: shop.example.com.
  3. Routing rules match /api/* to the order service pool, and the algorithm picks one instance.
  4. The balancer sends the request over its own connection to that instance, often adding X-Forwarded-For and X-Forwarded-Proto headers, 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.

nginx.conf (Layer 4)
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

nginx.conf (Layer 7)
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.

k8s-load-balancing.yaml
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

AspectLayer 4Layer 7
Works onIP addresses and ports (TCP/UDP)HTTP requests: paths, headers, cookies
Reads request contentNoYes
Routing optionsBy address, port or hashBy host, path, header, cookie, method
TLSUsually passthroughUsually terminated at the balancer
BalancesConnectionsIndividual requests
PerformanceHigher throughput, lower latencyMore processing per request
Typical productsAWS NLB, Nginx stream, HAProxy (mode tcp), Kubernetes ServiceAWS 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-For from 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 stream block for Layer 4 and the http block for Layer 7.
  • In Kubernetes, a Service works at Layer 4 and an Ingress works at Layer 7.

Layer 7 balancers are a kind of reverse proxy. Continue with Reverse Proxy.