Traffic and messaging
Purging and versioned URLs
Change content in a CDN by giving it a new URL, purging exact URLs, or purging by tag. Pick the cheapest one that matches how many pages a change touches.
When content changes, the CDN must stop serving the old version. You have three tools. A versioned URL gives new content a new address, so there's nothing to purge. A purge by URL deletes specific objects. A purge by tag deletes every object that carries a label, however many URLs it spans.
Concept
A purge removes an object from the cache before its TTL ends. A tag, also called a surrogate key, is a label the origin attaches to a response. A tag purge removes every response carrying that label.
The big idea
The best purge is the one you never run. Think of a new book edition with a new ISBN. The old edition stays on the shelves, but nobody asks for it, so nothing has to be pulled.
Fingerprinted asset URLs work this way. The build tool names a file after a hash of its contents, such as app.3f9a1c.js. The HTML points to that name. A code change makes a new hash, which makes a new URL. Old copies age out on their own.
Walking through it
Versioned URLs suit static assets. Those files get a one-year TTL and never revalidate:
Cache-Control: public, max-age=31536000, immutableOnly the small HTML document that references them needs a short TTL.
Purge by URL works for a few pages. But a price change can touch the product page, three category pages, search results and the homepage. Purging each URL by hand is error-prone.
Purge by tag solves that. The origin labels each response with the things it depends on:
Surrogate-Key: product-4521 category-shoesWhen product 4521 changes, purge the tag product-4521. Every response that included it disappears from every PoP, usually within a second or two on modern CDNs. This lets you cache dynamic HTML for hours and still update it at once. Many news and e-commerce sites reach high hit ratios this way.
A soft purge marks content stale rather than deleting it. Combined with stale-while-revalidate, users don't see a miss spike after the purge.
Avoid purge everything during traffic peaks. It sends 100% of requests to the origin at once. That is the stampede the CDN exists to prevent.
Common mistakes
- Purging everything at peak. Every request then hits the origin together.
- Caching HTML for hours with no tags. You then have no fast way to update it on a change.
- Fingerprinting assets but leaving a long TTL on the HTML. The HTML keeps pointing at old files, so the new hash never gets used.
Check yourself
- Why is a fingerprinted asset easier to manage than a purged one? A change creates a new URL, so there's nothing to purge. Old URLs simply stop being requested.
- Why is purge-everything risky? Every request then goes to the origin at once, the stampede the CDN normally absorbs.
Key takeaways
- Fingerprinted URLs need no purge at all. Use them for static assets with one-year TTLs.
- Tag purges let dynamic pages stay cached for hours and still update instantly.
- Soft purges with
stale-while-revalidateavoid miss spikes. - Never purge everything during peak traffic.
Further learning
Fastly's guide to working with surrogate keys covers tag purging with examples.
Next, read about CDN hit ratio and cost, which turns hit ratio into dollars.
Related
CDNs and the edge
Serve content from points of presence near users. A CDN cuts latency, absorbs attacks and cuts bandwidth costs, and its cache keys, headers and purge rules decide how well it works.
Cache control and cache keys
The cache key says which requests count as the same object, and Cache-Control headers say how long to keep it. Get the key wrong and you waste the cache or leak private data.
Cache stampede
When a hot key expires, every request misses at once and runs the same expensive query. Fix it by letting one request recompute, refreshing early, or serving slightly stale data.