Lesson 2 of 13 · 18m
Load Balancers and Proxies
L4 vs L7 balancing, TLS termination vs passthrough, `X-Forwarded-For`, health-check traps, and why sticky sessions are usually a bug you kept.
A load balancer is two jobs pretending to be one: keep a pool of backends, and decide which members deserve traffic right now. The client connects to one public IP; the balancer picks a member, forwards the bytes, and forwards the response. Everything else — TLS, routing, health checks — is implementation detail with real consequences.
L4 vs L7 is the fork in the road
A Layer 4 balancer (AWS NLB, HAProxy in mode tcp) sees a TCP flow. It doesn't parse HTTP — it forwards the connection, fast enough to hold millions of concurrent flows. It can't route /api/ differently from / because it never sees a path.
A Layer 7 balancer (AWS ALB, Nginx, Envoy, HAProxy in mode http) terminates the TCP connection, parses the request, and routes on host, path, or headers. It can also see and rewrite the request — which is where X-Forwarded-For comes in.
The IP your backend actually sees
Once a balancer sits in front, every connection to the app arrives from the balancer. The backend logs 10.0.0.4 for everything; rate limiters key on the wrong client; abuse tooling goes blind. The fix is headers:
location / {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
}The app then trusts X-Forwarded-For — but only from the balancer. set_real_ip_from 10.0.0.0/24; real_ip_header X-Forwarded-For; makes that trust explicit. Skip it and a client can spoof the header, writing any IP it likes into your logs and rate-limit buckets.
On an L4 balancer there's no request to annotate — PROXY protocol v2 prepends the real client IP to the byte stream and the backend has to speak it. AWS NLB preserves source IPs to most targets natively, but HAProxy in mode tcp needs send-proxy/expect-proxy configured or your backends see the balancer.
Two more L4/L7 facts worth the trade: L4 is cheap to run hot — no request parsing means an NLB holds millions of concurrent flows an L7 balancer can't. And L7 routing is where path-based split lives: location /api/ { proxy_pass http://api; } vs location / { proxy_pass http://web; } is one config file instead of two fleets.
TLS termination vs passthrough
Terminating TLS at the balancer puts certs at the edge (one renewal path, often managed by ACM or Let's Encrypt) and lets backends speak plain HTTP on a private subnet. The trade-off: the balancer-to-backend hop is plaintext — fine on a trusted network, wrong where compliance requires encryption end-to-end. Then the balancer either passes TLS untouched (L4 passthrough, which means no path routing) or terminates and re-encrypts to HTTPS backends — and the two AWS modes don't mix: passthrough is NLB-only, while ALB always terminates, optionally re-encrypting to HTTPS targets.
Health checks decide the pool
A backend returning 200s gets traffic; one failing checks gets pulled. Passive checks — open-source Nginx's max_fails/fail_timeout — mark a backend bad only after real requests fail. Active checks (HAProxy's check, Nginx Plus, Envoy, every cloud balancer) probe /healthz on an interval and drain a dead backend before users notice.
The trap is what /healthz checks. Probe "is the process up" and a wedged app keeps receiving requests. Probe "can I reach the database" and a 3-second DB blip drains every backend simultaneously — the check turned a small failure into a total one. Test what the request needs, and no more.
Pool fairness comes from the algorithm. Round-robin spreads evenly until one backend slows; least_conn (Nginx) routes to whoever has fewest in-flight requests — the right default when request costs vary. Weights (server 10.0.1.11 weight=3) steer traffic for capacity differences or a canary. And reuse backend connections: keepalive 32; on the upstream plus proxy_http_version 1.1 saves a TCP handshake per request.
Draining, and the myth of sticky sessions
Removing a backend mid-request cuts in-flight work. Balancers drain instead — AWS calls it deregistration_delay.timeout_seconds (default 300s): stop sending new connections, let old ones finish.
The wrong model is "we need sticky sessions." Cookie affinity exists because session state lives on one server's disk. Once sessions live in Redis, any backend answers any request and stickiness is the bug you kept. Externalize state; don't bolt affinity onto it.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
One Slow Call, Three Runtimes: How PHP-FPM, Python ASGI and Node Fall Over When a Dependency Takes 200 ms
I put the same 200 ms wait behind PHP-FPM, FastAPI on uvicorn and a plain Node server, then hit each one with 50 concurrent requests. Handled correctly, the async runtimes served 205-211 req/s against FPM's 19.6. One blocking call in the wrong place took both of them down to 4.98, slower than...
PHP OOP Explained Through a Real Kitchen Brigade System
Escoffier's brigade de cuisine solved the same problem PHP's class visibility solves: who gets to touch what, and who answers to whom. Part 2 of the kitchen series, and the one where we build something real.
The Slow Death of vBulletin, and Why It Isn't Over
Forum Archaeology #1: I built plugins for this software when it ran half the internet's communities. Sixteen years after the exodus that should have killed it, vBulletin is still getting hit with critical, unauthenticated remote code execution.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.