Introduction to a CDN
easy 26 minA content delivery network (CDN) is a set of servers in many places that keep copies of your content and answer users from nearby.
Video lesson · Chapter 1 of 5
A photograph crosses an ocean
What a CDN does, in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A content delivery network is a group of caching servers spread over many cities. Each one stands between the users near it and your own server, which is called the origin. If it holds a copy of what the user wants, it answers. If not, it fetches the content from the origin, answers, and usually keeps a copy.
Technically each of these servers is a reverse proxy with a cache. It speaks to the user as if it were your site, ends the encrypted connection, checks its cache, and talks to the origin only when it must.
Why it exists
Distance costs time. Light in glass fibre travels about 200,000 km each second, so a round trip between Sydney and Virginia, about 16,000 km apart, takes at least 160 ms. No faster server can change that. Only a closer one can.
Repetition costs money. If a million people load the same picture, the origin sends the same bytes a million times. With copies at the edge it sends them a few times, and the network takes the rest.
A single location is also fragile. Many locations can share a traffic spike, and one can be taken out of use while the others continue.
A short history
September 1997
RFC 2187 describes how web caches can cooperate with ICP, so that one cache asks its neighbours before it goes to the origin.
February 2003
RFC 3466 describes a model for connecting content delivery networks and defines shared words for them.
December 2006
RFC 4786 documents good practice for anycast services, where one address is announced from many places.
November 2008
Amazon opens CloudFront, a pay-as-you-go CDN.
June 2012
Netflix announces Open Connect, a delivery network of its own for its video.
June 2022
RFC 9111, the current HTTP caching standard, is published. The same month RFC 9213 gives CDNs a Cache-Control header of their own.
Words to know
- Origin
- Your own server or storage, where the content really lives.
- Edge server
- A CDN server near users that caches and answers.
- PoP
- Point of presence: a physical location with edge servers.
- Cache hit
- The edge has a usable copy and answers at once.
- Cache miss
- The edge has no usable copy and must ask the origin.
- TTL
- Time to live: how long a copy may be used.
- Offload
- The share of traffic that never reaches the origin.
How it works
Step through the idea, or press Play.
Ask nearby. The user's request reaches a close edge server
Understand it
Start here
One print shop, kiosks everywhere
Imagine a comic that is printed in one city. Readers live everywhere. They could all write to the print shop and wait. Or kiosks could keep a few copies.
A CDN is the chain of kiosks. A toy shop near your home instead of a factory far away works the same way.
The parts
Origin, edge, PoP
Two users, two edges, one origin
Words you need
Origin
Your server or bucket. The source of truth.
Edge server
A cache near users that speaks for your site.
PoP
A location with edge servers in it.
TTL
How long a copy may be used.
The problem
Life without a CDN
Five problems and what a CDN changes
| Problem | Why it happens | With a CDN |
|---|---|---|
| High latency | Each request travels to one place | A short trip to a close edge |
| Origin overload | All traffic meets the same servers | Most requests end at the edge |
| One point of failure | If the origin is down, everybody sees it | Cached content can still be served |
| Bandwidth cost | The same bytes are sent again and again | Sent to the edge once per copy |
| Uneven experience | Speed depends on the distance | More similar everywhere |
Without a CDN
How it works
A request, step by step
- 1
The browser asks
It wants a picture from a name such as cdn.example.com.
- 2
The name becomes an address
DNS or the network itself leads the browser to a nearby PoP.
- 3
The edge receives it
It finishes the TCP and TLS handshakes close to the user.
- 4
The cache is checked
The edge builds a cache key, usually from host, path and query, and looks it up.
- 5
Serve or fetch
On a hit it sends the copy. On a miss it asks the origin and keeps the answer if it may.
- 6
The response
The user gets the content with headers that tell the browser how long to keep it.
Miss, then hit
The numbers
Where the time goes
Take a user in Sydney and an origin in Virginia. Assume a 200 ms round trip to the origin, 10 ms to a Sydney edge, 20 ms for the name lookup and 30 ms of work at the origin. A new HTTPS connection with TLS 1.3 costs one round trip for TCP, one for TLS and one for the request.
A new connection, small file
The same trip, added up
| Step | Direct | Edge hit |
|---|---|---|
| Name lookup | 20 ms | 20 ms |
| TCP handshake | 220 ms | 30 ms |
| TLS handshake | 420 ms | 40 ms |
| Request and first byte | 650 ms | 52 ms |
Average and origin load
Gains
What you get
Lower latency
Fewer and shorter long trips.
Origin offload
At a 90% hit ratio the origin sees one request in ten.
Scale
A spike is spread over a large network.
Availability
Another PoP can take over, and stale copies can be served.
Attack protection
Floods meet the CDN's capacity first, if the origin cannot be reached around it.
Lower cost
Less origin traffic and fewer servers. Compare it with the CDN bill.
Kinds of content
Static and dynamic
Static: cache it
- Images, CSS, JavaScript
- Fonts
- Video and downloads
- Same bytes for everybody
Dynamic: think first
- API responses and HTML pages
- Real-time data
- Personal pages
- Short lifetime, or no shared cache
Dynamic traffic can still gain. The edge ends the handshakes near the user, keeps warm connections to the origin and may run small programs (edge compute). But the answer itself comes from the origin.
May the CDN keep this?
Kinds of CDN
Pull and push
| Pull | Push | |
|---|---|---|
| Who starts | The first reader | You, before anybody asks |
| First request | Slow: a miss | Fast, if the copy arrived |
| Storage | Only what is asked for | Everything you upload |
| Work for you | Headers | Uploading and removing |
| Good for | Many files, unknown demand | Few large files, known demand |
Control
The headers that matter
Rules for three kinds of files
| Path | Header | Reason |
|---|---|---|
/static/* with a hash in the name | public, max-age=31536000, immutable | The name changes when the content does: 1 year |
/images/* | public, max-age=604800 | 1 week |
/api/* with personal data | no-store | No cache may keep it |
no-store alone is enough. Adding no-cache and must-revalidate to it changes nothing for a cache that follows the standard.CDNs differ in what they cache by default. Cloudflare goes by file extension and leaves HTML and JSON alone unless you add a rule. CloudFront uses 24 hours when no cache policy and no header say otherwise. Read more in Cache TTL and API caching.
Limits
When a CDN helps little
Do
- Send Cache-Control on every response
- Use names with a hash for static files
- Compress text
- Serve public files through the CDN
- Watch the hit ratio
Avoid
- Shared caching of personal pages
- TTLs so short that they never hit
- Changing a file without a plan for the copies
- Caching secrets
- Leaving the origin open to everybody
Do we need one?
Summary
The whole topic in one minute.
A CDN keeps copies of your content in many places and answers from the nearest one. A hit is a short trip. A miss costs the long trip once, and later readers use the copy.
It shortens latency, protects the origin, absorbs spikes and cuts bandwidth. It does not help with content that is different for every person and changes all the time, except by ending connections early and reusing warm ones.
Decide three things per kind of content: may a shared cache keep it, for how long, and how does it change.
- A CDN is many caching reverse proxies near users.
- Hit: short trip. Miss: long trip once.
- Round trips, not bytes, dominate small files.
- Origin load is requests times (1 - hit ratio).
- Shared caches are for content that is the same for many people.