Course contents · Lesson 15 of 22

L4 vs L7 Load Balancing: What's the Difference?

Networks for Engineers · Lesson 15 of 22 · 11 min Engineer

You go to create a load balancer on AWS and must first pick a type, for example an Application Load Balancer or a Network Load Balancer. A colleague says the website needs the first and the database the second. This lesson explains L4 vs L7 load balancing: what each kind sees and does, what it costs, and when to use which.

Two layers: the envelope and the letter

Networking is described in layers, each wrapped inside the one below, numbered by the OSI model, a seven-layer reference model. Layer 3 is IP and its addresses (lesson 2). Layer 4, transport, is TCP or UDP with their ports (lesson 5). Layer 7, application, is what programs say, such as an HTTP request with its method, path and headers (lesson 6).

"L4" and "L7" name the deepest layer a load balancer reads. An L4 balancer reads IP addresses and ports, strictly layers 3 and 4, and treats the rest as bytes. An L7 balancer reads the request itself. With HTTPS, TLS encrypts that request inside the TCP connection (lesson 8), so an L7 balancer must decrypt it first.

In the diagram, notice that both see the same addresses and port, but only L7 sees GET /api/users and the Host header. The site name on the left is lesson 8's exception: the browser usually sends it unencrypted in its client hello.

Two panels showing one HTTPS request to shop.example.com, with illustrative values. Left, the L4 balancer sees the envelope: from 198.51.100.23 port 52114, to 203.0.113.7 port 443, protocol TCP, and usually the site name shop.example.com from the TLS client hello; after that, only encrypted bytes it cannot read. Right, the L7 balancer opens the letter: the same addresses and port, plus, after decrypting with the site's certificate and key, the request line GET /api/users HTTP/1.1, marked path, the header Host: shop.example.com, marked host, a session cookie and the User-Agent. A note says both see the addresses, but only L7 sees what is asked, so only L7 can route by path, headers or cookies
Both see the envelope; only the L7 balancer reads what is inside.

L4: forwarding connections by address and port

An L4 load balancer picks a backend when a connection opens, from the packet headers alone: the protocol plus lesson 2's four values, both IP addresses and both ports. Often it hashes them into a number that selects a server, or uses an algorithm from lesson 14. Every later packet of that connection goes to the same backend.

Some L4 balancers forward the client's own packets, rewriting just the destination address, a cousin of NAT (lesson 12), so the server still sees the client's real IP address. Others relay the bytes over a second connection of their own, so the server sees the balancer's. Neither parses the data.

So an L4 balancer works for any protocol on TCP or UDP: databases such as PostgreSQL on port 5432, SSH, mail or game servers. UDP has no connections, so it treats datagrams with the same addresses and ports as one flow and keeps it on one server. Doing little work per packet makes it fast.

The price: it decides once per connection, and HTTP/2 runs many requests over one connection (lesson 6), so all of them reach the same server, whatever they ask for. Some L4 balancers can read the site name from the client hello and pick a pool by it, still without decrypting.

L7: routing by what the request says

An L7 load balancer is a proxy, a go-between that ends the client's connection and opens its own. It terminates TLS, completing the handshake with the site's certificate (lesson 14), reads each HTTP request, and sends it to a backend. So it can pick a different server for each request, even on one connection.

A routing rule matches part of the request and sends it to a pool, a set of servers for one job (on AWS, a target group):

  • Path: /api/ to the API servers, /static/ to servers holding images and other files.
  • Host name: shop.example.com and blog.example.com can share one IP address yet reach different pools, because the Host header names the site.
  • Header or cookie: a cookie such as beta=1 sends testers to a new version.

Reading the request also lets it change things: rewrite headers, for example to add the client's IP address (lesson 16 covers how), redirect HTTP to HTTPS, and sometimes cache responses (lesson 17). Many also run a WAF (web application firewall), which blocks requests matching attack patterns, such as database commands hidden in a form field; lesson 13's firewalls never see the request.

In the flowchart, notice where each lane decides: L4 picks from step 1's headers and never looks again, while L7 must decrypt and read before step 2's question exists, and asks it for every request.

