1. Library
  2. Servers and Infrastructure
  3. Load Balancing

Updated 10 hours ago

Every load balancer faces the same question: what information can I use to make this routing decision?

The answer depends on how deep it's willing to look.

The Core Distinction

Layer 4 sees envelopes. Layer 7 reads letters.

A Layer 4 load balancer examines the outside of each network packet—source IP, destination IP, source port, destination port. That's it. It never opens the envelope to see what's inside. It makes decisions based purely on addressing information.

A Layer 7 load balancer tears open the envelope, reads the letter, and routes based on what it says. For HTTP traffic, this means inspecting URLs, headers, cookies, sometimes even the request body. It understands the conversation, not just the addressing.

This distinction drives everything else.

Layer 4: The Fast Packet Sorter

Imagine a postal sorting facility that routes packages based solely on ZIP codes. Workers never open anything—they just read the label and toss the package down the appropriate chute. This is Layer 4 load balancing.

When a TCP connection arrives, the Layer 4 load balancer sees:

  • Source IP address (who's sending)
  • Destination IP address (where they think they're sending)
  • Source port (sender's return address)
  • Destination port (which service they want—80 for HTTP, 443 for HTTPS, 5432 for PostgreSQL)

Based on this and a simple algorithm (round-robin, least connections, IP hash), it picks a backend server and forwards packets. The mechanism is usually NAT—the load balancer rewrites the destination IP from its own address to the chosen backend's address.

The packets flow through without the load balancer understanding what's inside them. HTTP requests, database queries, video streams, encrypted traffic—all look the same. Just envelopes to be sorted.

Speed is the prize. Without parsing application protocols, Layer 4 load balancers achieve remarkable throughput. Millions of connections per second. Microsecond latencies. They're limited by network bandwidth, not processing power.

Simplicity is the bonus. Layer 4 works for any TCP or UDP traffic. Database connections, game servers, VoIP, custom protocols—anything that speaks TCP or UDP can be load balanced without the load balancer understanding the protocol.

Blindness is the cost. Layer 4 can't route /api/* to one server pool and /images/* to another—it can't see URL paths. It can't stick sessions based on cookies—it can't see cookies. It can't inspect encrypted HTTPS traffic—it just sees encrypted bytes. The envelope is opaque.

Layer 7: The Letter Reader

Now imagine a mail room where workers open every letter, read the contents, and route based on what they find. Invoices go to accounting. Technical questions go to engineering. VIP customers get special handling. This is Layer 7 load balancing.

A Layer 7 load balancer terminates the client's TCP connection, waits for the full HTTP request to arrive, parses it, makes a routing decision based on the content, and opens a new connection to the chosen backend.

It sees everything:

  • The full URL path (/api/users/123)
  • All HTTP headers (Host, Cookie, User-Agent, Authorization)
  • Query parameters (?sort=date&limit=10)
  • Sometimes the request body itself

This visibility enables sophisticated routing:

Content-based routing: Send /api/* requests to API servers optimized for JSON processing. Send /images/* to servers with SSDs and image optimization. Send /admin/* to servers behind additional security.

User-based routing: Inspect the User-Agent header and send mobile users to servers optimized for mobile responses. Check authentication cookies and route premium users to faster infrastructure.

SSL/TLS termination: Decrypt HTTPS traffic at the load balancer, inspect the now-visible HTTP request, make routing decisions, then re-encrypt (or not) before forwarding to backends. This offloads cryptographic work from application servers.

Header manipulation: Add an X-Forwarded-For header so backends know the original client IP. Strip sensitive headers before forwarding. Rewrite URLs for backend compatibility.

Smart health checks: Don't just verify that port 443 accepts connections—actually request /health and verify the response says {"status": "ok"}. A Layer 4 health check might think a crashed application is healthy because the port is still listening.

Session affinity via cookies: Set a cookie on the first response that identifies which backend server handled the request. On subsequent requests, read that cookie and route to the same backend. No client cooperation required.

The price is processing. Parsing HTTP, managing two TCP connections per request, potentially handling SSL/TLS encryption—all of this takes CPU cycles. Layer 7 load balancers handle tens or hundreds of thousands of requests per second, not millions. Still substantial for most applications, but an order of magnitude less than Layer 4.

When Layer 4 Wins

Non-HTTP protocols. If you're load balancing PostgreSQL connections, Redis clusters, game servers, or VPN tunnels, Layer 7 has nothing to offer. These protocols aren't HTTP, and Layer 7 load balancers don't understand them. Layer 4 treats all TCP traffic equally.

Extreme throughput requirements. When you genuinely need millions of connections per second with microsecond latencies, Layer 4 is the only option. Layer 7 processing overhead becomes the bottleneck.

Pass-through SSL. Some architectures require end-to-end encryption where the load balancer never sees plaintext. Layer 4 can forward encrypted packets without decrypting them. Layer 7 must decrypt to inspect.

Simple distribution needs. If round-robin across identical backend servers is all you need, Layer 4's simplicity is a feature. No configuration complexity, no parsing overhead, just fast packet distribution.

When Layer 7 Wins

Microservices architectures. When /users/* routes to the user service, /orders/* routes to the order service, and /payments/* routes to the payment service—all behind the same domain—you need Layer 7. Layer 4 can't see URL paths.

Multi-tenant applications. Route customer1.example.com to one backend pool and customer2.example.com to another based on the Host header. Layer 4 can't distinguish—both resolve to the same IP.

Canary deployments and A/B testing. Send 5% of traffic to the new version based on a cookie or header. Gradually increase the percentage. Roll back instantly if metrics degrade. Layer 7 makes this surgical. Layer 4 can only do IP-based splitting.

Security integration. Web Application Firewalls inspect HTTP requests for SQL injection, cross-site scripting, and other attacks. This happens at Layer 7 because it requires understanding HTTP semantics. Layer 4 sees only encrypted bytes.

SSL offloading. Terminate thousands of SSL connections at the load balancer, then use plain HTTP to backends on a private network. Your application servers never touch cryptography.

The Hybrid Reality

Production architectures often use both.

Layer 4 at the edge, Layer 7 behind it. A Layer 4 load balancer handles initial traffic distribution across multiple Layer 7 load balancers. The Layer 4 handles raw throughput; the Layer 7s handle intelligent routing. This scales horizontally—add more Layer 7 load balancers behind the Layer 4 as traffic grows.

Layer 4 for non-HTTP, Layer 7 for HTTP. Database connections flow through Layer 4 load balancers. Web traffic flows through Layer 7. Each protocol gets the appropriate tool.

Cloud provider offerings reflect this. AWS provides Network Load Balancer (Layer 4) and Application Load Balancer (Layer 7). Google Cloud has TCP/UDP Load Balancing and HTTP(S) Load Balancing. Azure has Azure Load Balancer and Application Gateway. The distinction exists because both are genuinely useful for different purposes.

The Cost Equation

Layer 4 costs less. Less processing power required means cheaper hardware, lower cloud bills, and simpler infrastructure. Cloud providers charge significantly less for Network Load Balancers than Application Load Balancers.

But cost isn't just about the load balancer. Layer 7's features can reduce overall infrastructure costs:

  • SSL termination offloads work from application servers, potentially requiring fewer of them
  • Content-based routing can direct traffic more efficiently, improving backend utilization
  • Better health checks catch problems faster, reducing wasted capacity on unhealthy servers

For most HTTP applications, Layer 7's additional cost is justified by its capabilities. For non-HTTP traffic or extreme scale, Layer 4's efficiency wins.

Frequently Asked Questions About Layer 4 vs. Layer 7 Load Balancing

Was this page helpful?

😔
🤨
😃