Copies of your files kept in many cities, so users get them from a nearby server.
A CDN (Content Delivery Network) is a group of servers spread across many cities. They keep copies of your files, so each user gets them from a server close to them.
Data travels fast, but not instantly. A user in Mumbai who loads an image from a server in New York waits for the signal to cross the world and come back. A CDN fixes this by putting a copy of the image in Mumbai.
Some words you will see:
Your origin sends HTTP headers with every file. The CDN reads them. The most useful one is Cache-Control.
| Header / value | What it tells the cache |
|---|---|
max-age=3600 | Keep this file for 3600 seconds (1 hour). Browsers and CDNs can both use it. |
s-maxage=86400 | Like max-age, but only for shared caches such as a CDN. The browser can use a different time. |
no-cache | You may store it, but ask the origin if it is still fresh before using it. |
no-store | Do not store it at all. Use for private data like a bank page. |
private | Only the user's browser may keep it. A CDN must not. |
stale-while-revalidate=60 | After it expires, serve the old copy for up to 60 more seconds while fetching a new one in the background. |
ETag | A label for the file version. The cache asks “do you still have version X?” and gets a tiny 304 Not Modified if yes. |
HTTP/1.1 200 OK Content-Type: image/png Cache-Control: public, max-age=300, s-maxage=86400 ETag: "v42-a1b2c3"
A user needs to reach the nearest edge. CDNs do this with DNS (the answer depends on where you are) or with Anycast, where many edges share the same IP address and the network sends you to the closest one. Read more in Anycast Routing and DNS.
All times are illustrative. Real times depend on distance, the network and the load.
/img?size=small and /img?size=large can be two different entries. Random query strings (like tracking ids) can make every request a miss, so ignore them in the key.| Term | Meaning in easy words |
|---|---|
| Origin | Your own server that holds the original files |
| Edge server | A CDN server near the user that keeps copies |
| PoP (Point of Presence) | A CDN location, a data centre in one city with many edge servers |
| Cache hit / miss | Hit: the edge had the file. Miss: it had to ask the origin |
| Hit ratio | Hits divided by all requests. Higher is better |
| TTL (Time To Live) | How long a copy is allowed to be used before it expires |
| Purge / invalidate | Tell the CDN to delete or ignore a stored copy now |
| Cache busting | Change the file name (or add a version) so the old copy is not used |
| Cache-Control | A header that tells caches how long and where they may keep a file |
| ETag / 304 | A version label, and a short “not changed” reply that saves bandwidth |
| Origin shield | One middle cache layer so many edges ask it instead of all asking the origin |
| Cache key | What the CDN uses to decide two requests want the same file (often the URL) |
| Choice | We gain | We lose |
|---|---|---|
| Long TTL | Higher hit ratio, faster, cheaper | Changes show up late |
| Short TTL | Fresher content | More misses, more origin load |
| Pull CDN | Simple to run | A slow first request in each region |
| Push CDN | No cold first request, full control | You manage uploads and cleanup |
| Cache dynamic pages | Big speed and load win | Risk of showing the wrong user's data |
private or no-store.