Networking and the web
What is a CDN?
Definition
A CDN is a distributed network of servers that caches a site's content close to its users, so pages load faster and the origin serves less traffic.
Also known as: Content Delivery Network, Content Distribution Network
CDN in more detail
Distance costs milliseconds that no amount of server tuning can recover: a round trip across an ocean is bounded by physics. A CDN puts points of presence in many cities, and DNS or anycast routing sends each visitor to a nearby one. That edge server holds a cached copy of your images, scripts, stylesheets and increasingly your HTML, so the response travels a few hundred kilometres instead of ten thousand.
What is cached and for how long is controlled by response headers from your origin — mainly Cache-Control, plus ETag and Last-Modified for revalidation. The usual pattern is to give versioned asset filenames a very long max-age with immutable, because a new deploy produces a new filename, while HTML gets a short TTL or stale-while-revalidate so content updates quickly without every request reaching the origin.
Modern CDNs are more than caches. They terminate TLS at the edge, absorb volumetric DDoS traffic, apply web application firewall rules, compress and reformat images, and run small pieces of code at the edge for routing, authentication and personalisation. The operational catch is invalidation: once a URL is cached in hundreds of locations, fixing a mistake means an explicit purge, which is why content-hashed filenames are the more reliable habit.
CDN example
Without a CDN
Sydney user -> origin in Virginia ~200 ms per round trip
With a CDN
Sydney user -> Sydney edge (cache HIT) ~10 ms
cache MISS -> edge fetches from origin once, then serves
every later Sydney visitor from the copy
Cache headers that make it work
# hashed asset, safe to keep forever
Cache-Control: public, max-age=31536000, immutable
# HTML, fresh but never blocking
Cache-Control: public, max-age=60, stale-while-revalidate=600
x-cache: HIT <- the response came from the edgeCDN — common questions
- Do I need a CDN for a small website?
- If your audience is local to your server, the latency gain is small. A CDN is still worth it for free TLS, DDoS absorption and offloading bursts of traffic, and most hosting platforms now include one by default.
- Can a CDN cache dynamic or personalised pages?
- Parts of them. Responses that vary per user must either be marked private or split so the shell is cached and the personal fragment is fetched separately. Getting Vary and Cache-Control wrong here is how one user's page gets served to another.
- What is the difference between a CDN and a reverse proxy?
- A CDN is a globally distributed network of reverse proxies. The mechanism is the same — sit in front of the origin and answer on its behalf — but a CDN adds many locations, anycast routing and edge features you would otherwise build yourself.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary