CDN (content delivery network)
A network of servers around the world that caches static content (images, video, scripts) close to users.
Why it matters
Distance costs time: a user in Sydney fetching from London waits around a quarter of a second for each round trip, and every byte you send costs bandwidth. Big files and users all over the world make this much worse.
The idea
Instead of every user fetching a video from your one datacentre, copies are served from a nearby 'edge' server. This cuts latency, takes load off your servers and absorbs traffic spikes. Content is pulled into the CDN on the first request (pull CDN) or uploaded in advance (push CDN).
Local supermarket branches stocking popular products, so you don't have to drive to the central warehouse.
A supermarket can check the warehouse for the newest stock. A CDN serves its cached copy until it expires. So after you change a file, users may get the old one, unless you put a version in the file name or purge the cache.
How it works
- You point a hostname, like cdn.example.com, at the CDN provider.
- A user's request goes to the nearest edge server (found through DNS or 'anycast' routing).
- Cache hit: the edge already has the file and returns it in a few milliseconds.
- Cache miss: the edge fetches the file from your origin server once, stores it, and returns it. Every later user in that region gets the fast copy.
- The file stays until its TTL expires. To update it straight away, publish a new file name (app.3f9a.js → app.7c2b.js) or purge the old one.
Example
Netflix places its own CDN boxes inside internet providers' networks, so most video traffic never crosses the wider internet.
Key points
- Best for static, popular, cacheable content: images, video segments, JavaScript and CSS files.
- Pull CDN: fetches from your origin server on a cache miss. Push CDN: you upload content ahead of time.
- Control freshness with TTL headers; put a version or hash in file names (app.3f9a.js) so updates appear immediately.
- Also helps with DDoS protection, and can handle TLS close to the user.
Common mistakes
- Caching personalised pages at the edge, and showing one user's data to another.
- Long TTLs on files that change, without versioned names.
- Forgetting that the first request in each region is still slow (a 'cold' cache).
Not true: A CDN makes even the very first request in each region fast.
In fact: The first request in a region is a cache miss: the edge server has to fetch the file from the origin. Only the requests after it are fast.
In the interview
I'd serve static assets and media through a CDN, so users download them from a nearby edge server. That lowers latency and takes most of the bandwidth off our origin. I'd use versioned file names so updates show up immediately.
Part of Unit 1: Networking and APIs in the free System Design course.