System Design Playbook
13 · Performance & traffic6 min read

What is a CDN and how does it make sites fast worldwide?

A Content Delivery Network keeps copies of your files on servers close to your users, so a visitor in Tokyo doesn't wait for a server in Virginia.

In one sentence

A CDN is a cache spread around the world. Users download images, video, scripts and even pages from the nearest edge server; only the first request in each region goes all the way to your origin.

The problem: distance

Data travels through fibre at about two thirds of the speed of light. A round trip from Sydney to Virginia takes around 200 ms before your server does any work, and loading a page needs many round trips. Faster servers can't fix physics; getting closer can.

One origin in Virginia, users all over the world
Origin · VirginiaedgeNew YorkedgeMadridedgeSão PauloedgeMumbaiedgeTokyoedgeSydney
Requests
0
Average latency
—
Cache hit ratio
—
Requests to origin
0
Without a CDN every request crosses oceans to the origin. With one, the first request in each region is a miss (the edge fetches the file once), and every following request is served from a server a few milliseconds away. Latencies are illustrative.
Turn the CDN off and on. Watch the first request in each city (MISS) and every request after it (HIT).

How a CDN works

  1. Your domain points to the CDN (for example cdn.example.com). DNS sends each user to the nearest edge location.
  2. The edge checks its cache. Hit: it answers in a few milliseconds.
  3. Miss: the edge fetches the file from your origin (your server or an S3 bucket), stores a copy and answers.
  4. How long the copy lives is set by your Cache-Control headers (max-age, s-maxage).

Big providers (CloudFront, Cloudflare, Akamai, Fastly) run hundreds of locations. Netflix goes further and places its own servers inside internet providers' networks.

What to put on a CDN

ContentFitTypical setup
Images, fonts, JS, CSSPerfectHashed file names (app.3f9a.js) + cache for a year
Video segmentsPerfectHLS/DASH chunks; most of the traffic of YouTube or Netflix
Public HTML pagesGoodShort TTL (seconds to minutes), or stale-while-revalidate
API responses that are the same for everyoneSometimesShort TTL, cache key without cookies
Personal data (cart, inbox)NoMark as private or no-store

Updating files: versioning beats purging

When a file changes you can purge it from every edge (it takes seconds to minutes and is often rate limited), or avoid the problem by versioning: the new file gets a new name (logo.v2.png), so there's nothing to invalidate. Build tools do this automatically with content hashes.

Bonus benefits: the CDN absorbs traffic spikes, hides your origin's IP and usually includes DDoS protection and a web application firewall.

Key takeaways

  • 01A CDN caches content on edge servers near users, cutting latency caused by distance.
  • 02Only misses reach the origin, so the CDN also absorbs most of the traffic.
  • 03Perfect for static files and video; careful with anything personal.
  • 04Prefer versioned file names over purging when content changes.

Check your understanding

1 of 3

What does a CDN do?

Related concepts