November 19, 2026
CDN Caching Done Right: Fast for Everyone, Safe for Everyone
Picture a bakery that bakes everything in one city. Every customer has to travel to that single shop, and at breakfast time it is mobbed…

By Rufus Kehinde
3 min read
Picture a bakery that bakes everything in one city. Every customer has to travel to that single shop, and at breakfast time it is mobbed. Now suppose the baker stocks small corner shops across the country with the bread people ask for most. A content delivery network, or CDN, is exactly that for your app: copies of your files close to your users, with the origin server visited only when a copy is missing or stale. This post is language and framework-neutral.
Start with who is allowed to keep a copy MDN's Cache-Control reference separates private caches, such as a browser, from shared caches, such as a CDN or proxy, which store one response and reuse it for many users [S1]. That single idea decides most of the design. The private directive means only a private cache may store the response, and public allows a shared cache to store it [S1]. Treat "who may keep this?" as the first question for every response. Public marketing pages and fingerprinted assets are shared-cache material. Anything shaped by a login or a cookie is not, until proven otherwise.
Four directives worth knowing
- max-age keeps a response fresh for some seconds after it was generated on the origin [S1].
- s-maxage sets freshness for shared caches only, is ignored by private caches, and overrides max-age for shared caches [S1]. This lets a CDN keep something longer than a browser, or the reverse.
- stale-while-revalidate lets a cache reuse a stale response while it revalidates in the background [S1]. Users get an instant answer while the copy refreshes.
- immutable tells a cache the response will not change while it is fresh, avoiding needless conditional requests [S1].
A common beginner trap: no-cache does not mean "do not cache". MDN says it allows storing but requires revalidation before reuse, and that no-store is the directive for "do not store" [S1].
The naming trick that removes most headaches My practice: put a fingerprint, such as a content hash, into the file name of scripts, styles, and images. When the file changes, its name changes, so the old name can be cached for a very long time as immutable and nobody ever sees a stale copy. HTML pages that point to those files stay short-lived. This is where a previous post here on caching meets another here on images and another here on the first three seconds.
Security section: two ways a CDN hurts you First, personal data leaks. MDN says that forgetting private on personalised content can let a shared cache store it and reuse it for multiple users, leaking personal information, and that private should be added especially for responses after login and for cookie-managed sessions [S1]. It also notes that requests with an Authorization header must not be stored in a shared cache by default, but that public forces them to be stored [S1], so be careful where you write that word.
Second, cache poisoning. PortSwigger describes it as exploiting the behaviour of a web server and its cache: the attacker gets a harmful response from the back end and ensures it is cached and served to victims. The cache decides two requests are equivalent using a cache key, and parts of a request left out of the key are "unkeyed", which attackers can use to inject payloads [S2]. Their prevention list includes restricting caching to purely static responses, disabling request headers the site does not need, and avoiding accepting "fat" GET requests [S2]. They note that the definitive prevention is disabling caching altogether [S2]; my judgment is that this is rarely practical, so the narrower rules matter more.
Tradeoffs, honestly
- Long freshness is fast, but stale content is real. Plan how you will clear bad copies.
- Serving stale while revalidating trades perfect freshness for speed, which suits news feeds and catalogues and does not suit prices or stock counts.
- Every extra variation of a response, such as by language or device, divides your cache and lowers the hit rate.
- A CDN is another system to monitor. Tie it into [Day 32] on observability: watch the cache hit rate and origin load.
A checklist you can use this week
- Does every response say who may cache it?
- Are personalised and logged-in responses marked private or not stored?
- Do static files carry fingerprints and long lifetimes?
- Do you know which request parts are in your cache key?
- Can you purge a bad copy quickly?
Sources [S1] MDN Web Docs, Cache-Control header. https://developer.mozilla.org/docs/Web/HTTP/Reference/Headers/Cache-Control [S2] PortSwigger Web Security Academy, Web cache poisoning. https://portswigger.net/web-security/web-cache-poisoning