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.
Typical figures, not a benchmark.
2 · How it works
Edge servers in front of your origin
The first request for a file in each city goes to the origin; every later one is served at the edge.
- The user asks for `cdn.example.com/app.js`.
DNS (or anycast routing) sends them to the nearest healthy edge.
- The edge checks its cache.
On a hit it answers straight away.
- On a miss it asks the origin,
stores the response for as long as its headers allow, and answers.
- 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
| Tool | What 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 |
| Purge | Ask 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
| Content | Fit | Why |
|---|---|---|
| Images, scripts, styles, fonts | Ideal | The same for everyone, change rarely |
| Video segments | Ideal | Huge, and the same few popular ones are watched by many |
| Public API responses | Good, with short TTLs | A few seconds of caching takes most of the load at peaks |
| Personal pages | Rarely | Different 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
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.