Skip to main content
← Back to Insights

Load Balancer - Under the Hood

· 11 min read
Jitender Sharma
Advisor & Technical Leader · Enterprise AI & Platforms

Split diagram of Layer 4 connection balancing versus Layer 7 HTTP request routing to backend pools

DNS gave you an IP. TLS may wrap the bytes. Something still has to decide which of N healthy machines answers this connection or this request.

Most engineers know a load balancer as "the box that spreads traffic across servers." That explanation is correct. It skips the OSI layer, what headers the balancer can read, and whether cloud "Network" vs "Application" load balancers are marketing or a real wire difference.

A load balancer sits in front of a backend pool. Clients talk to a virtual IP (or hostname that resolves to one). The balancer picks a healthy target, forwards traffic, and fails over when health checks fail. The hard part is which layer it balances on.

THE CLAIM

Layer 4 balances connections (and packets) using IP and port. Layer 7 balances requests using application data (HTTP host, path, headers, cookies). Cloud names map to that choice: Network Load Balancer ≈ L4; Application Load Balancer ≈ L7. Pick the layer for the decision you need, not the product brochure.

The bottom line first

  • Load balancer = VIP + pool + health + policy in front of backends; clients should not pick a single instance forever.
  • L4 (network / transport): sees TCP/UDP 4-tuple; fast, protocol-agnostic; cannot route on URL path.
  • L7 (application): terminates (or inspects) HTTP/gRPC; routes on host, path, headers; often terminates TLS.
  • Network LB (AWS NLB, Azure Load Balancer, GCP Network/Passthrough) ≈ L4.
  • Application LB (AWS ALB, Azure Application Gateway, GCP HTTP(S) LB) ≈ L7.
  • CDN and DNS steer where traffic enters a region or edge; the LB steers which instance inside that boundary. See DNS, CDN, Anycast vs Unicast.

What a load balancer actually is

Without a balancer, clients hardcode one server (or one DNS A record). That host dies, restarts, or saturates, and the product dies with it.

A load balancer separates service identity (the VIP / hostname clients use) from instance location (which backend has capacity right now).


Components involved

ComponentRole
VIP / listenerAddress and port clients connect to (443, 80, …)
Target / backend poolInstances, containers, IPs, or Lambda-style targets
Health checkProbe that marks a target in/out of rotation
Scheduling policyRound-robin, least connections, consistent hash, weighted, …
Security / TLSOptional terminate, passthrough, or re-encrypt to backends

DNS answers "which IP owns this name?" (often the LB or a CDN in front). The load balancer answers "which healthy backend gets this connection or request?" Transport details: TCP vs UDP Under the Hood.


Layer 4 load balancers (network / transport)

What it is

A Layer 4 load balancer works at the transport layer. It forwards TCP or UDP flows using network and transport headers: source IP, destination IP, source port, destination port (the 4-tuple), plus protocol. It does not need to understand HTTP, gRPC framing, or URL paths.

Modes you will hear:

ModeIdea
Pass-through / DSR-stylePackets steered with minimal rewrite; backend may see client IP
NAT / proxy L4Balancer owns the client connection and opens a second connection to the backend

Either way, the decision is still L4: connection or flow affinity, not "/api/v2 goes to pool B."

What it sees (and does not)

SeesDoes not see (without going L7)
IP, port, TCP/UDP, SYN/flow stateHTTP method, path, Host, cookies
Connection rate, bytes, RST/timeoutsJSON body, gRPC method name
TLS as opaque bytes (if not terminating)Certificate SAN routing as HTTP logic

When to use

  • Extreme throughput / low latency (millions of connections, thin proxy)
  • Non-HTTP protocols: databases, MQTT, custom TCP, raw UDP, gaming
  • TLS passthrough to backends that terminate TLS themselves
  • Static or simple port-based services (:5432, :6379, :443 as a pipe)

When not to use

  • You need path-based or host-based routing (/api vs /web)
  • You need cookie stickiness, WAF, or header-based canaries
  • You want the balancer to terminate TLS and inject identity headers

Pros and cons

ProsCons
Fast; low CPU per connectionNo HTTP-aware routing
Protocol-agnosticLimited observability of "requests"
Good for TCP/UDP and TLS passthroughSticky sessions usually mean source-IP hash, not cookies
Simple mental modelWrong tool for microservices URL meshes
L4 mental model (TCP flow to a backend)
Client L4 LB Backend
|---- TCP SYN ---------->| |
| |---- TCP SYN (chosen IP) ----->|
|<--- TCP stream --------|<--- TCP stream ---------------|
| (balanced as one connection / flow) |

Decision inputs: listener port, 4-tuple (or flow hash), health, algorithm. Not GET /checkout.

TAKEAWAY

L4 is a smart pipe distributor. It is the right default when the app protocol is not HTTP, or when you must not inspect application bytes.


Layer 7 load balancers (application)

What it is

A Layer 7 load balancer understands the application protocol, almost always HTTP/1.1, HTTP/2, or gRPC. It can terminate TLS, parse requests, and route each request (not only each TCP connection) to a different target group based on host, path, headers, methods, or cookies.

That is why cloud products call these Application Load Balancers: the unit of balance is often the request, after the balancer speaks HTTP.

Companion framing: HTTP vs HTTPS Under the Hood and HTTPS Encryption Lifecycle Under the Hood for cleartext vs TLS and who terminates where.

What it sees

