All articles
165 articles · updated weekly See our Tools
All articles
Tutorials

What Is a CDN and How Does It Work? A Guide with Examples

A CDN (content delivery network) keeps copies of your site on servers near the user. How its cache works, real headers, purge, and when it doesn't help.

What Is a CDN and How Does It Work? A Guide with Examples
COVER · Tutorials

Your site loads in 200 ms here in São Paulo. You demo it to the boss, everyone claps, you ship on Friday. Monday morning an email lands from a customer in Lisbon complaining the page takes "about three seconds to open." You run the test again, 200 ms, and stare at the screen like it owes you money. The problem isn't your code: it's the physical distance between your server and your user, and no JavaScript micro-optimization fixes that. A CDN does.

This post explains what a CDN is, how it works inside (cache, hit, miss, purge), which ones exist, where it genuinely helps, and where it does nothing at all. The measurements and headers below were collected on November 9, 2026, from an Alpine container on a connection in São Paulo.

What a CDN is

CDN stands for Content Delivery Network. It's a set of servers spread around the world, called edge servers or points of presence (PoPs), that hold copies of your site's content close to where your users are.

Without a CDN, every visitor fetches content straight from your origin server, that one machine in a data center somewhere specific. If the origin is in Virginia and the visitor is in Tokyo, every file crosses half the planet. With a CDN, the Tokyo visitor gets the file from an edge in Tokyo, and the Lisbon visitor from an edge in Europe. The origin is only consulted when the edge doesn't have that copy yet.

One thing that confuses a lot of people: a CDN doesn't replace hosting. The site still needs an origin, whether a server, an object storage bucket or a platform. The CDN sits in front of it. The apparent exception is platforms like Cloudflare Pages, Vercel and Netlify, which sell hosting and CDN as one package: the origin exists, you just don't manage it.

How a CDN works: the user lands on the nearest edge

To get the visitor to the right edge, a CDN uses DNS or anycast. With DNS, the domain points (usually via CNAME) to a CDN hostname, and the CDN's DNS server answers with the IP of an edge close to whoever asked. With anycast, the same IP is announced from every PoP and internet routing carries the packet to the nearest one. Cloudflare uses anycast; you can see which PoP you hit:

$ curl -s https://www.cloudflare.com/cdn-cgi/trace | grep colo
colo=GRU

GRU is the airport code for Guarulhos: the São Paulo PoP. How a DNS query turns into an IP is covered in how a DNS lookup works.

Why it speeds things up: latency is physics

Light in fiber travels at roughly two-thirds of its speed in a vacuum, and submarine cables don't run in straight lines. The numbers show it. In WonderNetwork's public measurements on November 9, 2026, average ping between servers in São Paulo and Lisbon was around 226 ms, and between São Paulo and London, 193 ms. From my container, ping to Cloudflare's São Paulo PoP averaged 23 ms.

Now multiply. A new HTTPS connection spends one RTT on the TCP handshake and another on TLS 1.3 before the request goes out, plus one more for the response to come back. At 226 ms per RTT, that's nearly 700 ms before the first byte of HTML, not counting DNS. If the page pulls CSS, JS and fonts from other domains, each new connection pays the toll again. With the edge 23 ms away, the same stretch drops below 100 ms. Those round trips step by step are in what happens when you type a URL.

A CDN attacks the problem on three fronts: it shortens each RTT, it terminates TLS at the edge (the handshake happens near the user, and the edge-to-origin connection stays open and gets reused), and, when it has the copy, it skips the trip to the origin entirely. That's why it moves TTFB and LCP, the Core Web Vitals metrics most sensitive to the network.

Cache hit, cache miss and origin pull

When the edge gets a request, either it has a valid copy (cache hit, answers immediately) or it doesn't (cache miss: it does an origin pull, fetches from the origin, stores a copy and delivers it). CDNs report what happened in a header. On Cloudflare it's cf-cache-status, and the documentation lists more values than hit and miss:

Value Meaning
HIT Served from the edge cache
MISS Cacheable but not in cache; served from the origin
EXPIRED In cache but expired; fetched again from the origin
REVALIDATED The origin confirmed (conditional request) that the copy is still valid
STALE / UPDATING Expired copy served while the origin is unreachable / being refreshed in the background
BYPASS Cacheable, but the origin response forbade it (no-store, private, Set-Cookie)
DYNAMIC Not eligible for cache; went straight to the origin with no cache lookup

What you'll see most on HTML is DYNAMIC. Cloudflare doesn't cache HTML or JSON by default: it caches by file extension (images, CSS, JS, fonts...), and the page itself goes to the origin on every request unless you create a Cache Rule. Quick Tools itself, which runs on Cloudflare, shows it:

$ curl -sI https://quickeasy.tools/pt/tools | grep -iE 'cf-cache-status|content-type'
content-type: text/html; charset=utf-8
cf-cache-status: DYNAMIC

$ curl -sI https://developers.cloudflare.com/ | grep -iE 'cf-cache-status|content-type'
content-type: text/html
cf-cache-status: HIT

