Cache Purging Strategies - CDN Cache Invalidation

LESSON

Caching, Workers, and Performance

023 30 min intermediate

Cache Purging Strategies - CDN Cache Invalidation

By the end of this lesson, you will be able to...

  • Trace what happens at the edge and origin after a CDN purge for a changed public page.

  • Choose a purge handle and a hard or soft transition from the freshness requirement and expected refill pressure.

  • Investigate stale survivors and origin spikes using purge scope, cache-status, propagation, and refill signals.

Idea in one sentence: A CDN purge is a distributed state change: it must select every unsafe copy, make the transition visible at the edge, and keep the refill wave from becoming an origin outage.

Core Insight

At 11:00, the shop replaces a misleading hero banner on its homepage. The page is public and cached at many edge locations. It has four representations: Spanish and English, each in desktop and mobile form.

The first model says: issue “purge /,” then the page is fixed. That works only if / identifies every stored representation and the origin can comfortably answer every miss that follows. Neither condition is automatic.

One edge may hold home|es|desktop; another may hold home|en|mobile. If the purge matches only a visible URL but the provider's effective key includes variants, some users may still see the old hero. If a broad purge removes every warm copy at once, the change can be correct while the origin is suddenly asked to render the homepage from many places at the same time.

Purging is therefore not a delete button after a deployment. It is an operational transition between an old cache population and a new one. The same plan needs to answer: which copies become unsafe, how will readers get a fresh representation, and how much concurrent refill can the origin absorb?

The Production Symptom: Old Banner Here, Origin Spike There

The on-call view shows two reports ten seconds after the content team publishes:

Madrid, es, desktop: new banner
Toronto, en, mobile: old banner

origin homepage renders: 4x normal

These observations are not contradictory. They point to two separate questions.

  1. Scope: Did the purge target the cache entries that can serve the English/mobile representation?
  2. Transition: Did removing or expiring copies cause a refill surge through many edges or request paths?

Do not start by assuming that the CDN is “slow.” First capture the request route, chosen cache key dimensions, cache status, purge identifier or tag, content version, and edge location. Those details let the team distinguish an incomplete purge from a completed purge followed by a weak refill design.

The Initial Model: One URL Is One Cached Object

For a small local cache, a key may look like one URL and deleting it may be enough. That model works while the URL is the complete identity of the response and one cache serves a modest number of readers.

At a CDN, a public page can vary by path, language, device policy, query handling, an edge-selected experiment bucket, or provider-specific cache configuration. The previous lesson made one such choice visible: an edge function can turn raw request data into a bounded public variant. That same choice changes which stored objects a purge must affect.

Two opposing mistakes follow:

The better model is not “always purge by URL” or “always purge everything.” It is to give related responses an explicit invalidation handle, then choose the smallest handle that matches the freshness promise.

The Failure Mechanism: Scope Meets Refill

In plain English, a purge tells an edge that a stored representation may no longer be used under the old freshness rule. The exact behavior and vocabulary differ by CDN, so confirm the selected provider's semantics. In this lesson, use these teaching definitions:

The technical names are purge scope, propagation, and refill control. Scope selects the representations. Propagation is the control-plane movement that tells edge locations about the change. Refill control limits duplicate work once readers need a new copy.

For the homepage, a content model can attach a stable tag such as homepage to every language/device representation:

home|es|desktop  -> tags: homepage, campaign:summer
home|es|mobile   -> tags: homepage, campaign:summer
home|en|desktop  -> tags: homepage, campaign:summer
home|en|mobile   -> tags: homepage, campaign:summer

A purge for homepage has a clearer intent than manually listing every variant, as long as the origin and edge configuration actually assign that tag to every representation. That “as long as” is the operational contract. A tag that is missing on one response is not a documentation problem; it is a stale-content path.

A Worked Trace: Correct the Banner Without Creating a Stampede

Suppose the homepage may be stale for at most five minutes ordinarily, but the misleading banner must be removed quickly. The team has an origin shield and request coalescing for a cache miss. The timestamps are illustrative.

