Software Engineeringunpacked

What is a CDN? How a content delivery network works, from edge servers to cache hits

A CDN (content delivery network) is a group of servers spread across many locations that keep copies of your website's files close to your visitors. When someone asks for a file, a nearby edge server answers. If it already holds a fresh copy, that is a cache hit and the file comes back at once. If not, it is a cache miss: the edge fetches the file from your own server (the origin), sends it on, and keeps a copy for the next visitor. The result is shorter trips for your users and less work for your server.

Video · What is a CDN? How content delivery networks work in under a minute · 0:40

What is a CDN, in one paragraph

Prefer to watch? The one-minute Short shows the core idea with diagrams; this guide goes further.

A CDN, short for content delivery network, is a set of servers in many locations that hold copies of your files so each visitor can be served from somewhere close to them. MDN puts it simply: the servers store duplicate copies of data, and each request is served by the server closest to the user (MDN).

Why does that matter? Distance costs time. Every request has to cross the network to your server and back, and a visitor on another continent pays for that trip on every image, script and stylesheet. A CDN shortens the trip by putting a copy of the file near the visitor.

Picture a bakery that bakes in one town but stocks corner shops everywhere. You buy your bread down the street instead of driving to the bakery. The bakery is your origin server, the corner shops are the CDN's edge servers, and the bread is your files.

How does a CDN work, step by step

Here is the path of one request through a CDN, based on how Amazon CloudFront documents it (AWS docs). Other CDNs follow the same idea.

  1. A visitor's browser asks for a file, such as /images/logo.png.
  2. The request is routed to a nearby edge location (CloudFront uses DNS to pick the one that can best serve it, typically the nearest in latency).
  3. The edge checks its cache. If it holds the file, it returns it right away.

  4. If not, the edge forwards the request to your origin, for example a web server or a storage bucket.

  5. The origin sends the file back to the edge, which starts passing it to the visitor as soon as the first bytes arrive, and adds it to its cache for the next request.

Getting a visitor to the right edge relies on the basics of how the internet works: DNS answers with an address, and routing carries the packets there. Some CDNs use DNS to hand out the address of a nearby location; others use anycast, where the same address is announced from many places and the network delivers each request to one of them (RFC 4786).

Cache hit vs cache miss

Every request to an edge ends in one of two ways, and the ratio between them decides how much a CDN helps you.

Cache hit Cache miss
The edge has a fresh copy? yes no
Who answers the edge, alone the edge, after asking the origin
Speed for the visitor fast: one short trip slower: the long trip to the origin too
Load on your origin none one request
What happens next nothing the edge stores the copy for later

A file that many people request, like your logo or your main script, is a hit almost every time. A file that almost nobody asks for may be evicted from a busy edge before the next request comes, and will often miss. Larger CDNs add a middle layer for this: CloudFront has regional edge caches between its edge locations and your origin, with bigger caches, so less popular files still avoid the long trip (AWS docs).

How a CDN knows what to cache, and for how long

A CDN is a shared cache, and it follows the same HTTP caching rules as the cache in your browser (MDN, HTTP caching). Your origin controls it with the Cache-Control header on each response:

When a stale copy is checked, the origin can answer 304 Not Modified if nothing changed. That reply has no body, so it is cheap, and the edge keeps serving its copy.

CDNs also have defaults for responses with no clear instructions. Cloudflare, for example, caches by file extension (images, fonts, scripts, styles and more), does not cache HTML or JSON by default, and skips responses that set cookies or say private or no-store (Cloudflare docs). Check your provider's defaults before you assume a page is or is not cached.

Updating files: purging and versioned URLs

The awkward side of caching is change. If you publish a new app.js but edges still hold the old one with a long max-age, some visitors get the old version until it expires.

