Skip to content

CDNs and edge caching

A CDN keeps copies of your responses at edge locations near users and answers repeat requests there, so your servers see far less traffic. A strong answer says what it stores, how long, and how a wrong copy goes.

On this page

What it is

A CDN (content delivery network) is a shared HTTP cache placed in front of your servers at many locations close to your users. In the vocabulary of the HTTP caching spec, a browser cache is a private cache tied to one client, while a CDN is a shared cache: it sits between clients and your origin and stores responses that other users may reuse. MDN groups CDNs with reverse proxies as managed caches, deployed by the service's own developers to take load off the origin and steered through the Cache-Control header plus the product's configuration. The nearby location (an edge, or point of presence) answers a hit from its copy; on a miss it fetches from the origin, keeps the response if the headers allow it, and serves that copy to later requests until it goes stale.

Two things decide most of what an edge returns, alongside the directives below and the product's own configuration. The first is the cache key: stored responses are told apart by URL, and the Vary header adds named request headers to that key, so a response that differs by Accept-Language is stored once per language instead of being mixed up. The second is freshness. A stored response is fresh while its age, the seconds since the origin generated it, is below max-age; after that it is stale. s-maxage sets a separate limit for shared caches and overrides max-age and Expires there, while private caches ignore it, so you can hold a copy at the edge for an hour and tell browsers to keep theirs for a minute. A shared cache reports the elapsed time in an Age header so the browser's own countdown starts from the right point.

DirectiveWhat a cache does with itReach for it when

s-maxage=N

Fresh for N seconds in shared caches; browsers ignore it

The edge should hold a copy longer (or shorter) than browsers

max-age=N

Fresh for N seconds in any cache that has no s-maxage to follow

The default lifetime for browsers and the edge alike

public

May be stored by a shared cache even when the request carried Authorization

Responses behind basic auth that are the same for all users

private

May be stored by the user's own browser; never by a shared cache

Pages rendered for a logged-in user

no-store

Not stored by any cache; a copy stored earlier is not removed

Responses that should not be kept anywhere; pair with no-cache if an older copy may exist

immutable

Won't change while fresh, so a reload skips the revalidation request

Assets with a hash or version in the URL and a long lifetime

stale-while-revalidate=N

May serve the stale copy for N more seconds while fetching a fresh one in the background

Hiding origin latency at the moment a copy expires

When a copy goes stale it is not thrown away at that instant. If it carries a validator, the cache can ask the origin with a conditional request, If-None-Match carrying the stored ETag or If-Modified-Since carrying the stored date; if nothing changed, the origin answers 304 Not Modified without a body and the cache treats its copy as fresh again. Without a validator the cache fetches the response again in full. In standard HTTP caching the timer running out does not refresh anything on its own: a request has to arrive and find the copy stale. stale-while-revalidate changes what that request gets: the stale copy at once, while the validation happens behind it.

Going stale is the usual way a copy stops being served as is; a purge is for when it has to go now. Cloudflare's cache documentation offers purge by single URL, which it recommends, plus purge by hostname, cache tag, URL prefix or the whole zone, and a separate invalidate call that marks content stale so it is revalidated on the next request. It warns that a successful purge call means the request was accepted, not that a copy was evicted, so you confirm by requesting the asset and checking its cache status header. Amazon CloudFront's developer guide also lets you invalidate paths, but for files that change often it recommends versioned file names instead: a new name is a new cache key, so no purge is needed; a user who holds the old copy in a browser or a corporate proxy still gets the new file, because the old URL is simply not requested again; and invalidation has its own charge, which versioning avoids, though moving the new files to the edge is still paid for.

An edge can also run small pieces of logic, which matters when the response is a redirect rather than a file. Cloudflare's rules documentation describes three ways: Single Redirects, rules at the domain level with wildcard patterns; Bulk Redirects, a static list of source and target URLs kept at the account level, with a default quota of a million entries on its top plan and more on request; and Snippets, short JavaScript for cases that need computation. Single and Bulk Redirects both require the domain's DNS records to be proxied through the CDN, and Single Redirects are evaluated before Bulk Redirects in the rule execution order.

