System designCore3 min

Content Delivery Networks (CDNs)

Serve files from a server a few milliseconds from the user instead of one on another continent, and keep most of the traffic off your own servers.

1 · The problem

Light is fast, but the planet is big

A round trip from Sydney to a server in Virginia takes about 200 ms before any work is done, and loading a page takes many round trips. No amount of server tuning fixes distance: you have to move the content closer.

Same city
5 ms round trip
Same continent
40 ms round trip
Across an ocean
150 ms round trip
Opposite side of the world
250 ms round trip

Typical figures, not a benchmark.

2 · How it works

Edge servers in front of your origin

User in SydneyEdge serverSydneyOriginyour servers, Virginia5 msmiss onlyUser in SydneyEdge serverSydneyOriginyour servers, Virginia5 msmiss only

The first request for a file in each city goes to the origin; every later one is served at the edge.

  1. The user asks for `cdn.example.com/app.js`.

    DNS (or anycast routing) sends them to the nearest healthy edge.

  2. The edge checks its cache.

    On a hit it answers straight away.

  3. On a miss it asks the origin,

    stores the response for as long as its headers allow, and answers.

  4. Everyone nearby now gets a hit.

    Popular files are fetched from the origin once per edge, not once per user.

3 · Keeping it fresh

Headers decide, and file names help

ToolWhat it does
`Cache-Control: max-age=…`How long browsers and the CDN may reuse a response
`s-maxage`A separate lifetime for shared caches such as the CDN
`stale-while-revalidate`Serve the old copy while fetching a new one in the background
Versioned file names`app.3f9a1c.js`: a new build is a new URL, so old copies never need purging and can be cached for a year
PurgeAsk the CDN to drop a URL now. Slow to spread and easy to forget, so a last resort

Pull CDNs (above) fetch from the origin on demand, and suit most sites. Push CDNs get files uploaded ahead of time, which suits large, predictable content such as a video catalog.

4 · What goes on a CDN

Static first, then more

ContentFitWhy
Images, scripts, styles, fontsIdealThe same for everyone, change rarely
Video segmentsIdealHuge, and the same few popular ones are watched by many
Public API responsesGood, with short TTLsA few seconds of caching takes most of the load at peaks
Personal pagesRarelyDifferent per user; cache the shared parts instead

CDNs also absorb traffic spikes and many kinds of attack, because the edge network is far bigger than your origin.

Check yourself

3 questions

1. Why does a CDN make a page load faster for a user far from your servers?
2. You ship a new `app.js`. What's the most reliable way to make sure users get it?
3. Which is the poorest fit for CDN caching?

Takeaways

Remember this

  • CDNs beat distance by serving content from edges near users.
  • The origin sees one request per edge per file, not one per user.
  • Control freshness with Cache-Control, and version file names instead of purging.
  • Static files and video first; short-TTL public API responses next.