Skip to content
Velloreth
Technology 10 min read

How CDNs Actually Work

Anycast, BGP, cache hierarchies, and cache keys — the actual machinery behind content delivery networks, explained without the marketing layer.

By

A content delivery network is a network of cache servers distributed across many locations, plus the routing system that sends each user to the right one. Everything else — the marketing about “accelerating your digital experience” — is a description of the consequences. Understanding the actual machinery (anycast, BGP, cache keys, tiered caching) is what lets you diagnose why a request was slow, why a deploy did not purge, or why your origin bill is larger than it should be.

The core idea

Without a CDN, a user in Nairobi requesting a file from a server in Virginia pays the full network path: dozens of hops, multiple oceans of fiber, and a round-trip time often north of 200ms. Multiply that by every asset on a page and every page view, and the latency and bandwidth costs compound.

A CDN inserts cache servers between the user and the origin. Popular content is copied to those servers once and served from the nearest one thereafter. The user’s request travels ten kilometers instead of twelve thousand.

The two hard problems a CDN must solve:

  1. Routing: How does the user’s request reach the nearest cache server?
  2. Caching: What is stored, where, for how long, and under what key?

Routing: anycast vs GeoDNS

Anycast

The dominant modern approach, used by Cloudflare, Fastly, and most large networks: the same IP address is announced from hundreds or thousands of locations using BGP (the internet’s routing protocol). When a user sends a packet to that IP, BGP naturally delivers it to whichever announcing location is topologically closest — measured in network hops and routing policy, not always in physical distance.

Consequences of anycast:

  • Failover is fast. To take a location offline, you stop announcing the prefix there. BGP reconverges in seconds to minutes, with no DNS TTL to wait out.
  • No GeoDNS dependency. Traditional CDNs that use GeoDNS route based on where the DNS resolver is, which is a poor proxy for where the user is — especially when the user uses a public resolver like 1.1.1.1 or 8.8.8.8 that is not nearby. Anycast routes the actual traffic, not the lookup.
  • DDoS absorption is a side effect. An attack on one IP is spread across every location announcing it.
  • “Nearest” is routing-nearest. BGP optimizes for policy and path, not latency. In rare cases a user is handed to a location that is topologically close but physically farther. Well-run networks tune this with latency probing and traffic engineering; it is not perfect.

Cloudflare, for example, announces from over 330 cities. Every one of those locations serves the same anycast IP; which one answers depends on where the user’s packets converge.

GeoDNS

The older approach: authoritative DNS servers return a different IP depending on the resolver’s location, sending the user to a nearby PoP. It still exists, often as a complement to anycast, but it inherits the resolver-location problem and adds DNS TTL latency to failover.

Caching: the part you actually control

Routing gets a request to an edge server. Caching determines whether that server can answer without calling your origin.

The request path

  1. Request arrives at the edge PoP.
  2. The CDN builds a cache key — typically the URL, sometimes plus headers, cookies, or query parameters.
  3. If a matching object is in cache and fresh, it is served directly. This is a cache HIT.
  4. If not, the edge fetches from your origin (possibly via a mid-tier or origin shield), stores the result, and serves it. This is a cache MISS.

The difference between a HIT and a MISS for your origin is the entire economics of running a CDN. Origin egress, origin compute, and origin database load all scale with misses.

What gets cached

Your origin tells the CDN what is cacheable through HTTP headers:

HeaderRole
Cache-Control: max-ageHow long a shared cache may serve the object without revalidation
Cache-Control: s-maxageSame, but only for shared (CDN) caches — lets you give the CDN a longer life than the browser
Cache-Control: no-storeDo not cache at all — use for private data
ETag / Last-ModifiedValidators for conditional revalidation on stale objects
VaryWhich request headers produce distinct cached variants (e.g., Vary: Accept-Encoding, Vary: Accept-Language)

The cache key is separate from these headers. A common bug: your application serves different content based on a query parameter or cookie, but the cache key only includes the URL — so one user’s personalized response gets served to everyone. The fix is either to vary the cache key on that input or to stop personalizing cacheable responses.

Purging

When you deploy new assets, you need the old ones gone from hundreds of locations. Providers offer:

  • URL purge — exact-match invalidation, usually free up to a threshold (Cloudflare includes 300 purge requests per five minutes on free plans, for example).
  • Tag purge — label objects at cache time and purge by tag; scales better than URL lists.
  • Zone-wide purge — nuclear option; expect a thundering herd of misses against your origin afterward.

The correct pattern for deploys: content-hashed asset filenames (app.4f3a2b.js) so a new deploy is a new URL, never a purge. Purge HTML pages if you must; never purge your whole static bundle.

Hierarchy: tiered caching and origin shields

With hundreds of PoPs, a naive design has every location fetch misses directly from your origin. That is hundreds of potential origin connections per object and a bad origin load profile.

Tiered caching introduces hierarchy: a smaller set of locations act as upper tiers. A PoP that misses checks its upper tier before going to origin. Cloudflare’s Smart Tiered Cache selects an upper-tier data center near your origin (with a fallback elsewhere) so origin connections are concentrated in a handful of places rather than 330.

Origin shield is the same idea under different branding (CloudFront, Fastly): a designated middle layer between edge and origin that absorbs miss traffic and improves origin cacheability.

If you self-host an origin, enabling tiered cache is one of the highest-leverage configuration changes available. It does not improve your hit ratio at the user-facing edge much; it dramatically reduces the number of times your origin is contacted.

Transport: what sits underneath

