Why you need it
Data moves through cables at about two thirds of the speed of light, and the world is big. A visitor in Sydney loading a site hosted in London waits for every request to cross the planet and back. No faster server fixes that; only a copy that is physically closer does.
Think of it like this
A chain of corner shops stocking the same popular items as the big central warehouse. You get milk from the shop on your street, not by driving to the warehouse.
Where you’ll find it
- Netflix: Puts its own boxes full of films inside internet providers' buildings, called Open Connect, so a show is streamed from a few streets away.
- Cloudflare: Runs data centres in hundreds of cities that keep copies of sites close to their visitors.
- AWS: CloudFront is Amazon's CDN, often put in front of files stored in S3.
What to know first
Origin. Your own server, where the real files live.
Edge. A CDN server near visitors, holding copies.
Round trip. A message there and back. Loading one page takes many of them.
How it works
- Copies near every visitor. A CDN keeps servers in many cities. Visitors are sent to the nearest one, usually by DNS.
- The first visitor fills the shelf. If the nearby edge has no copy, it fetches from your origin, keeps it and answers. Everyone nearby after that gets the copy. The same idea as Caching (cache-aside), but placed by geography.
- Best for things that rarely change. Images, videos, scripts and styles are ideal. Their names often include a version, like
app.3f9a.js, so they can be cached for a year and a new name is used when they change. - It also shields your origin. Most requests stop at the edge, so your servers see a small part of the traffic. Many CDNs also absorb attacks and spikes before they reach you.
- Updates need a plan. Edge copies follow TTLs just like any cache. To change something now, purge it from the CDN or give it a new name. See Invalidation and TTL.
An example
London to Sydney is about 17,000 km of cable. At about 200,000 km a second, one round trip takes roughly 170 ms before any work is done.
A page needing 10 round trips (connection, security, files) spends about 1.7 seconds just travelling.
From a CDN edge in Sydney, about 20 km away, each round trip is a few milliseconds. The same page arrives in well under half a second.
Further reading
- What is a CDN?, Cloudflare Learning Center. A clear introduction with diagrams. Start with this one.
- Primer on latency and bandwidth, Ilya Grigorik, High Performance Browser Networking. Why distance, not bandwidth, makes most of the web feel slow. Free to read.
- Open Connect, Netflix. How the biggest video service puts its films next to its viewers.