Video in progress

What is the difference between an L4 and an L7 load balancer?

An L4 balancer works at the TCP/UDP level: it sees only the IP and port and doesn't look inside the packet. An L7 balancer understands HTTP: it routes by URL, header or cookie and terminates TLS, but uses more resources.

📖 Answer

For L4, a connection is just a stream of bytes. It pins the connection to one backend and leaves it there until the end. It's very fast and protocol-independent — it can balance PostgreSQL, Kafka and gRPC traffic too. Examples: AWS NLB, HAProxy's mode tcp.

L7 reads the whole request and decides based on it:

location /api/payments/ { proxy_pass http://payments; }
location /api/          { proxy_pass http://core; }
location /static/       { root /var/www; }

What L7 can do:

  • send requests to different services by path or host;
  • terminate TLS at the balancer, managing certificates in one place;
  • add headers (X-Request-Id, X-Forwarded-For), gzip, rate limiting;
  • balance every HTTP request separately — requests within one keep-alive connection can go to different backends.

A subtle point: with HTTP/2 or gRPC, thousands of requests flow through a single long connection. An L4 balancer pins that connection to one pod, and the load is distributed unevenly. That's why gRPC needs an L7 balancer or client-side balancing.

In practice you often have both: L4 on the outside (fast, simple), and behind it an L7 like Nginx or Envoy.