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

  1. You point a hostname, like cdn.example.com, at the CDN provider.
  2. A user's request goes to the nearest edge server (found through DNS or 'anycast' routing).
  3. Cache hit: the edge already has the file and returns it in a few milliseconds.
  4. 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.
  5. 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

Common mistakes

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.