Course map

Topic 093 / Core

Layer 4 and Layer 7 load balancing

Chapter 6.3 · Scaling Services

What and why

Load balancing (→ 92) can operate at different layers of the network stack (→ 23); the layer chosen determines what the balancer can see and do. Layer 4 (L4) balances by IP and port alone — it forwards packets without understanding what's inside them. Layer 7 (L7) balances by the application-level request — path, headers, cookies — because it terminates and reads the protocol (typically HTTP, → 28) itself. Builds on: → 92 (load balancing and its algorithms).

Diagram

L4 L7
Sees source/destination IP and port the full request: path, headers, cookies, method
Decision example "this connection" → this instance "/api/v2/*" → this service; "a header" → the canary instance
Cost low — no protocol parsing higher — must terminate TLS (→ 27) and parse the request
Enables raw throughput, protocol-agnostic balancing canary routing, path-based routing, TLS termination at the edge

How it works

  1. L4 balancing sees a connection's IP/port and forwards packets to a chosen instance for the life of that connection, without ever parsing what's inside — it works for any protocol, not only HTTP.
  2. L7 balancing terminates the connection itself (including TLS, → 27), reads the actual request, and decides based on its content — path, header, or cookie — then opens its own connection to the chosen instance.
  3. Because L4 does no parsing, it's cheap and fast but blind: it cannot route /v1 and /v2 differently, or send a percentage of traffic to a canary (→ 187) by cookie.
  4. Because L7 parses every request, it costs more CPU per request but enables exactly that: path-based routing, header-based routing, and canary or A/B splits.
  5. Real deployments often layer both: an L4 balancer in front for raw throughput, with L7 balancing behind it for the application-aware decisions.

Practical example

(numbers illustrative). A deployment wants to send 5% of traffic to a canary version (→ 187) based on a request header. An L4 balancer cannot do this — it only sees IP and port, not headers — so a header-based split is not expressible at that layer. An L7 balancer reads the header on every request and can route exactly 5 of every 100 matching requests to the canary, with 95 going to the stable version — the split only exists because L7 reads inside the request.

When to use it

  • Use L4 when the protocol isn't HTTP, when per-request overhead must be minimal, or when the decision genuinely doesn't need to see inside the request.
  • Use L7 when routing depends on path, header, or cookie — canary deployments, multi-tenant routing by subdomain, A/B testing.
  • Avoid paying L7's per-request parsing cost when an L4 decision would do; avoid asking L4 to make a decision it structurally cannot see.
  • Failure scenarios: TLS terminated at an L7 balancer that then forwards unencrypted traffic over a network wrongly assumed to be trusted; an L4 balancer's long-lived connection pinning a client to one instance for the connection's whole life even after that instance becomes unhealthy mid-connection.
  • Simpler alternative: for a single-service, single-protocol deployment with no routing rules beyond "spread load evenly," L4 alone is enough — add L7 only when a specific routing decision requires reading the request.

Practice

(vocabulary/classification drill, 15 min, paper).

  1. Classify each as L4 or L7, and why: "route database replication traffic by port," "send /checkout to a dedicated fleet," "balance raw UDP game traffic," "route 10% of requests with a given header to a beta service."
  2. For a hypothetical e-commerce site, name one routing decision that requires L7 and explain what information L4 is missing to make it.

Done when every classification above matches the "can it see inside the request" test correctly.

Key takeaway

L4 balances blind and cheap by IP/port; L7 balances informed and costlier by reading the actual request — the choice is whether the decision needs to see inside the request. Done when you can: classify any routing requirement as L4 or L7 and explain why by what information is available at each layer. Used next in → 94 (global traffic routing, a layer above both), → 187 (canary deployments, L7's typical justification).

References

None — this topic makes no external claims.

Source directory

Search the guide