CSE Roadmap

Common tier · about 4 hours · 8 parts

Part 5 of 8 · 2 min read

Freshness

Keys, TTLs and purges.

Cache key. The key identifies "the same content". It is usually the host plus path, plus chosen query parameters and headers. Too specific a key (every random tracking parameter) fragments the cache and lowers the hit ratio. Too loose a key serves the wrong content (for example mixing a mobile and a desktop image). Customers choose which parts to include. Headers such as the accepted language or compression format are added by a "vary" rule.

TTL. Set by the origin with headers, or by a default rule. The book's advice: the expiry should be neither too long (stale content) nor too short (repeated loads from the origin). Static files with a hash in the name can have a TTL of a year. A page that changes hourly gets a few minutes.

Revalidation. When the TTL expires, the cache need not download again. It asks the origin "has it changed?" with a version tag (ETag) or a modified time. If not, the origin replies "not modified" with no body, and the cache extends the TTL. This saves bandwidth.

Stale rules. Serve a stale copy while fetching a fresh one (stale-while-revalidate), and serve stale if the origin is down (stale-if-error). This keeps the site up when the origin fails.

Invalidation. To remove a file before it expires, two methods (from the book):

  1. Purge by API: the customer calls the CDN's API with a URL, a prefix or a tag. The control plane sends the purge to every PoP. The speed matters: a purge should reach all PoPs in seconds. Implementation: a publish and subscribe channel from the control plane to all PoPs, with each PoP confirming.
  2. Versioning: put a version in the URL (image.png?v=2, or a hash in the file name). A new version is a new key, so no purge is needed and old copies fade by themselves. This is the safest and cheapest method, and the best default for assets.

Negative caching. Cache "not found" and error answers for a short time (say 10 seconds), so a missing file does not hit the origin on every request.

A worked example (made up). A site ships app.3f9a1c.js with a 1-year TTL. A new release creates app.8be220.js, and the HTML (TTL 5 minutes) now points to it. No purge needed: after 5 minutes every user loads the new file name, and the old one expires unused.

Common mistake

A long TTL on a file whose name never changes. Users keep the old version until it expires, and a purge must be sent to every edge, and to every browser cache too, which is impossible.

Check yourself: 1. What goes wrong with a cache key that includes a tracking parameter? 2. Why is versioning safer than purging? 3. What is negative caching?