CDN and the edge
A CDN is a cache with geography. Tier 0 established the unmovable fact: Mumbai to Virginia is ~180 ms round trip, and no code fixes physics. What does fix it: don't make the trip. A CDN operates hundreds of PoPs (points of presence) worldwide; your user in Jaipur fetches from a PoP a few dozen kilometres away, and only that PoP's occasional refills travel to your origin server. One user pays the long trip; the next hundred thousand get the short one.
What belongs on a CDN
Anything static and identical for many users: images, video segments, JavaScript, CSS, fonts. Increasingly also cacheable public API responses (that trending list every client requests) and even whole pages rendered at the edge. The disqualifier is per-user content — and getting that wrong is the incident in the trap below.
Cache-Control: the contract, header by header
You control CDN and browser behaviour through HTTP headers — a small language worth speaking fluently:
max-age=3600— any cache may serve this for an hour without asking you.s-maxage=86400— overrides max-age for shared caches only. The power move: cache hard at the CDN (a day) but lightly in browsers (a minute) — because you can purge your CDN with an API call, but you can never purge a user's browser.stale-while-revalidate=60— when an entry expires, serve the stale copy immediately and refresh in the background. Users never wait on a refill; content is never more than a minute behind. Excellent and weirdly underused.private— browsers only, shared caches must not store this. Anything user-specific must carry it. The failure mode when it's missing: the CDN caches user A's account page and serves it to user B. This is a real incident category that recurs across the industry, and it's the bad kind of incident — the kind with a disclosure letter.
Content-hashed filenames — the cleanest trick in web caching
Name each asset by a hash of its own content — app.9f2a1c.js — and serve it with max-age=31536000, immutable (one year, never re-check). Deploy a change, the content changes, so the hash changes, so the URL changes — and every cache on Earth treats it as a brand-new file, fetching it immediately. The old file? Never invalidated, just never requested again; it quietly ages out. You get infinite caching and instant updates, and you never purge anything. If you adopt one pattern from this lesson, adopt this one.
Purging: the escape hatch, not the plan
CDNs let you force-remove cached content, but global purges take seconds to minutes to propagate and are rate limited. A design that needs frequent purges is a design that chose the wrong headers — with content hashing plus sensible s-maxage, purging becomes something you do twice a year, for emergencies.
Caching a response that contains anything session-specific — a name in the corner, an account balance, a "welcome back" — without private or a Vary on the auth header. The result is serving one user's data to another at CDN speed and CDN scale. Audit every cacheable endpoint with the question: "is every byte of this identical for all users?" Any hesitation means private.
The tradeoff sheet
- Problem solved
- Users a long way from your origin paying the round trip on every byte, and the origin paying to serve the same bytes repeatedly.
- Rejected, and why
- A bigger origin and a fatter pipe help throughput and do nothing about the speed of light. Regional origins solve it properly and cost you a deployment target per region.
- The trade
- A second copy of your content with its own lifetime, so every cacheable response now needs a deliberate cache-control decision and a purge story.
- First to break at 10x
- Purge latency and the long tail. At 10x, more content is uncacheable-but-popular, hit rates fall on the tail, and origin sees the traffic you assumed the edge had absorbed.
- What it does to the bill
- Usually the clearest saving on the page — edge egress is priced below origin egress and the origin fleet shrinks. The counterweight is per-request pricing on tiny, uncacheable responses.
- Day-2 operations
- Monitor hit ratio by cache key pattern, not overall. A deploy that changes a header can make everything uncacheable with no error anywhere, and the only symptom is the origin bill.