L4 vs L7 Load Balancing: What's the Difference?
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.
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.comandblog.example.comcan share one IP address yet reach different pools, because theHostheader names the site. - Header or cookie: a cookie such as
beta=1sends 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.
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.
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
- NGINX ingress controller – Deep dive with AWS EKS: builds the "both" pattern: an AWS Network Load Balancer in front of NGINX on Kubernetes.