Cache Control Headers for CDNs
You'll learn how to use HTTP headers to instruct CDNs exactly how to cache and serve your content efficiently.
Why Cache Control Matters for CDNs
When you use a CDN, you're not just serving files from a faster location—you're delegating caching decisions to distributed servers worldwide. Without proper headers, CDNs might cache too aggressively (serving stale content) or not enough (missing performance gains).
Think of cache headers as instruction labels on food containers. Just as "refrigerate after opening" or "best before" tells you how to store and when to check freshness, cache headers tell CDNs how long to store your content and when to verify it's still fresh.
The Three Key Headers
Cache-Control is your primary tool. Directives like max-age=31536000 tell the CDN "keep this for one year without checking back." For CDNs specifically, s-maxage overrides max-age, letting you set different cache durations for CDN edges versus browser caches.
Expires sets an absolute expiration date. While Cache-Control is preferred, some older systems still use this.
ETag provides a validation fingerprint. When cached content expires, the CDN sends the ETag to your origin server asking "is this version still current?" If yes, the origin responds with 304 Not Modified—no data transfer needed.
Typical CDN Configuration
For static assets: Cache-Control: public, max-age=31536000, immutable
For dynamic content: Cache-Control: public, max-age=300, s-maxage=3600
Always include: ETag for efficient revalidation
The CDN reads these headers from your origin response and caches accordingly. Setting them correctly means faster delivery, lower bandwidth costs, and less load on your origin servers.
Key Takeaway: Cache-Control, Expires, and ETag headers are your interface for programming CDN behavior—use them to balance freshness with performance by controlling cache duration and validation.