When to use it

Use a CDN when many users, spread across regions, ask for the same public response: a stylesheet, an image, a product page that reads the same for everyone, an API response that changes on a schedule. The best fit is a static asset with a hash or version in its URL. Give it a long max-age plus immutable, and publish changes under a new URL, the cache-busting pattern, so the old copy stays correct for its own URL and simply stops being requested.

Our favorite case for a CDN is when one link, one image or one short code suddenly takes most of the traffic. If the response may be stored, each edge location that misses fetches it from the origin once and serves nearby users from its copy until the lifetime ends, so origin load grows with the number of edge locations and lifetimes, not with the number of users. Even a short lifetime changes the shape of the load this way; the price is that a copy may be served a little after the data changed, and longer still if you allow stale reuse with stale-while-revalidate. If the hot response is a redirect you want to count, decide where the click is recorded: if the origin must record each click, don't let a shared cache serve the redirect; if the edge may answer it, say how the count is collected instead. Either way, set Cache-Control explicitly rather than leaving caches to guess, because a response with no caching headers can still be stored and reused under heuristic caching, with a lifetime the cache picks from the Last-Modified date.

When not to

Not for personalized responses. A page rendered for a logged-in user, a cart, an inbox: a shared cache that stores one of these can hand it to the next user, which is a data leak rather than a latency problem. Mark them private so that at most the user's own browser keeps a copy, and note that a cookie on the request does not tell a cache the response is personal; the directive does.

Not for reads that must be current: a balance, an inventory count, a request you bill for. Send no-cache, which makes a cache validate a stored copy with the origin before reusing it, rather than relying on no-store on its own: no-store stops new copies from being stored, but it does not remove a copy a cache stored earlier.

Not when a simpler cache does the job. If your users and your servers share one region, a reverse proxy you run, or a cache inside your service, gives a similar hit rate without a third party in the path. If each URL is requested about once, as with search results or per-request API calls, the edge misses almost all the time and adds a hop. And if content must change for everyone within seconds and you can't version the URL, know what a purge can and can't do: purge calls are rate limited per account, and a purge at the CDN leaves copies already in browsers in place until they expire.

Compared

OptionWhere the copy livesRight choice whenWeaker when

Browser cache (private)

On one user's device

The same user repeats the request, or the content is personal

Nothing is shared between users, and a purge at the CDN does not reach it

CDN edge (shared, managed)

Many locations near users

Public responses, users spread worldwide, one hot URL

Responses are personal or must be current on each read; the audience is in one region

Reverse proxy you run

One shared cache in your own data center

You want full control over the cache and users are near it

Distant users cross the network on each request; your hardware is the ceiling

Cache inside your service

Behind your service, keyed by your code

Per-user data, computed objects, deletes that must be exact

Each request still travels to your service and back

Logic at the edge (rules, functions)

Code at the edge that can answer before the origin is asked

Static redirect tables, header rewrites, routing by country

The decision needs a fresh database read per request

In a picture

miss304Tokyo edgeTokyo usersOrigin

Step 1 of 3

A miss fills one edge

Users near Tokyo ask the Tokyo edge. It has nothing, fetches from the origin, and stores the response for the lifetime the origin named.

How to say it in an interview

The common mistake

Quiz

You want the CDN to keep a response for ten minutes but browsers to recheck after thirty seconds. Which header does that?
You changed a stylesheet, purged it at the CDN, and a user still sees the old version. What is the most likely reason?
A response shows the logged-in user's name. Which directive keeps it out of the CDN while letting the user's browser store it?
One URL takes nearly all traffic and is served with s-maxage=10. What does the origin see?

Questions that use this

Related

Sources

  1. MDN: HTTP caching
  2. MDN: Cache-Control header
  3. Amazon CloudFront Developer Guide: Invalidate files to remove content
  4. Cloudflare Cache (CDN) docs: Purge cache
  5. Cloudflare Rules docs: Redirects
Sign in to report a mistake