Modern CDNs also optimize the transport layer, which matters as much as caching for first-load performance:

  • TLS termination at the edge. The CDN holds the certificate and terminates HTTPS locally; the connection to origin may use a separate TLS session (or even plain HTTP inside a trusted network, though TLS-everywhere is the better default).
  • HTTP/2 and HTTP/3. Multiplexing over a single connection (HTTP/2) and QUIC-based transport (HTTP/3) reduce head-of-line blocking and improve performance on lossy mobile links. Most large CDNs have supported HTTP/3 for years.
  • TCP connection management. The CDN maintains warm connections to origin so that user requests do not each pay a fresh TCP + TLS handshake on the origin hop.

When a CDN does not help

CDNs are not universally beneficial:

  1. Single-country audiences with no local PoP. You get caching and security, but the nearest PoP is still abroad; last-mile latency barely improves.
  2. Fully personalized, uncacheable responses. An API where every response is per-user and no-store gains nothing from caching — only from DDoS protection and TLS offload. Some CDNs will still charge for the bandwidth.
  3. Very small sites with a local audience. If your origin is already 15ms from your users, a CDN adds a hop and a configuration surface for little gain.
  4. Workloads dominated by origin round trips. If the user-facing latency is dominated by database queries, moving the HTTP hop closer saves single-digit milliseconds against a 300ms query. Fix the query first.

Practical header recipes

Theory is fine; these are the configurations that work in practice.

Immutable, content-hashed assets (JavaScript, CSS, images with fingerprinted filenames):

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

The URL changes on every deploy, so the CDN and browsers can cache forever without coordination.

HTML pages (must reflect deploys quickly):

Cache-Control: public, max-age=60, s-maxage=600

Short browser TTL so users pick up new deploys quickly; a longer CDN TTL so your origin sees a fraction of the traffic. Purge HTML on deploy if you need faster propagation.

API responses that are cacheable for a few seconds:

Cache-Control: public, max-age=10, s-maxage=10
Vary: Accept-Encoding

Ten seconds of edge caching on a high-traffic endpoint removes a surprising share of origin load with no user-visible staleness.

Private, never-cacheable data:

Cache-Control: no-store

Use it for authenticated responses and anything containing personal data. If you see a CDN serving this response to a different user, you have a cache-key bug that no-store is now containing.

The immutable directive deserves one note: it tells caches not to revalidate during the max-age window at all. Only use it when the URL actually changes with the content. Putting immutable on a mutable URL is how you get a deploy that does not deploy.

HTTP/3 and QUIC, concretely

Most large CDNs have supported HTTP/3 for years, and the performance argument is specific: HTTP/2 multiplexes many requests over one TCP connection, but a single lost packet stalls every stream on that connection (head-of-line blocking). HTTP/3 runs over QUIC (UDP), which multiplexes streams independently — a lost packet stalls only the stream that carried it.

The practical effect is largest on lossy links: mobile networks, long-haul international paths, congested Wi-Fi. On a clean fiber connection, HTTP/3 and HTTP/2 are hard to tell apart. For a global audience on mixed networks, enabling HTTP/3 at the edge is a free win, and you do not have to change application code — the CDN negotiates it with the client.

Measuring whether it is working

Three numbers tell you almost everything:

  • Cache hit ratio — edge HITs divided by total requests. Target 95%+ for static-heavy sites. If it is low, your cacheability headers or cache key are wrong.
  • Origin offload — the inverse framing: what fraction of traffic your origin never saw. Usually derived from the same data, but useful when negotiating origin capacity.
  • TTFB by geography — time to first byte split by user region. A CDN that is working shows roughly uniform TTFB worldwide; one that is misconfigured shows spikes wherever the nearest PoP is far away.

Cloudflare’s cf-ray header identifies which PoP served a request (for example, ...-SIN for Singapore), which makes it easy to confirm which location a given user hit.

The mental model

Strip away the branding and a CDN is three things: a distributed cache, a routing system that gets users to the cache, and an operations layer for purging and configuration. The providers differ mainly in network size, routing method, and how much compute they let you run next to the cache.

If you understand anycast (or GeoDNS), cache keys, Cache-Control, and tiered caching, you can debug almost any CDN problem you will actually encounter. The rest is a control panel. And when something is slow, the debugging order is fixed: check which PoP served the request (the cf-ray-style header), check whether it was a HIT or MISS, check whether the miss went to origin directly or through a tier, and check whether your cache key matched what you thought it did. Four questions, in that order, resolve the large majority of real CDN incidents.

Frequently asked questions

What is the difference between a CDN and a cloud provider's edge locations?
A CDN is a caching and delivery network optimized for serving content to many users from many locations. Cloud providers offer CDNs (CloudFront, Google Cloud CDN, Azure CDN) alongside their compute regions. The distinction blurs when platforms like Cloudflare combine CDN, compute (Workers), and storage (R2) on one anycast network.
Does a CDN improve performance for all users?
It helps most when your origin is far from a large share of your audience and your content is cacheable. If all your users are in one country with no PoP in that country, a CDN gives you cache and security benefits but limited last-mile latency improvement — the nearest PoP may still be in another region.
Why does my CDN serve stale content?
Usually the cache key does not vary on the thing that changed — a query parameter, a cookie, or an Accept header that your application uses to serve different content. Check your cache key configuration and the Vary header, and verify that your purge actually targeted the right URL pattern.
What is a cache hit ratio and what is a good one?
Cache hit ratio is the percentage of edge requests served from cache rather than fetched from your origin. For mostly-static sites, 95%+ is achievable and expected. For highly personalized APIs, even 50% may be ambitious. Hit ratio matters because origin egress and origin capacity scale with misses, not total traffic.