What Is a CDN? Content Delivery Networks, Explained
In Mumbai, you open a shop whose servers are in the United States, yet its photos appear at once, while a rarely viewed page takes a beat longer. This lesson answers what is a CDN: where its servers sit, when they answer from a saved copy, how long copies live and how to replace an old one.
What a CDN is: copies kept close to users
A CDN, or content delivery network, is a fleet of caching servers in many cities, run by a company such as Cloudflare, Akamai, Fastly or Amazon CloudFront. Each site in the fleet is an edge location, also called a point of presence (PoP), and its machines are edge servers.
The origin is your own server, or a cloud file store, holding the original files. Each edge server is a caching reverse proxy in front of it (lesson 16): users talk to the edge, which answers from its cache, its store of saved copies, and asks the origin only when it must.
The point is distance. A round trip from Mumbai to Virginia takes about 200 ms, for example, however fast the server is, and a new HTTPS connection needs two round trips first (lesson 10). A round trip to an edge in your own city can take 10 ms or less.
Two common ways lead you to a nearby edge. With DNS (lesson 3), your site's name is a CNAME for a CDN name, whose DNS servers return the address of an edge near the resolver that asked; CloudFront works this way. With anycast, every edge location announces the same IP address, and routers (lesson 12) deliver your packets to the nearest by network path; Cloudflare works this way.
In the map, notice that users' trips are short and only dashed lines cross oceans: an edge contacts the origin only for files it lacks.
Cache hit and cache miss
When a request arrives, the edge looks for a fresh copy, one still within its allowed lifetime. Finding one is a cache hit: the edge answers at once, and the origin never hears of it. Not finding one is a cache miss: the edge fetches the file from the origin, stores a copy and sends it on.
In the flowchart, notice the dashed arrow: a miss is paid once, then turns the next requests for that file into hits. Each edge has its own copies, though, so a hit in Mumbai does nothing for London's first visitor.
The timeline puts illustrative numbers on it. The first visitor waits about 230 ms, mostly for the edge's round trip to Virginia; a neighbour a minute later waits about 30 ms. Even the miss beats having no CDN, because the handshakes happen with the nearby edge, which usually keeps a connection to the origin open.
What is cached, and for how long
A CDN is best at static files, the same for every visitor: images, CSS, JavaScript, fonts and video. Many CDNs cache these by default; Cloudflare, for example, picks by file extension, such as .jpg, .css and .mp4, and skips HTML unless you add a rule.
The origin says how long a copy may be kept with the Cache-Control response header (lesson 6). That lifetime is the copy's TTL, time to live, as with DNS in lesson 3. Common directives:
max-age=86400: fresh for 86,400 seconds, one day, in browsers and CDNs alike.s-maxage=600: overrides max-age for shared caches, which serve many users, such as a CDN.private: only the user's own browser may store it, never a CDN; right for personal pages.no-store: no cache may keep it. Despite its name,no-cacheallows storing but makes the cache check with the origin before every reuse.
Caches also send an Age header: how many seconds the copy has existed since it came from the origin. A copy is fresh while its age is below its max-age. With no Cache-Control, the CDN uses a default TTL from its own settings.
A stale copy need not be downloaded again: the edge can ask the origin whether it changed, called revalidation, and a short 304 Not Modified reply renews it.
More than a cache: spikes, TLS, attacks and compression
Standing in front of your origin, edges do other jobs too:
- Traffic spikes: when a sale sends a million people to the same images, the edges serve them and the origin sees only misses.
- TLS: the edge holds a certificate for your domain (lesson 18) and completes the HTTPS handshake (lesson 8) near the user.
- Attacks: a DDoS (distributed denial-of-service) attack floods a site from many machines at once. Spread over a large CDN's hundreds of locations, most of it never reaches your origin. Most CDNs also offer a WAF, the filter from lesson 15.
- Compression: the edge shrinks text files with gzip or Brotli for browsers whose
Accept-Encodingrequest header says they can unpack them.
Attackers who learn the origin's address can still bypass the CDN, so let the origin's firewall or security group (lesson 13) admit only the CDN's published address ranges.
Gotchas: stale content, purges and versioned names
The price of copies is stale content: copies that no longer match the origin. You fix a typo in style.css and deploy, but edges serve the old file until its TTL runs out, and browsers keep theirs. With max-age=86400, some visitors see old styles for a day.
One fix is a purge: you ask the CDN to delete its copies of a URL, a path pattern or everything, so the next request misses and fetches the new file. CloudFront calls this an invalidation. It reaches the edges within seconds to minutes, never browsers, and purging everything sends a burst of misses to the origin.
The sturdier fix is versioned file names: a short hash of the contents in the name, such as style.3f9a2c.css, which most build tools can add. A changed file gets a new name that no cache holds, and old copies are never asked for again.
So versioned files can be cached for a year, while HTML, which names them, is checked with the origin each time. Here is a sketch for an nginx origin serving files from disk; the longest matching location prefix wins, as in lesson 16:
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location / {
add_header Cache-Control "no-cache";
}
31,536,000 seconds is 365 days, and immutable tells browsers the file will never change, so they skip rechecking it on reload. Everything else, including HTML, gets no-cache, so a deploy shows on the next page load. The classic mistake is a long max-age on a file whose name never changes.
Check yourself
A photo was viewed thousands of times in Mumbai this morning. A first visitor in São Paulo now opens it. Hit or miss?
Most likely a miss: each edge keeps its own copies, and São Paulo's has not fetched it yet. Its next visitors will hit.
You replace logo.png, served with max-age=604800, and purge the CDN. Some users still see the old logo. Why?
Their browsers saved the old copy for up to a week, and a purge clears only the CDN. A versioned name such as logo.8c1e4f.png would be a new URL no cache holds.
A signed-in user's account page is sent with Cache-Control: public, max-age=300. What could go wrong?
A CDN may store it and show one person's account page to other visitors for up to five minutes. Personal pages need private or no-store.