CYART // SCHEMA · REQUEST-PATH7 NODES · 6 LINKSLOOKUP1HTTPS234READ / WRITE5HIT FIRSTACTOR · User — browser / appACTOR01Userbrowser / appEDGE · DNS — resolves nameEDGE02DNSresolves nameEDGE · CDN / WAF — TLS ends hereEDGE03CDN / WAFTLS ends hereEDGE · Load balancer — L7, health checksEDGE04Load balancerL7, health checksSERVICE · API service — authn + authzSERVICE05API serviceauthn + authzDATA · Database — primary + replicasDATA06Databaseprimary + replicasDATA · Cache — short TTLDATA07Cacheshort TTL

Load balancers and reverse proxies

What sits between the internet and your servers: layer 4 versus layer 7 load balancing, where TLS ends, how to find the real client IP without trusting forged headers, and the proxy mistakes behind request smuggling and origin bypass.

● EasyFREEBy ·Design problem·15 min read·Beginner → Intermediate
load balancerreverse proxyTLS terminationX-Forwarded-Forrequest smugglingorigin protection

01What they are and why security cares

✓What you will learn
  • Tell a layer 4 load balancer from a layer 7 reverse proxy, and what each can see and enforce
  • Decide where TLS terminates and whether traffic is re-encrypted to the backend
  • Recover the real client IP from X-Forwarded-For without trusting what the client wrote
  • Avoid the classic proxy failures: origin bypass, request smuggling, exposed admin ports
◎Interview radar

What they probe: whether you know what the box in front of your service can actually inspect, which hop decides the client IP your rate limiter uses, and what happens if someone reaches the backend directly. Where candidates slip: drawing a load balancer as a magic box, rate limiting on the leftmost X-Forwarded-For entry, leaving backends reachable from the internet.

A load balancer spreads incoming connections across several servers so no single one is overloaded and a failed one can be taken out of rotation. A reverse proxy is a server that accepts requests on behalf of backends and forwards them; it can also terminate TLS, add headers, cache, compress and enforce rules. In practice the two words overlap: NGINX, HAProxy, Envoy and cloud load balancers all do both.

Layer 4 (transport)Layer 7 (application)
SeesIP addresses, ports, TCP or UDPFull HTTP: method, path, headers, cookies, body
Routes by5-tuple, connection hashHost, path, header, weight, user cohort
TLSUsually passes it through untouchedUsually terminates it
Security it can enforceIP allow lists, SYN flood absorption, connection limitsWAF rules, auth checks, rate limits per user or route, header hygiene
CostVery cheap per connectionMore CPU per request, more moving parts
iEvery hop is a trust decision

The proxy is the first code in your system that parses attacker input. Whatever it adds, strips or rewrites becomes something the backend trusts. Say out loud which headers the proxy sets and which it removes.

02A typical layout and its trust boundaries

FigWhere the proxy sits
CYART // SCHEMA · REQUEST-PATH7 NODES · 6 LINKSLOOKUP1HTTPS234READ / WRITE5HIT FIRSTACTOR · User — browser / appACTOR01Userbrowser / appEDGE · DNS — resolves nameEDGE02DNSresolves nameEDGE · CDN / WAF — TLS ends hereEDGE03CDN / WAFTLS ends hereEDGE · Load balancer — L7, health checksEDGE04Load balancerL7, health checksSERVICE · API service — authn + authzSERVICE05API serviceauthn + authzDATA · Database — primary + replicasDATA06Databaseprimary + replicasDATA · Cache — short TTLDATA07Cacheshort TTL
  • ACTOR
  • EDGE
  • SERVICE
  • DATA
  • Live flow
  • Lookup / async
//A typical request path: DNS sends the user to an edge tier (CDN and WAF) and then a layer 7 load balancer, either of which can terminate TLS. The load balancer health-checks a pool of application servers in a private network and forwards only to healthy ones; the servers accept traffic only from it.
  1. DNS points the public name at the load balancer (often anycast or a cloud managed address).
  2. The load balancer terminates TLS with a certificate whose private key it holds, so it sees plaintext.
  3. It normalises the request (rejects malformed or ambiguous framing), strips client-supplied internal headers, and appends X-Forwarded-For and X-Forwarded-Proto.
  4. It forwards to a healthy backend chosen by round robin, least connections or a consistent hash, ideally over TLS again.
  5. Backends sit in private subnets and accept connections only from the load balancer's addresses or identity.

Where should TLS end?

OptionGainCost
Terminate at the LB, plaintext to backendSimple, LB can inspect and route on HTTPAnyone on the internal network can read and alter traffic
Terminate and re-encryptLB inspects, internal hop still protectedTwo certificates to manage, slight CPU cost
TLS passthrough (layer 4)Private key only on backends, end-to-endLB cannot see HTTP: no path routing, no WAF, no per-user limits
mTLS to backendBackend can prove the request came via the LBNeeds an internal CA and rotation
Is re-encryption affordable?txt
public traffic        20,000 requests / s at peak
keep-alive reuse      ~10 requests per TLS connection
new handshakes        20,000 / 10 = 2,000 / s
TLS 1.3 full handshake  ~1 ms CPU
  -> 2,000 x 1 ms      = 2 core-seconds per second = 2 cores just for handshakes
re-encrypt to backend  pooled connections, handshakes rare -> negligible
conclusion            re-encryption is affordable; do not drop it to save CPU
▭On the whiteboard · Layout3 points
01
edge
DNS → LB (TLS terminates, WAF, rate limits)
02
inside
LB → backends over TLS or mTLS, private subnets only
03
headers
strip client X-Forwarded-*, append own, pass X-Request-Id

