A CDN (content delivery network) is a group of servers in many locations that store copies of a website's files and serve each visitor from a nearby copy instead of from the site's own server. The site's server, called the origin, is only contacted when the CDN does not have a fresh copy of the file being asked for.
You can tell whether a file came from a CDN cache by looking at its response headers: curl -sI https://example.com/app.js and look for headers such as Age, X-Cache or CF-Cache-Status.
How a CDN works, step by step
- DNS points at the CDN. The site's domain (or a subdomain like
static.example.com) resolves to the CDN's servers instead of to the origin. Visitors connect to a CDN server near them, usually called an edge server or point of presence. - The edge checks its cache. If it has a stored copy of the requested URL that is still fresh, it answers immediately. That is a cache hit.
- On a miss, the edge asks the origin. It fetches the file, sends it to the visitor, and stores a copy if the response is allowed to be cached.
- The next visitor in that region gets the cached copy until it expires or someone purges it.
The time saved comes from distance. A visitor in Sydney fetching a file from an origin in Frankfurt pays for every round trip across that distance, and a new HTTPS connection spends round trips on the TCP and TLS handshakes before the first byte of the file arrives. If an edge server in Sydney already has the file, those round trips are short. If you want the theory behind why distance matters more than raw speed here, see latency vs bandwidth.
What a CDN response looks like
Here is a real request for a file on the jsDelivr CDN (example output, trimmed, edge location names replaced):
curl -sI https://cdn.jsdelivr.net/npm/[email protected]/package.json
HTTP/1.1 200 OK
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
Age: 876331
X-Served-By: cache-ams-1, cache-ams-2
X-Cache: HIT, MISS
Reading it line by line:
Cache-Control: public, max-age=31536000, s-maxage=31536000tells every cache it may store the file for 31,536,000 seconds (365 days). The URL contains a fixed version number, so the content never changes and a long lifetime is safe.Age: 876331is the cache's estimate of how long ago the response was generated by the origin, in seconds. 876,331 s is roughly 10 days.X-Cache: HIT, MISSshows two cache layers: one had the file (HIT), the other did not. Header names for this vary by provider; Cloudflare usesCF-Cache-Status.
Which files a CDN caches
A CDN is a shared cache. RFC 9111 defines that as "a cache that stores responses for reuse by more than one user", as opposed to your browser's private cache. Because of that, CDNs are careful about what they store. Two things decide it:
- The origin's headers.
Cache-Control: s-maxage=Nsets how long shared caches keep a response and overridesmax-agefor them.privatemeans only the browser may store it, andno-storemeans nobody may. - The CDN's own defaults and rules. These differ by provider. Cloudflare, for example, caches by file extension and, in its own words, "does not cache HTML or JSON by default". Its default list covers static types such as CSS, JS, JPG, PNG, SVG, WOFF2, PDF and ZIP. A zone owner can change that with cache rules.
| Header on the response | What a CDN does |
|---|---|
Cache-Control: public, s-maxage=86400 | Stores it and serves it for 86,400 s (1 day) |
Cache-Control: max-age=600 | Stores it for 600 s if the CDN caches this file type |
Cache-Control: no-cache | May store it, but must check with the origin before every reuse |
Cache-Control: private | Does not store it; only the visitor's browser may |
Cache-Control: no-store | Does not store it anywhere |
no-cache is the one people misread. It does not mean "don't cache"; it means "revalidate before use". If you mean "never keep a copy", use no-store.
Reading CF-Cache-Status on Cloudflare
Cloudflare documents these values for its CF-Cache-Status header:
| Value | Meaning |
|---|---|
HIT | Served from Cloudflare's cache |
MISS | Cacheable, but not in cache yet, so fetched from the origin |
EXPIRED | Was in cache but expired, so fetched from the origin again |
REVALIDATED | Origin confirmed the cached copy is unchanged |
UPDATING | Expired copy served while the origin refreshes it in the background |
STALE | Expired copy served because the origin could not be reached |
BYPASS | The origin's response was not cacheable, so it was not stored |
DYNAMIC | Not eligible for caching at all; went straight to the origin |
If every request for an image shows MISS or DYNAMIC, the CDN is only acting as a pass-through and you are paying latency without getting caching. Check the origin's Cache-Control header first.
Why sites use a CDN
- Lower latency for visitors far from the origin, because repeat files come from a nearby edge.
- Less load and bandwidth on the origin. A file cached at the edge is fetched from the origin about once per edge location instead of once per visitor.
- Traffic spikes. MDN's definition notes that CDNs make service "less affected by high traffic", since the edges absorb repeat requests.
- TLS close to the user. The HTTPS handshake happens with the nearby edge, which shortens connection setup even for responses that are not cached.
The catch: stale content after a deploy
The same caching that makes a CDN fast will keep serving an old file after you change it, until the cached copy expires. There are two ways to deal with it:
- Versioned URLs. Put a hash or version in the file name (
app.3f9a1c.js) or query string (styles.css?v=12). A new deploy produces new URLs, so nothing old is ever served for them, and you can safely set a longmax-age. - Purging. Tell the CDN to drop cached copies. Cloudflare, for example, supports purging by single URL, by hostname, by prefix, by cache tag, or everything, on all plans, with rate limits that differ by plan. Purging a single URL is what Cloudflare recommends.
HTML pages usually cannot be versioned, because their URLs are what people link to. Give them a short cache lifetime, or purge them as part of the deploy.
CDN vs reverse proxy vs host
A CDN edge server is technically a caching reverse proxy: visitors talk to it as if it were the website, and it forwards requests to the origin when it needs to. The difference is scale and placement. A reverse proxy is usually one server (or a small cluster) in front of your app in the same data centre; a CDN is the same idea spread across many cities and operated by a provider. Neither replaces the host. The origin still has to exist and still serves every request the CDN cannot answer from cache.
If the CDN seems not to work
Check that the domain actually resolves to the CDN (nslookup static.example.com should return the provider's addresses, not your origin's IP). Then request the same file twice with curl -sI and compare: a working cache shows a miss first and a hit second, with the Age header appearing or growing. If the second request is still a miss, the origin is probably sending private or no-store on that file, or the file type is not in the CDN's default cache list. If DNS is the problem, how DNS works covers resolution and caching of the records themselves.
Frequently asked questions
- Is a CDN the same as web hosting?
- No. Your web host runs the origin server that holds the real copy of your site. A CDN sits in front of it, keeps cached copies of files on servers in many locations, and answers visitors from the nearest copy. When a CDN does not have a file cached, it fetches it from your origin.
- Does a CDN make every page faster?
- It speeds up whatever it can serve from cache, which is mostly static files such as images, CSS, JavaScript and fonts. Personalised or uncacheable responses still travel to the origin, so the CDN mainly saves the connection setup on those. Some CDNs, Cloudflare for example, do not cache HTML by default at all.
- Why does my site still show the old version after I deployed?
- The CDN is still serving a cached copy that has not expired yet. How long it keeps it depends on the Cache-Control header (s-maxage or max-age) or on the CDN's own cache rules. Purge the changed URLs in the CDN dashboard or API, or use versioned file names so a new deploy produces new URLs.
- Is it safe to put personal pages behind a CDN?
- Only if those responses are marked as not shareable. A CDN is a shared cache: one stored response can be handed to many users. Send Cache-Control: private or no-store on anything personalised, such as account pages, so the CDN never stores it.