SeesTypical actions
Host, path, query, headers, cookiesPath/host routing to different pools
HTTP method, status codesRetries, redirects, fixed responses
TLS (if terminating)Certs, SNI, optional mTLS
gRPC / HTTP/2 streamsPer-stream routing and health

When to use

  • Microservices and APIs with path or host-based routing
  • Browser and mobile HTTP(S) front doors
  • TLS termination at the edge of the VPC / cluster
  • Canary or blue/green by header or weighted target groups
  • WAF, auth redirects, and request-level observability

When not to use

  • Raw TCP/UDP or database protocols (use L4)
  • You need absolute minimum latency and no HTTP parsing
  • Backends must see end-to-end TLS with no proxy in the middle (use L4 passthrough)

Pros and cons

ProsCons
Rich routing and HTTP featuresMore CPU and state per request
Request-level metrics and access logsProtocol surface (HTTP only, in practice)
Easy canaries, sticky cookies, redirectsMisconfigured rules become outages
Natural fit for APIs and websitesExtra hop if you also terminate TLS again upstream
L7 mental model (HTTP request routing)
Client L7 LB Backends
|-- TLS + HTTP --------->| terminate TLS |
| GET /api/orders |-- rule: /api/* --------------->| api-pool
| GET /assets/app.js |-- rule: /assets/* ------------->| static-pool

One TCP connection (especially HTTP/2) can carry many requests; L7 can send those requests to different pools.

TAKEAWAY

L7 is a request router that happens to load-balance. If your policy needs the URL or headers, you are in L7 territory.


Network vs application load balancer (cloud names)

Vendors productise the same OSI split with different names. Map the layer, not the logo.

KindLayerCloud examplesBalances on
Network (NLB)L4AWS NLB, Azure Load Balancer, GCP Network LBIP:port, TCP/UDP
Application (ALB)L7AWS ALB, Azure App Gateway, GCP HTTP(S) LBHost, path, headers

Same split outside the big three: HAProxy/Nginx stream and LVS/IPVS are typically L4; Nginx/Envoy HTTP and API gateways are typically L7.

Examples in practice

WorkloadPreferWhy
Public HTTPS API with /v1 and /v2 servicesApplication (L7)Path rules to different target groups
Postgres or Redis behind a stable VIPNetwork (L4)TCP pipe; no HTTP
gRPC microservices with one hostnameApplication (L7) (HTTP/2)Request/stream-aware
Millions of WebSocket or MQTT connectionsOften Network (L4) or specialised L7Connection scale; check vendor limits
TLS passthrough to pods that present certsNetwork (L4)Opaque TCP to backend
Host-based multi-tenant a.example.com / b.example.comApplication (L7)SNI + Host routing

GATEWAY ≠ LOAD BALANCER

API gateways and service meshes (Envoy, Istio, Kong) often include L7 load balancing plus auth, rate limits, and schema checks. The L4/L7 distinction still applies to the balancing part; the product may do more than balance.


Algorithms and health checks

Scheduling (how a target is chosen)

AlgorithmIdeaCommon on
Round-robinNext target in turnL4 and L7
Least connectionsPrefer the least-busy targetL4 and L7
WeightedSend more traffic to larger instancesBoth
Consistent hash / source IPSame client tends to same backendL4 stickiness
Cookie stickinessSame session to same backendL7

Health checks

LayerTypical probeFailure signal
L4TCP connect (or UDP synthetic) to portConnect timeout, RST
L7GET /healthz expecting 200Bad status, slow TTFB, TLS errors

L7 checks catch "port open but app wedged." L4 checks are cheaper and blinder. Use both in deep stacks (LB L7 check + app readiness).


Request path walkthrough

One tab per layer: steps, then the swim lane.

  1. Resolve name → network LB VIP (DNS).
  2. Open TCP (or UDP) to VIP:port.
  3. LB picks a healthy target (hash, RR, least conn).
  4. Bytes flow (preserve or NAT client IP by mode).
  5. Unhealthy target leaves the pool; new flows go elsewhere.

When to choose L4 vs L7

NeedChoose
Route by URL path or HostL7 / Application LB
Database, Redis, custom TCP/UDPL4 / Network LB
TLS passthrough to backendsL4
TLS terminate + WAF + canary headersL7
Max connections, minimal proxy costL4 (often)
Request logs, status-based healthL7
gRPC / HTTP/2 microservices front doorL7

Rule of thumb: if the scheduling decision needs application bytes, use L7. If it only needs "this TCP/UDP flow to a healthy IP:port," use L4.

Stacks often combine them: CDN (edge) → Application LB (HTTP) → Network LB or Service (TCP to pods) or NLB → ingress controller (L7). Own each hop's job.

Common mistakes

MistakeWhy it hurts
Expecting path routing on an NLBL4 never sees /api; rules will not exist
Putting Postgres behind an ALBApplication LBs speak HTTP, not the DB wire protocol
Health check = TCP only for a wedged JVMPort accepts SYN while /healthz would fail
Source-IP stickiness behind CGNATThousands of users share one IP; affinity collapses
Double TLS without a reasonLatency and cert sprawl; know who terminates (HTTPS lifecycle)
One giant ALB rule soupUndebuggable routing; split by domain or intentional gateway

Final takeaway

"Load balancer" is not one product. Layer 4 distributes connections with IP and port. Layer 7 distributes requests with HTTP semantics. Network load balancers are the cloud name for L4; Application load balancers are the cloud name for L7. Choose the layer for the decision you need, put health checks at the same depth as the failure mode you care about, and keep DNS/CDN steering separate from instance selection.