The second one is HTML served from cache: Cloudflare's docs site has a rule configured for it. Your hit ratio is the number that tells you whether your CDN is working or just passing messages along.

What to cache (and what to handle with care)

Static assets are the easy case: images, CSS, JS, fonts. They don't change between users, so they can sit on the edge and in the browser for a long time, as long as the file name changes when the content changes (app.4f2a9c.js) and the header says so:

Cache-Control: public, max-age=31536000, immutable

Public HTML, like a blog's or a store's homepage, can be cached at the edge with a short TTL, but you have to switch that on (on Cloudflare, a Cache Rule) and make sure the page doesn't vary per user. Anything personalized (cart, logged-in dashboard) can't go into the shared cache, or user A sees user B's data: Cache-Control: private or no-store.

The details of max-age, no-cache vs no-store, ETag and 304 Not Modified belong to the browser cache and are in Browser cache: Cache-Control and ETag. Here the topic is the shared cache at the edge, which obeys the same headers (plus s-maxage, which applies only to shared caches) and the CDN's own rules.

Purge: invalidating the CDN cache

When a cached file changes and its URL doesn't, you need a purge: the command that tells the edges to drop the copy. On the big CDNs this is fast. Cloudflare published in 2024 a global median (P50) under 150 ms for purge by tag, hostname and prefix (234 ms for single-file purge), and every plan now has access to every purge method. Fastly advertises a 150 ms mean global purge since 2011.

So why do I still prefer versioned URLs? Because a purge clears the edge, not the rest of the path. A copy already in the user's browser cache, or in a corporate proxy along the way, stays there until its max-age runs out, and no purge reaches it. With a hash in the file name, a change becomes a new URL and the invalidation problem simply doesn't exist. Purge is for what you can't version: HTML and fixed-URL files.

CDN examples (and free options)

  • Cloudflare: has a free plan with CDN, TLS and basic DDoS protection, and a PoP in São Paulo (the GRU above). It works as a reverse proxy: you switch the domain's nameservers to theirs.
  • Amazon CloudFront, Fastly, Akamai: paid CDNs common in large companies; CloudFront is the natural path if you already use S3 or other AWS services.
  • Bunny.net: usage-based pricing, popular with smaller projects.
  • CDN built into the platform: Vercel, Netlify and Cloudflare Pages already serve the site from their own edge network, no extra setup.

Prices and limits change often; check each provider's page before deciding.

A CDN is more than speed

Since all traffic passes through the edge before reaching the origin, the CDN also becomes a defense layer: it absorbs volumetric DDoS attacks, applies WAF rules against injection and bots, manages the TLS certificate (issuance and renewal; more in is a free SSL certificate safe?) and, on many of them, optimizes images on the fly. It doesn't replace a load balancer between your servers, though many offer origin load balancing as a separate product. And it always sits in front of the origin, whether that's on a public, private or hybrid cloud.

When a CDN doesn't help

If content is personalized per user and can't be cached, the edge has nothing to serve: it becomes a proxy forwarding everything to the origin. An API returning unique data per request, a dashboard heavy on queries, a feed computed in real time: none of that gets faster just by having a CDN in front. There's still some gain (TLS terminated near the user, a warm connection to the origin), but the real bottleneck is the database or the query, and that's where you need to work.

Frequently asked questions

Does a CDN replace hosting?

No. The CDN stores copies and delivers them from nearby, but the source of truth is the origin, which still needs hosting. Platforms like Vercel, Netlify and Cloudflare Pages bundle both into one product, which makes it look like the CDN "hosts" the site.

Is there a free CDN?

Yes. Cloudflare's free plan is the most widely used, and static-site hosting platforms include a CDN at no extra cost. For heavy traffic or advanced features (sophisticated cache rules, full WAF), you move to paid plans.

What is a cache hit and a cache miss?

A cache hit is when the edge already has the copy and responds without consulting the origin. A cache miss is when it doesn't and must fetch it from the origin, store it and then deliver it. The higher the hit ratio, the faster for users and the lighter the load on your server.

Why does my HTML show DYNAMIC on Cloudflare?

Because Cloudflare doesn't cache HTML by default: it caches by file extension, and HTML isn't on the list. To cache pages, create a Cache Rule and make sure they don't vary per user (no logged-in content or Set-Cookie in the response).

A CDN fixes distance, not a slow origin

A CDN speeds things up because it solves a physics problem: it puts content 20 ms from the user instead of 200, terminates TLS near them and skips trips to the origin. For assets, version the URLs, mark them immutable and let the cache work. For HTML, know that on Cloudflare it's DYNAMIC until you add a rule, and use purge for what you can't version, remembering it doesn't clear anyone's browser. And don't expect miracles where the bottleneck is the database.

The editorial content ends here. When debugging CDN caching you run into 304 Not Modified, a 200 from the origin, a 403 from the WAF and 5xx from the proxy; the Quick Tools HTTP status codes reference explains each status in seconds.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile