Content Delivery Networks (CDNs)
A CDN keeps copies of your files on servers around the world so users download them from nearby. Learn how pull and push CDNs work, what makes a good cache key, and how to update content safely.
In a nutshell
- A CDN serves content from edge servers close to users: lower latency and far less load on your origin.
- Pull CDN: fetches from your origin on the first request and caches it. Push CDN: you upload content ahead of time.
- Control caching with
Cache-Controlheaders; use fingerprinted file names so new versions never need purging. - CDNs also absorb traffic spikes and attacks, and terminate TLS near users.
Why a CDN
The speed of light is fixed. A user in Chennai fetching an image from a server in Virginia waits on a round trip of well over 200 ms, for every file the page needs. A CDN puts copies of those files on servers in hundreds of cities, so the round trip shrinks to a few milliseconds.
Edge servers near users
Diagram as text
- Users in Chennai → Edge: Chennai: ~5 ms
- Users in London → Edge: London: ~5 ms
- Edge: Chennai → Origin: miss only
- Edge: London → Origin
- Faster pages: static files load from a nearby edge.
- Less load and cost on your servers: most requests never reach them.
- Resilience: the CDN absorbs traffic spikes and many denial-of-service attacks.
- TLS at the edge: the encryption handshake happens close to the user, saving round trips.
Pull vs push
Pull CDN: the first request fills the cache
All steps The first user in a region pays for the trip to the origin; everyone after them is served from the edge.
- The first user in the region asks the edge for a file.
- The edge does not have it yet, so it fetches it from your origin.
- The origin returns the file with headers saying how long it may be cached.
- The edge stores a copy and serves it.
- The next user asks for the same file.
- This time the edge answers from its cache without contacting the origin.
Diagram as text
- The first user in the region asks the edge for a file.
- The edge does not have it yet, so it fetches it from your origin.
- The origin returns the file with headers saying how long it may be cached.
- The edge stores a copy and serves it.
- The next user asks for the same file.
- This time the edge answers from its cache without contacting the origin.
| Pull CDN | Push CDN | |
|---|---|---|
| How content arrives | Fetched from the origin on the first request | You upload it to the CDN in advance |
| Effort | Minimal: point the CDN at your site | You manage uploads and updates |
| First request | Slower (a miss) | Fast (already there) |
| Best for | Websites and APIs with many files that change often | Large files that change rarely: videos, installers, game downloads |
Controlling the cache
The origin decides what the CDN may cache, and for how long, with HTTP headers:
Cache-Control: public, max-age=31536000, immutablefor files with a content hash in the name.Cache-Control: public, s-maxage=60to let shared caches (CDNs) keep a page for a minute.Cache-Control: privateorno-storefor anything personal: account pages, carts, API responses with user data.
The cache key decides which requests share a cached copy. By default it is usually the URL. Adding needless parts (tracking query strings) splits the cache and lowers the hit rate; leaving out parts that change the response (language, device type) serves the wrong content.
Updating content
Two ways to make users see a new version:
- Fingerprinting (best): build tools name files by their content, such as
app.3f9a1c.js. A new version gets a new name, the HTML points to it, and the old file can stay cached forever. Nothing needs purging. - Purging: ask the CDN to drop a URL from every edge. Useful for content whose URL cannot change (a product image, an HTML page), but it takes time to spread, and purging everything at once sends all traffic back to your origin.
Common mistakes
- Caching personalised pages at the CDN.
- Using the same file name for every release and then fighting stale caches.
- Including random query parameters in the cache key and getting a near-zero hit rate.
- Purging everything on each deploy, sending all traffic back to the origin at once.
Interview questions
Try answering out loud before you open each one.
Pull or push CDN for a photo-sharing app?
Pull. There are millions of photos, most rarely viewed; a pull CDN caches the popular ones automatically near the users who request them. Push suits a small set of large, known files such as a game download.
How do you deploy a new JavaScript bundle without users getting a stale one?
Fingerprint file names with a content hash and serve them with a long max-age. The HTML (cached briefly or not at all) references the new name, so browsers and CDNs fetch it as a new file. No purge needed.
Check yourself
Related topics
Further reading
- What is a CDN? Cloudflare Learning Center
- HTTP caching MDN Web Docs
Your notes
Saved in this browser as you type.
Finished this topic?
Mark it read to track your progress, then carry on with the next one.