Learn / Infrastructure / Lesson 08

Edge caching

A CDN can serve your pages from a city near the visitor in milliseconds, or serve one person's account page to thousands of strangers. The headers decide which.

Last updated: 2026-09-19

What it is

A CDN (content delivery network) such as Cloudflare keeps copies of your responses in data centres around the world, called the edge. When a visitor in Manila asks for a page, the copy in Manila answers, and your server never hears about it.

What the edge may keep, and for how long, is decided by one header on your response: Cache-Control.

Why it is a rule

The lesson: caching a page that varies by person is not a performance bug. It is a data breach.

How to do it

Know the directives

DirectiveMeaning
publicAny cache may keep it, including the CDN
privateOnly the visitor's own browser may keep it
no-storeNobody keeps it, ever
no-cacheKeep it, but check with the server before each use
max-age=NThe browser may use it for N seconds
s-maxage=NShared caches (the CDN) may use it for N seconds; overrides max-age there
immutableThis URL's content will never change; do not even revalidate

Sort every response into one of three kinds

text
Same for everyone, changes on deploy   →  public, max-age=0, s-maxage=31536000
Same for everyone, never changes        →  public, max-age=31536000, immutable
Different per person, or has a secret   →  private or no-store

Purge on deploy

A long s-maxage is only safe if every deploy clears the edge. The new server purges the CDN as it boots, so no visitor sees an old page after a release.

Keep the list short and explicit

Decide per page, in one place, which paths may be edge-cached. Everything else defaults to not cached. A page added later is then safe by default.

How we do it here

Only pages that are identical for every visitor are edge-cached: the home page, About, the legal pages and these lessons. They are cached at Cloudflare until the next deploy, which purges the cache when the new server starts. Anything that reads a session, and every API response, is never stored at the edge.

Benefits

Disadvantages

Checklist

Sources

Read this lesson as Markdown