CDNs and edge caching

In one sentence

A CDN caches your responses on servers near users, keyed by URL, so most requests never reach your origin; you control freshness with Cache-Control TTLs, versioned URLs and purges.

Analogy: A publisher stocks bestsellers in every neighborhood bookstore so readers never visit the warehouse. A rare title is ordered from the regional depot, not reprinted at the press.

Two cache tiers: PoPs answer most requests, and the shield turns many PoP misses into one origin request.

How it works

  • Users reach the nearest PoP; a miss goes to your origin and leaves a copy at the edge.
  • The cache key is the URL by default; every header, cookie or parameter you add fragments the cache.
  • Ship assets under versioned URLs cached for a year as immutable; keep HTML short-lived instead of purging.
  • An origin shield funnels every PoP miss through one cache, so the origin sees ~one request per object.
  • Per-user responses are private or no-store: a shared cache must never keep them.

Check yourself

Answer to mark it learned; your answers aren't scored.

After a release, the CDN hit ratio drops from 95% to 40% and origin CPU spikes. The diff shows the cache policy now includes the whole Cookie header in the cache key. Best fix?

Go deeper

Latency ladder Interactive labCache hit ratio Interactive lab
Full explanation

A CDN is a fleet of caching servers (PoPs) spread around the world. Each user reaches the nearest one: on a hit it answers from there; on a miss it fetches from your origin and keeps a copy.

  • Cache key: by default the URL (host + path + query). Every header, cookie or parameter you add to the key splits the cache into more, colder copies.
  • Freshness: the origin sets Cache-Control. max-age applies to every cache; s-maxage overrides it for shared caches like the CDN; stale-while-revalidate serves the old copy while refreshing it.
  • Invalidation: prefer versioned URLs (app.3f9a1c.js, cached for a year as immutable) over purges; a full purge makes every PoP miss at once.
  • Origin shield: a mid-tier cache that all PoP misses go through, so the origin sees ~one request per object instead of one per PoP.

Static assets cache for months. Dynamic responses still gain: TLS ends near the user, connections to the origin stay warm, and public data can be microcached for seconds. Anything per user is private.

Code Cache-Control per kind of response
textCache-Control per kind of response
# Fingerprinted asset: a new build gets a new URL, so cache it a year
GET /static/app.3f9a1c.js
Cache-Control: public, max-age=31536000, immutable

# HTML entry point: browsers revalidate, the CDN keeps it 60 s
GET /index.html
Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=30

# Public API list: microcache at the edge, refresh in the background
GET /api/products?page=1
Cache-Control: public, s-maxage=5, stale-while-revalidate=30

# Per-user data: a shared cache must never store it
GET /api/me
Cache-Control: private, no-store
Real example

Assume a launch page served from 300 PoPs with s-maxage=60. Without a shield, each busy PoP misses once a minute: up to ~300 origin requests per minute for one URL, all at once after a purge. With an origin shield they collapse to ~1. A week later the hit ratio falls from 95% to 40%: someone added the whole Cookie header to the cache key, so every session got its own copy. Origin load jumps 12× (5% → 60% of requests). Keying on the experiment variant alone restores it.

Trade-offs +3 · −3

+ You gain

  • Lower latency: content comes from a PoP near the user, not a distant origin.
  • Offloads the origin: at a 95% hit ratio it serves only 5% of requests.
  • Absorbs traffic spikes and volumetric DDoS at the edge, before they reach you.

− You pay

  • Staleness: a change shows up only after the TTL expires or a purge runs.
  • A wrong cache key or header can leak private pages or crater the hit ratio.
  • Another vendor in the critical path, with its own outages, limits and bills.

Use it when

Static assets, media, downloads and public pages read by many users; also as the TLS and DDoS front door for APIs with a global audience.

Avoid it when

Per-user or write-heavy responses that can't be shared, single-region internal tools, or data that must never be stale, like balances or permissions.

Numbers to know 4
  • Origin load = requests × (1 − hit ratio): 95% hits → 5% reach the origin; 99% → 1%.
  • Fingerprinted assets: Cache-Control: public, max-age=31536000, immutable (one year).
  • CloudFront: first 1,000 invalidation paths a month free; a /* wildcard counts as one path (checked Nov 2026).
  • CloudFront without a cache policy uses a 24 h default TTL when the origin sends no Cache-Control.
Common pitfalls 4
  • Putting Cookie, Authorization or User-Agent in the cache key: one copy per user and a near-zero hit ratio.
  • Caching authenticated pages in a shared cache: in Dec 2015 Steam showed ~34k users' account pages to other users this way.
  • A CloudFront minimum TTL above 0 caches responses even when the origin sends no-store or private.
  • Purging everything on every deploy: all PoPs miss at once and stampede the origin.
Interview questions 3

Answer out loud first, then reveal.

How do you deploy a new frontend so no user gets a mix of old and new files?Show answer

I fingerprint every asset with a content hash, like app.3f9a1c.js, and cache it for a year as immutable: a new build is a new URL, so nothing needs purging. The HTML that points to them gets a short s-maxage, say 60 s, or one targeted purge. Order matters: upload the new assets first, then switch the HTML, and keep old assets for a few days for tabs that are still open.

Your CDN hit ratio fell from 95% to 40% after a release. What happened?Show answer

Origin load just went up 12×, so it's urgent. The usual cause is cache-key fragmentation: a new tracking parameter, a cookie or a Vary: User-Agent now in the key, so each variant gets its own cold copy. I'd diff the cache policy and response headers between releases, allow-list only the parameters that change the response, normalize the rest, and add hit ratio to the release checks.

Can a CDN help a dynamic, personalized API?Show answer

Yes, partly. Even with zero caching, the PoP terminates TLS near the user and reuses warm connections to the origin, which saves round trips. Public fragments, like a product list, can be microcached for a few seconds with stale-while-revalidate. Anything per user stays private. I'd split the page so the shared part is cacheable and only a small personalized call reaches the origin.