Module F-15·20 min read

TLS termination, routing, edge caching, cache keys that accidentally cache nothing, and purging content you already told the world to keep.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module F-15 — Reverse Proxies, API Gateways, and CDNs

What this module covers: The layer in front of everything, and the three distinct things people call "the edge." This module separates reverse proxy, API gateway, and CDN by the job each does, then covers what belongs at the edge and what must never move there, TLS termination and who holds the certificate, Cache-Control semantics precisely (including the four directives that solve real problems and the two that don't do what their names suggest), conditional requests, CDN cache keys and the misconfiguration that leaks one user's data to another, purging strategies and why versioned URLs beat purging, origin shielding and request collapsing, and the protections a proxy gives you that have nothing to do with caching.


Three Different Things

Reverse proxyAPI gatewayCDN
Examplesnginx, HAProxy, Envoy, CaddyKong, Apigee, AWS API Gateway, EnvoyCloudflare, Fastly, CloudFront, Akamai
Primary jobRoute, terminate TLS, bufferEnforce per-consumer policyCache content near users
LocationYour infrastructureYour infrastructureHundreds of PoPs worldwide
Typically knows aboutBackendsConsumers, keys, quotasCache keys, TTLs, geography

They overlap heavily in practice — a CDN does reverse-proxy work, Envoy can be all three — but the jobs are distinct, and conflating them is how policy ends up in the wrong place.

A reverse proxy sits in front of your application servers: routes by host and path, terminates TLS, buffers requests and responses, adds and strips headers, compresses. It's infrastructure plumbing, and it protects your app servers in ways covered at the end of this module.

An API gateway enforces policy about consumers: authentication and API keys, per-client rate limits and quotas (Module P-18), request/response transformation, usage metering, versioned routing. The distinguishing feature is that it knows who is calling, not just what they're asking for.

A CDN is a geographically distributed cache. Its value is proximity — a cache hit at a PoP 10ms from the user never reaches your infrastructure at all. And as Module F-5 established, terminating TLS 10ms away instead of 180ms away is a large latency win even for content the CDN can't cache.

Analogy: a large office building's front desk, its security barrier, and the newsagent in the lobby. The front desk takes every visitor, checks where they are going and sends them to the right lift — it does not care who you are, only where you belong; that is the reverse proxy. The barrier knows your pass, your access level and how many times you have badged in today, and turns you away by policy; that is the API gateway. The newsagent stocks the same paper everyone wants so nobody has to travel to the printer for it — and stocks it in the lobby of every building in the city; that is the CDN. Conflating them is how the barrier ends up deciding routing, or the newsagent ends up handing one visitor a parcel addressed to another, which is exactly the cache-key failure later in this module.


What Belongs at the Edge

The edge is the right home for cross-cutting concerns that every request needs and that don't require your domain data:

  • TLS termination, HTTP/2 and HTTP/3, compression
  • Routing and path rewriting
  • Coarse rate limiting and DDoS absorption
  • WAF rules and IP reputation filtering
  • Static asset caching and image transformation
  • Redirects, canonical host enforcement, security headers
  • Cheap authentication checks — verifying a signature with a local key

And the rule for what must not move there:

Business logic must not live in the gateway.

This is worth being firm about, because gateway products actively encourage it — most support request transformation, conditional routing, and scripting. The reasons it's a bad trade:

  • It's logic outside your repository, often outside version control, usually untested, and invisible to anyone reading the codebase. A rule in a gateway console is a rule nobody knows exists.
  • It can't be tested locally. Behaviour that only exists in deployed configuration is behaviour you find out about in production.
  • It becomes a shared mutable bottleneck. Every team's special case accumulates in one config, and eventually nobody will change it.
  • It's the wrong deployment unit. A logic change now means a gateway config change, with a different review process and a different rollback story.

The line is: the edge decides whether a request proceeds; the application decides what the request means. An edge check that rejects a request with an invalid signature is fine. An edge rule computing a discount is a maintenance liability with a long tail.

Why this matters in production: the failure mode is diagnostic. A request behaves in a way the code cannot explain, because the explanation is in a config file in a vendor console, written by someone who has since left. Time to resolution on those is measured in days.


TLS Termination: Where, and Who Holds the Key

Where you terminate TLS determines who can read the traffic and how much handshake cost the user pays.

Terminate at the CDN edge (the common choice): the expensive handshake happens close to the user, and the CDN holds a warm pooled connection to your origin. The CDN can therefore see the plaintext — which is what lets it cache, compress, and apply a WAF, and is also exactly what some compliance regimes forbid.

Terminate at your own load balancer: you keep control of certificates and plaintext; users pay the full-distance handshake.

End-to-end encryption with re-encryption at each hop: the edge decrypts, processes, and re-encrypts to your origin. This is the normal production configuration, and the origin certificate should be validated rather than skipped — an unauthenticated origin connection is a real gap, and "we'll fix the cert later" survives for years.

mTLS to the origin: the origin only accepts connections presenting a client certificate the CDN holds. This closes the hole where someone who discovers your origin IP bypasses the CDN entirely — including its WAF and rate limits. Worth doing, and routinely skipped.


Cache-Control, Precisely

Most caching bugs at this layer are misunderstood directives. The ones that matter:

http

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.