What it is
A content delivery network is a large, shared fleet of caching reverse proxies placed in many points of presence (PoPs) around the world. Your domain points at the CDN; users are routed to a nearby PoP (by DNS answers or by anycast routing), and the PoP answers from its cache when it can. When it cannot, it fetches the object from your origin (an application, a load balancer or an object storage bucket), returns it and keeps a copy for later requests.
Why it exists
Distance costs time. A user 80 ms away from your origin waits at least 160 ms per round trip, and a page load involves many round trips: DNS, TCP and TLS handshakes, then dozens of requests. Moving copies of content near users removes most of that distance. It also removes load: requests answered at the edge never reach the origin, so the same origin can serve far more users, and traffic spikes and many kinds of attack are absorbed by the CDN's capacity instead of yours.
How it works
Hits and misses. On a hit the PoP returns the cached object. On a miss it fetches from the origin (or from a parent tier), stores the result if it is cacheable and returns it. Origin load is roughly traffic × miss ratio: going from a 90% to a 96% hit ratio cuts origin traffic by more than half. Because misses take the long path, the latency tail (P95, P99) is set by misses even when the median looks excellent.
Cache-Control. The origin controls caching through response headers. max-age sets how long any cache may reuse a response; s-maxage overrides it for shared caches such as CDNs. no-store forbids storing it, private allows only the user's browser to store it. stale-while-revalidate lets a cache serve a slightly stale copy while it fetches a fresh one in the background; stale-if-error lets it serve a stale copy when the origin fails. Validators (ETag, Last-Modified) let an expired copy be revalidated cheaply with a conditional request.
Cache keys. By default an object is identified by host and path, often plus the query string. Anything else that changes the response (a header via Vary, a cookie, a device class) has to be part of the key, and everything in the key fragments the cache. Normalise and drop parameters that do not change the response.
Changing content. Two ways: versioned URLs (put a content hash in the file name, cache for a year, reference the new name on deploy) and purges (tell the CDN to drop objects by URL, tag or prefix, or everything). Purges typically reach all PoPs within seconds to minutes, depending on the provider, but every purged object then has to be fetched again.
Tiered caching and origin shield. Each PoP has its own cache, so a file popular everywhere is fetched once per PoP per expiry. An origin shield (or tiered cache) adds a shared parent cache, usually close to the origin: PoPs fetch from the shield, and only the shield goes to the origin. Expect a modest hit ratio at the shield itself: it only sees what the edges missed, the long tail. It pays off anyway, because each of its hits saves an origin fetch that every PoP would otherwise make on its own.
When to use it
- Static assets (scripts, styles, fonts, images), media and video segments, software downloads.
- Public, shared API responses where a few seconds of staleness are fine (product listings, scores, feeds that are the same for everyone).
- Any public website, for TLS termination near users, DDoS absorption and HTTP/2 and HTTP/3 support, even when the cache hit ratio is modest.
When not to
- Highly personalised or private responses, unless you design keys and controls very deliberately; a mistake serves one user's data to another.
- Content that must never be stale and changes on every request.
- Internal traffic between services in one region: the round trip to a PoP adds latency instead of removing it.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Longer TTL | Higher hit ratio, lower origin load | Staler content, more reliance on purges |
| Versioned asset names | Cache forever and deploy instantly | Build tooling to fingerprint and rewrite references |
| Origin shield | Far fewer origin requests across many PoPs | An extra hop on shield misses, one more tier to purge |
| Wider cache key (more headers, cookies, params) | Correct variants per user group | Fragmented cache, lower hit ratio |
| stale-while-revalidate / stale-if-error | Smooth refreshes, resilience to origin failures | Users may briefly see outdated content |
Alternatives
- Serve from the origin with a regional cache or reverse proxy: enough when users are close to the origin.
- Multi-region origins: full application stacks near users, needed for dynamic, personalised responses that cannot be cached.
- Edge compute: run small functions at PoPs (redirects, header logic, A/B assignment, authentication checks) without a round trip to the origin.
Failure modes
- Purge storms: purging everything sends a burst of misses from every PoP to the origin.
- Cache-key leaks: caching a response that depends on a cookie or authorization header without keying on it serves private data to other users.
- Low hit ratio from fragmentation: tracking parameters, random query strings or
Vary: User-Agentcreate millions of distinct entries. - Origin outage: without stale-if-error, cached objects stop being served as soon as they expire.
- CDN outage or misconfiguration: the CDN itself is a dependency; a bad configuration push can take down every site behind it. Some teams keep a second CDN or a direct-to-origin fallback for critical paths.
In production
Measure hit ratio (by requests and by bytes), origin request rate, and errors and latency per PoP. Set explicit Cache-Control headers on every response instead of relying on defaults. Fingerprint static assets. Use targeted purges by URL or tag. Enable an origin shield in front of small or expensive origins. Restrict the origin to accept traffic only from the CDN (signed headers, private buckets with origin access controls) so attackers cannot bypass it.
Common misconceptions
- "A CDN is only for static files." Short-TTL caching of shared dynamic responses and edge TLS termination help dynamic sites too.
- "A 90% hit ratio means the origin is almost idle." It still sees 10% of all traffic, and that share jumps when the ratio drops.
- "Purging is free." Every purged object must be refetched by every PoP that needs it.
- "TTL guarantees freshness." It is an upper bound on reuse, not a promise of fresh content; clients and intermediate caches may hold copies too.
Interview angle
Put a CDN in front of static content and media by default and say what it buys: latency, origin offload, DDoS absorption. Then show depth: hit ratio and origin load arithmetic, Cache-Control choices, versioned assets instead of purges, cache keys for semi-personalised content, origin shield for many PoPs, and how the origin is protected and sized for a cold cache. For video or large downloads, mention that bytes served from the edge dominate the cost.
Model assumptions
The simulation is a simplified model, not a benchmark. Users request 2,000 cacheable files with Zipf(1) popularity at random (Poisson) times. The origin is modelled as a store with 10 concurrent requests and a 40 ms mean response time (about 250 requests per second); waiting more than 300 ms for a slot fails the request. Each PoP is a fast proxy (1 ms) with an LRU cache of 300 files that stores a copy after every origin fetch. TTLs are compressed: 3 simulated seconds stand in for minutes. A purge is modelled as the edge cache restarting empty. stale-if-error and stale-while-revalidate are described, not simulated. The origin shield is a proxy with its own 1,000-file LRU cache. Every cache stores whatever its next hop answers, so an edge cache also keeps copies the shield served, and the shield sees only the PoPs' real misses (its hit ratio, about a third, is the honest result of that). One-way latencies are fixed: users to a nearby PoP 8 ms, Europe to the origin 75–80 ms, North America 20 ms, Asia 90 ms. Numbers show behaviour and orders of magnitude only.