Time Control plane / edge state Reader result Signal to inspect
11:00:00 Origin publishes banner version b19; all four cached variants contain b18. Existing readers may still receive b18 under normal TTL. Origin version and cache version.
11:00:02 Team submits a purge for tag homepage with request id purge-884. No user result yet; submission is not proof of completion. Accepted scope and control-plane status.
11:00:06 An edge receives the purge and marks home|en|mobile stale. First reader there triggers revalidation/refresh under policy. Propagation lag and cache status.
11:00:07 Several readers arrive at that edge. Coalescing lets one origin/shield request refill; followers wait or reuse according to policy. Duplicate origin fetches per key.
11:00:12 Origin returns b19; edge stores the new representation. Later readers receive b19 locally. Content version, hit recovery, tail latency.

With a hard purge, the edge may need a new full response before serving the next request. That can be appropriate when the old content must not be shown. It costs more immediate origin work. With a soft purge or stale-while-revalidate-like policy, the platform may make a more gradual transition, but that is suitable only when the remaining stale window is acceptable. Do not label a soft transition “instant removal” if the product requirement says the old banner is unsafe.

The trade-off is situated: hard removal is justified when the old response is unsafe to show, while a soft transition is justified only when a stated stale window is acceptable and origin stability matters. Neither choice is a universal default.

So far, we have separated two outcomes. A successful control-plane submission is not the same as every edge having applied the policy. And a correct invalidation at every edge is not the same as a stable data-plane refill. Both must be observed.

Mitigation and Prevention

Choose the strategy from the content shape:

Change Starting strategy Why Boundary to name
Versioned JavaScript or image asset Publish a new versioned URL. New requests name the new immutable object; broad purge may be unnecessary. Old URLs can still be requested until their policy expires.
One public page with known variants Purge a URL or explicit tag that covers all variants. Scope matches the page's cache population. Verify every variant gets the handle.
Related pages share one campaign fragment Purge a campaign tag/key. One action targets a meaningful content set. Tag assignment becomes release-critical metadata.
Unsafe content must stop being served Hard purge plus origin/shield headroom and request coalescing. Prioritizes removal. A wide refill wave can still overload dependencies.
Briefly stale content is acceptable Stale/revalidation-oriented transition where supported. Smooths refill and protects origin. State the allowed stale time; provider semantics vary.

For frequently updated static assets, versioned names are often simpler than invalidating a stable name. CloudFront likewise documents versioned filenames as an alternative to invalidation and notes that invalidating an edge file makes the next viewer request fetch the latest version from origin. AWS CloudFront documentation.

Prevention starts before the incident: document cache-key dimensions, add tags at render time, test an edge variant in staging, estimate the origin cost of a cold key, and record the purge request id with the release. A purge runbook should include a rollback or fallback decision, not only the API command.

Signals to Watch

During a purge, use evidence that maps to the mechanism:

These signals expose the two common failure mechanisms. Old content with normal origin load suggests a scope or propagation gap. Origin stress with correct new content suggests a refill or purge-blast-radius problem. Both can happen together.

Check: A tag purge reports success, Spanish desktop shows the new banner, but English mobile still shows the old one. Origin load is normal. What should the investigation test first?

Think first, then reveal.

Answer: Test scope before capacity. Compare the English/mobile cache key and its assigned tags with the purge target, then inspect that edge's propagation state and response version. Normal origin load makes a refill stampede less likely; the evidence points first to a missing variant handle or incomplete propagation.

Readiness Check: Plan a Product Recall Notice

The shop must show a recall notice on all product pages within a short window. Product pages vary by language and device, while images use versioned URLs. The origin can tolerate a 30% temporary increase in page renders but not a full global cold start.

Write the smallest safe plan. Specify the purge handle, whether the old page may be served during transition, one refill protection, and four observations required before declaring the change complete.

A good answer should mention:

Connections

The previous lesson showed that an edge function can create bounded cache variants. This lesson makes their lifecycle operational: a purge must match the variants that edge code actually produces. The next lesson optimizes the same transition with key shaping, shielding, compression, and evidence-driven measurement.

Resources

Key Takeaways

PREVIOUS Edge Functions - Compute at the CDN Edge NEXT CDN Optimization Techniques - Performance at Scale