Fundamentals · 07
Content delivery networks (CDNs)
Serve static and cacheable content from servers near your users, using pull or push models, cache headers and versioned URLs.
Light in fibre is fast, but a round trip between continents still takes ~150 ms, and a page needs many round trips. A content delivery network fixes that by keeping copies of your content in points of presence (PoPs) around the world, so most requests are answered a few milliseconds from the user.
An origin shield is an extra cache layer between the edges and your origin, so a cache miss in 50 PoPs becomes one request to the origin rather than 50.
Pull vs push
Pull CDN
- Edge fetches from the origin on the first request, then caches it
- Zero upload work; new files just appear
- First user in each region pays the miss
- Best for most websites and APIs
Push CDN
- You upload files to the CDN ahead of time
- No first-request miss; you control exactly what’s there
- You manage uploads, storage and expiry
- Best for large, rarely changing files (video libraries, game patches)
Controlling freshness
Cache-Control: public, max-age=31536000, immutable: cache for a year, for files whose name changes when their content does.Cache-Control: max-age=60: cache briefly, for things that change.Cache-Control: no-store: never cache private or per-user responses.ETagandLast-Modifiedlet caches revalidate cheaply (304 Not Modified) instead of downloading again.
What else a CDN gives you
- Lower origin load and cost: most bandwidth is served from the edge.
- Spike and DDoS absorption: huge distributed capacity in front of your servers.
- TLS termination near users: faster handshakes.
- Edge compute: small functions at the PoP for redirects, A/B tests or auth checks.
- Dynamic acceleration: even uncached API calls benefit from the CDN’s optimised network paths back to the origin.
Pros
- Big latency win for static and media content
- Offloads bandwidth and requests from your servers
- Built-in resilience against spikes and attacks
Cons
- Cost per GB at very large scale
- Stale content if cache headers are wrong
- Purges take time to propagate
- Personalised content can’t be cached as-is
Test yourself
Answer in your head, then click a card to check. All cards are in the Anki deck.