03Deep dive: finding the real client IP

Behind a proxy, every request appears to come from the proxy's address. The proxy adds the client address to X-Forwarded-For (XFF), appending to whatever is already there. The header is a comma-separated list, oldest first. The client can send its own XFF header, so the leftmost entries are attacker-controlled. Only entries added by proxies you operate are trustworthy.

Reading X-Forwarded-For safelytxt
client sends:   X-Forwarded-For: 1.2.3.4            (forged)
real client IP: 203.0.113.9
CDN appends:    X-Forwarded-For: 1.2.3.4, 203.0.113.9
your LB appends the CDN edge IP:
                X-Forwarded-For: 1.2.3.4, 203.0.113.9, 198.51.100.7

trusted proxies on the path: 2 (CDN, LB)
rule: take the entry 2 from the right  -> 203.0.113.9   (correct)
wrong rule: take the leftmost entry    -> 1.2.3.4       (attacker chose it)
Count trusted hops from the right, never take the leftmostts
/**
 * Return the client IP given the number of trusted proxies that append to XFF.
 * The rightmost entry was added by the proxy closest to us, so skip trusted hops from the right.
 */
export function clientIp(xff: string | undefined, socketIp: string, trustedHops: number): string {
  if (!Number.isInteger(trustedHops) || trustedHops < 0 || trustedHops > 10) {
    throw new RangeError("trustedHops must be an integer in 0..10");
  }
  if (trustedHops === 0 || !xff) return socketIp;           // no proxy, or nothing appended
  const parts = xff.split(",").map((s) => s.trim()).filter(Boolean);
  if (parts.length > 50) return socketIp;                     // absurd header: do not trust it
  const idx = parts.length - trustedHops;
  if (idx < 0) return socketIp;                               // fewer entries than hops: misconfigured or forged
  return parts[idx];
}
  • The standard Forwarded header (RFC 7239) has the same trust problem; the parsing rule is identical.
  • For layer 4 load balancers, the PROXY protocol (from HAProxy) prepends the client address to the TCP stream instead of a header; only accept it from known proxy addresses.
  • Whatever IP you settle on feeds rate limits, audit logs and geo rules. A forgeable IP breaks all three.
!Strip internal headers at the edge

If backends trust a header such as X-User-Id or X-Internal: true set by an internal gateway, the edge proxy must delete any client-supplied copy first. Otherwise a client simply sends the header and becomes whoever it likes.

04Failure modes and attacks

FailureWhat goes wrongDefence
Origin bypassBackends or the origin behind a CDN are reachable directly, skipping the WAF and rate limitsPrivate subnets, firewall to LB or CDN ranges only, mTLS or a secret header from the CDN, origin IP never in DNS history
Request smugglingProxy and backend disagree on where a request ends (Content-Length versus Transfer-Encoding), so an attacker hides a second request inside the firstReject ambiguous requests at the proxy, use HTTP/2 end to end where possible, keep proxy and server versions current
Host header abuseProxy routes or the app builds links from an attacker-chosen HostAllow list of hosts at the proxy; default virtual host returns 404
Slow clients (Slowloris)Thousands of connections send headers a byte at a time and exhaust backend workersProxy buffers full requests and enforces header and body timeouts
Exposed admin interfaceEnvoy admin, HAProxy stats or a cloud LB API reachable from the internetBind admin to localhost or a management network, require auth
Unhealthy backend left in rotationErrors for a share of usersActive health checks plus passive outlier ejection, connection draining on deploy
iFail closed on parsing, not on availability

If a request is malformed, the proxy rejects it. If a backend is down, the proxy routes around it. Mixing these up (passing odd requests through to be safe, or failing all traffic when one backend is sick) is a common design slip.

05Summary, follow-ups and self-test

▭On the whiteboard · Remember3 points
01
L4 vs L7
L4 sees connections, L7 sees HTTP; security rules need L7
02
TLS
terminate at the LB, re-encrypt inside; passthrough only when the LB must not see plaintext
03
IP
count trusted hops from the right of XFF; strip internal headers at the edge
“How to say it in the room

"Clients hit a layer 7 load balancer that terminates TLS, so it can apply WAF rules and per-user rate limits, and it re-encrypts to the backends, which live in private subnets and only accept traffic from it. It strips any client-supplied internal headers and appends its own X-Forwarded-For. Downstream, I read the client IP by counting my trusted proxy hops from the right of that header, never the leftmost entry, because the client can write that one."

Q1When would you choose TLS passthrough?+

When the load balancer must not hold the private key or see plaintext, for example a payment backend in a stricter compliance zone. You give up HTTP routing, WAF and per-user limits at that hop and push them to the backend.

Q2How do you stop someone bypassing your CDN and hitting the origin directly?+

Firewall the origin to the CDN's published ranges, and require proof from the CDN on each request, such as mTLS (authenticated origin pulls) or a secret header rotated regularly. Never publish the origin IP in DNS.

Q3Why does request smuggling happen at all?+

Two HTTP parsers on the same stream disagree about message length. The front proxy forwards what it thinks is one request; the backend reads two. The cure is strict, identical parsing or rejecting ambiguous framing at the front.

Test yourself

Traffic flows client to CDN to your load balancer to the app. The app sees `X-Forwarded-For: 10.0.0.5, 203.0.113.9, 198.51.100.7`. Which entry is the client IP?

Finished this one?
Tick it and it shows as done on the roadmap and the course.