System Design · Note 5 of 22
Content Delivery Networks (CDN)
Quick answer
What is a CDN in system design?
A content delivery network is a fleet of edge servers spread across many regions that cache copies of a site's content close to users. A user's request goes to a nearby edge instead of the distant origin, which cuts round-trip latency, absorbs traffic the origin would otherwise serve and keeps working when one location fails.
On this page
A CDN is a cache that sits in many places at once: copies of your content live on edge servers close to your users.
What a CDN does
- Cuts latency: requests travel to a nearby edge, not across the world.
- Offloads the origin: most requests are answered from edge caches.
- Absorbs spikes and attacks: a large, spread-out fleet soaks up load.
- Serves static assets best: images, video, CSS, JS, downloads.
Distance sets a floor that no server tuning removes:
Without a CDN every user fetches from one origin in one region. The distances here are illustrative: 13,000, 6,500, 7,500 km.
Transcript · 6 steps
How an edge fills: origin pull
- The first request for a file at an edge is a miss; the edge fetches it from the origin.
- The edge stores the copy for as long as
Cache-Controlallows. - Later requests to that edge are hits and never reach the origin.
- Each edge fills on its own, so the origin sees roughly one miss per edge per file.
Users ask the nearest edge for /logo.png. The edge starts with an empty cache.
Transcript · 7 steps
Freshness: TTL and revalidation
Cache-Control: max-age=60makes a copy fresh for 60 seconds.- A fresh copy is served without asking the origin — even if the origin changed.
- An expired copy is revalidated with
If-None-Match: <ETag>: - unchanged →
304 Not Modified, no body, fresh again; - changed →
200with the new file. - The TTL is the trade-off: longer means fewer origin trips but staler content.
At 0s the first request misses. The edge fetches v1 with Cache-Control: max-age=60 and keeps it fresh until 60s.
Transcript · 6 steps
Cache keys and invalidation
- The edge looks copies up by a cache key, by default roughly host + path + query.
- Two URLs for the same bytes are two keys — and two origin fetches.
- Keep only what changes the response in the key: drop tracking parameters, sort the rest.
- To change a cached file:
- Versioned URLs (
app.3f9c2.js): a new name is a new key; cache old names for a year. - Purge through the CDN's API: works for fixed URLs, but takes time to reach every edge.
The edge looks each request up by its cache key. /a.png?w=200 is new under both keys, so both miss and fetch from the origin.
Transcript · 6 steps
Push vs pull
| Pull CDN | Push CDN | |
|---|---|---|
| How content arrives | Edge fetches on first miss | You upload ahead of time |
| First request | Slower (a miss) | Fast |
| Effort | Point the CDN at the origin | Manage uploads and deletes |
| Fits | Most websites, changing assets | Large, rarely changing files |
- Never cache personal responses on a shared edge: send
Cache-Control: privateorno-store. - DNS or anycast routing sends each user to a nearby edge — see DNS.
Interview questions
- What would you put on a CDN? Static assets and media; shared API responses only with short TTLs.
- How do you deploy a new version of a cached file? Use content-hashed file names so a deploy changes the URL; purge only what has a fixed URL.
- What is a cache hit ratio and why does it matter? The share of requests answered at the edge; every miss is origin load and a slow response.
- Why did users still see the old file after a deploy? It was fresh by its TTL at the edges (and maybe in browsers); purge it or version the URL.
Next: Database Replication · Before this: Caching
Common questions
How does a CDN reduce latency?
Every round trip costs time proportional to distance, and a first HTTPS request needs several of them. An edge a few hundred kilometres away answers the handshakes and serves the cached file, so the user never waits on a round trip to the far origin.
What is the difference between a push CDN and a pull CDN?
A pull CDN fetches a file from the origin the first time an edge is asked for it and caches it from then on. A push CDN has you upload content to the CDN ahead of time. Pull needs no upload step; push suits large files that change rarely and must never miss.
What happens when the TTL of a cached file expires?
The edge does not throw the copy away. It revalidates: it asks the origin with If-None-Match or If-Modified-Since, and the origin answers 304 Not Modified with no body if nothing changed, or 200 with the new file if it did.
How do you update a file that a CDN has cached?
Either purge it through the CDN's invalidation API, which takes effect across edges after a short delay, or give each version a new URL such as app.3f9c2.js. Versioned URLs let you cache for a year and change instantly, because a new name is a new cache key.
Can a CDN cache dynamic or personalised pages?
Shared responses can be cached for seconds, which still absorbs bursts. Anything personal must be marked private or no-store so an edge never serves one user's page to another. CDNs still help dynamic traffic by terminating TLS near the user and keeping warm connections to the origin.