Course contentsShow
← Web Development
Lesson 2162 of 2,98239. Network PerformanceFree lesson

CDN Cache Invalidation

Learn purging strategies, cache tags, and surrogate keys to invalidate stale content when deploying updates.

CDN Cache Invalidation

You'll learn how to force CDNs to serve fresh content when you deploy updates, preventing users from seeing stale files.

When you deploy a new version of your site, the CDN may still be serving old files from its edge servers. Cache invalidation is the process of telling the CDN to remove or refresh specific cached content so users get your latest code immediately.

Why Invalidation Matters

Imagine a bookstore chain that keeps copies of catalogs at every location. When you update the catalog, you need to either replace all copies or mark them outdated. CDNs work the same way—without invalidation, users might see outdated HTML, broken JavaScript references, or mismatched assets for hours or days.

Purging Strategies

Purge by URL: Remove specific files like /app.js or /index.html. Simple but tedious for large deployments.

Purge by path pattern: Clear entire directories like /assets/*. More efficient but less granular.

Purge all: Nuclear option—clears everything. Use sparingly as it defeats caching benefits and can spike origin server load.

Cache Tags

CDN providers let you assign custom tags to groups of related content (e.g., homepage, product-123). When content changes, purge by tag instead of listing individual URLs. This scales better and keeps your invalidation logic maintainable.

Surrogate Keys

Similar to cache tags but often generated dynamically by your origin server. Your backend sends a Surrogate-Key header with the response, and you can later invalidate all content matching that key. Perfect for complex relationships like "all pages mentioning Product X."

Key Takeaway: Use cache tags or surrogate keys for scalable invalidation, reserving full purges for major releases—this ensures users always see fresh content without overwhelming your CDN or origin server.