Перейти к содержимому
Skip to content
← All articles

CDN Cache Invalidation: Strategies for Delivering Fresh Content

CDNs dramatically improve performance by caching content at edge locations worldwide. But caching creates a fundamental challenge: how do you ensure users see updated content when you make changes? Cache invalidation — the process of removing or replacing stale cached content — is one of the hardest problems in web infrastructure.

Why Cache Invalidation Is Hard

Phil Karlton famously said: "There are only two hard things in Computer Science: cache invalidation and naming things." CDN cache invalidation is hard because:

Cache Invalidation Strategies

1. Purge (Cache Clear)

Explicitly tell the CDN to remove specific URLs from cache:

# Cloudflare API purge
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
  -H "Authorization: Bearer {token}" \
  -d '{"files":["https://example.com/styles.css"]}'

2. Cache-Busting (Versioned URLs)

Append a version hash or timestamp to file URLs:

<link rel="stylesheet" href="/css/main.css?v=a1b2c3d4">
<script src="/js/app.js?v=20250315"></script>

/* Or with hashed filenames */
<link rel="stylesheet" href="/css/main.a1b2c3d4.css">

3. TTL-Based Expiration

Set appropriate Cache-Control headers so content expires naturally:

# Short TTL for HTML (changes often)
Cache-Control: public, max-age=300

# Long TTL for versioned assets (URL changes on update)
Cache-Control: public, max-age=31536000, immutable

# No cache for dynamic API responses
Cache-Control: no-cache, no-store

4. Stale-While-Revalidate

Serve stale content while fetching fresh content in the background:

Cache-Control: public, max-age=300, stale-while-revalidate=86400

After 5 minutes, the content is "stale" but still served for up to 24 hours while the CDN fetches a fresh copy. Users never see a cache miss delay.

5. Cache Tags (Surrogate Keys)

Tag cached responses with logical identifiers, then purge by tag:

# Response header
Surrogate-Key: product-123 category-electronics homepage

# Purge all content tagged with "product-123"
curl -X POST "https://cdn.example.com/purge/product-123"

This is powerful for CMS-driven sites where one content update affects multiple pages.

Strategy by Content Type

ContentStrategyTTL
HTML pagesShort TTL + purge on publish5-60 min
CSS/JS (versioned)Cache-busting + immutable1 year
Images (versioned)Cache-busting1 year
API responsesShort TTL or no-cache0-60 sec
User-specificNo CDN cache (private)0
FontsLong TTL + CORS headers1 year

Best Practices

Conclusion

The best cache invalidation strategy is often to avoid invalidation entirely — use versioned URLs for static assets and let cache-busting do the work. For dynamic content, combine short TTLs with stale-while-revalidate. Reserve explicit purges for urgent updates and emergencies.

Check your website right now

Check now →
More articles: Performance
Performance
Latency vs Throughput: Understanding Network Performance Metrics
16.03.2026 · 19 views
Performance
Web Performance Budgets: Setting, Enforcing, and Automating Performance Limits
16.03.2026 · 12 views
Performance
API Performance Metrics and Optimization
14.03.2026 · 11 views
Performance
Web Application Caching Strategies
14.03.2026 · 10 views