Flowchart. A new connection arrives at 203.0.113.7 port 443 and splits into two lanes. The L4 lane reads the envelope: 1, read addresses and ports, such as 198.51.100.23:52114 to port 443; 2, pick a server by a hash or round robin; 3, pass every byte to it, still encrypted and never opened. A note says this is decided once, before any data, so all the connection's requests go to that server. The L7 lane reads the letter: 1, decrypt TLS and read the request, GET /api/users with the Host header; 2, which path? A path starting /api/ goes to the API pool, /static/ to the static pool, and anything else to the web pool; 3, pick a server in that pool, again for every request
L4 chooses before reading anything; L7 reads first, then chooses per request.

What reading the letter costs

Decrypting TLS, parsing HTTP and matching rules for every request takes CPU and memory an L4 balancer never spends. For the same traffic, an L7 balancer needs more capacity and adds a little delay to each request.

It also holds the site's private key, and inside it your traffic is plain text, so it needs the same protection as your servers.

Above all, an L7 balancer understands only the protocols it was built for. An AWS Application Load Balancer accepts only HTTP and HTTPS; a PostgreSQL or SSH connection carries no HTTP request for it to read. Some proxies, such as Envoy, understand a few more, but each must be built in.

Choosing: L4, L7 or both

Lean towards L4 for databases, message queues, SSH and anything on UDP, or when servers must see each client's real address, or when TLS must stay unbroken from client to server. On AWS, that is the Network Load Balancer.

Lean towards L7 for websites and APIs: routing by path or host, many sites on one address, certificates in one place, a WAF, and a fresh choice of server per request. On AWS, that is the Application Load Balancer.

Many setups use both: an L4 balancer spreads connections across a few L7 proxies, which route each request. For example, an AWS Network Load Balancer can front an NGINX ingress controller, the L7 proxy into Kubernetes, the container platform. One tool can do both: this HAProxy file, like lesson 14's, has an L4 frontend for two read-only database copies, or replicas, and an L7 frontend for the shop; addresses and path are illustrative.

frontend db_in
    mode tcp
    bind :5432
    default_backend db_replicas

backend db_replicas
    mode tcp
    balance leastconn
    server db-1 10.0.2.21:5432 check
    server db-2 10.0.2.22:5432 check

frontend web_in
    mode http
    bind :443 ssl crt /etc/haproxy/certs/shop.pem
    use_backend api if { path_beg /api/ }
    default_backend web

mode tcp forwards bytes unparsed, so db_replicas balances whole connections, and leastconn suits long-lived ones. mode http parses each request: ssl crt terminates TLS with a file holding the certificate and key, and path_beg /api/ sends paths starting /api/ to the api backend. That backend and web are left out.

The table sums up; as its note says, the layer describes how a balancer handles traffic, not a brand.

Comparison table of L4, transport, and L7, application. Sees: L4 addresses and ports, then only encrypted bytes; L7 the whole request, path, host, headers and cookies. Speed: L4 very fast, little CPU per connection; L7 fast but more CPU per request. Decides: L4 once per connection; L7 for every request. Routing rules: L4 address and port, some also the TLS site name; L7 path, host, header and cookie, plus method and query string. TLS: L4 usually passes it through, some can decrypt it; L7 usually terminates it and holds the certificate and key. Protocols: L4 any TCP or UDP protocol; L7 only those it understands, such as HTTP and gRPC. Examples: L4 AWS Network Load Balancer and HAProxy in tcp mode; L7 AWS Application Load Balancer, NGINX and Envoy. A note says many tools do both: HAProxy and NGINX each have an L4 and an L7 mode
L4 trades visibility for speed and reach; L7 trades CPU for control.

Check yourself

Behind an L4 balancer, a browser loads 40 images over HTTP/2 and every request reaches the same server. Is the balancer broken?

No. HTTP/2 sends them all over one connection, and an L4 balancer chooses once per connection. An L7 balancer, which reads each request, could spread them.

Your balancer passes TLS through untouched, and you want /api/ requests on a separate pool. Why can't it do that?

The path is inside the encrypted bytes, so it never sees it. Terminate TLS at an L7 balancer, or add L7 proxies behind it, and route there.

Your team wants to send PostgreSQL connections through the website's Application Load Balancer. Why won't that work?

It understands only HTTP and HTTPS, and PostgreSQL speaks its own protocol, so there is no request to read. An L4 balancer forwards any TCP connection.

Go deeper