There are two fixes:

  1. Purge (also called invalidate). Most CDNs let you remove a file from every edge through an API or dashboard, so the next request goes to the origin. MDN notes this is the one way to delete responses from a managed cache like a CDN.

  2. Versioned URLs. Put a version or a hash of the content in the file name, such as app.3f9a1c.js. A new version gets a new URL, so the old copy simply stops being requested. Because each URL never changes, it can be cached for a year with max-age=31536000, immutable.

Most build tools produce hashed file names for you. The usual setup is long caching for those hashed assets and no-cache for the HTML page that points to them, since the page URL itself cannot change.

What a CDN is good for, and what it is not

A CDN helps most with static files that many visitors share: images, video, fonts, stylesheets, JavaScript bundles and downloads. Whole static sites can be served from one. You get shorter trips for visitors far from your origin, far fewer requests reaching your origin, and more capacity when traffic spikes, because the edges absorb repeated requests.

Many CDNs do more than cache. They often terminate TLS close to the visitor, compress responses, and filter abusive traffic before it reaches you. Those features vary by provider, so read the docs of the one you pick.

A CDN does not help much with responses that differ for every user, like an inbox or a shopping cart, because those must not sit in a shared cache. It also does not speed up long lived connections such as WebSockets, which are a live line to your server rather than a file to copy. And it adds a dependency: MDN points out that if a third party CDN goes down or is blocked, your site can break with it.

CDN vs load balancer vs origin server

These three often appear in the same diagram, so it helps to separate their jobs.

What it does Where it sits
Origin server holds the real files and runs your code your hosting or cloud account
CDN keeps copies near visitors and answers repeated requests many locations, in front of the origin
Load balancer spreads requests across several origin servers in front of your servers, behind the CDN

A CDN reduces how many requests reach you. A load balancer decides which of your servers handles the ones that do. Large sites use both.

How to check whether a page came from a CDN

Open your browser's developer tools, go to the Network tab, reload, and click a request. Look at the response headers:

If every request shows a miss, check that your origin sends a max-age and does not set cookies on static files. That is the most common reason a CDN quietly caches nothing. Treat it like any other behavior you rely on: test it after each change to your headers or build. More guides like this one are in the article library.

Key takeaways

  1. A CDN is a network of servers that keep copies of your files near your visitors.
  2. A cache hit is answered by the edge alone; a miss goes to your origin once and is stored for next time.
  3. Your origin controls caching with Cache-Control: max-age for fresh time, private or no-store for anything personal.
  4. Ship changes with versioned file names or a purge.
  5. Use a CDN for shared static files; keep personal pages and live connections off the cache.

Sources: MDN, CDN · MDN, HTTP caching · How CloudFront delivers content · Cloudflare, default cache behavior · RFC 4786, operation of anycast services

FAQ

What is a CDN in simple terms?

A CDN is a network of servers in many places that store copies of your website's files. Each visitor is served by a server near them instead of by your one server far away, so pages load faster and your server handles fewer requests.

How does a CDN work?

Your visitor's request goes to a nearby edge server. If the edge has a fresh copy of the file, it returns it straight away (a cache hit). If not (a cache miss), it requests the file from your origin server, forwards it to the visitor and stores a copy for later requests. How long a copy stays fresh is set by the Cache-Control header your origin sends.

Does a CDN replace web hosting?

Usually not. Your site still lives on an origin, such as a web server or a storage bucket, and the CDN sits in front of it and caches what it is allowed to cache. Some static sites can be served entirely from a CDN, but the files still have to come from somewhere first.

What is the difference between a CDN and a cache?

A cache is any store of copies that saves a trip, like the one in your browser. A CDN is a shared cache spread over many locations and run as a service, sitting between all your visitors and your origin. The HTTP caching rules are the same for both.

Can a CDN cache dynamic pages?

Only when you allow it. Personalized responses, such as a logged-in user's account page, should be marked private or no-store so a shared cache never stores them. Many CDNs also skip HTML and API responses by default and cache images, scripts, styles and fonts.