Cache-Control
The primary HTTP header for controlling caching behavior. Tells browsers, CDNs, and proxies whether to cache a response, for how long, and under what conditions to serve or revalidate it.
Full Explanation
Cache-Control is the single most important header for CDN operations. It's how your origin tells every cache in the chain what to do with a response. Get it wrong and you'll either serve stale content to users or hammer your origin with unnecessary requests.
The header accepts multiple directives, comma-separated. The key ones for CDN work: max-age (how long to cache), s-maxage (override for shared caches like CDNs), no-cache (cache but always revalidate), no-store (don't cache at all), must-revalidate (don't serve stale), and the stale-* directives for resilience.
A common pattern: set a short max-age for browsers (60s) and a longer s-maxage for your CDN (3600s). The CDN caches for an hour, browsers recheck every minute. Best of both worlds.
Try the interactive Cache-Control Builder animation in the course to see how directives combine and affect caching behavior.
See also the Cache Hierarchy animation to understand how Cache-Control flows through multi-tier caches.
Interactive Animation
Examples
# Common patterns:
# Static assets (immutable, hash in filename)
Cache-Control: public, max-age=31536000, immutable
# API responses (short browser cache, longer CDN cache)
Cache-Control: public, max-age=60, s-maxage=3600
# Personalized content (don't cache on CDN)
Cache-Control: private, max-age=0, no-store
# Resilient caching (serve stale on errors)
Cache-Control: public, max-age=300, stale-while-revalidate=60, stale-if-error=86400
# Nginx example
location /api/ {
add_header Cache-Control "public, max-age=60, s-maxage=3600";
}
Video Explanation
Frequently Asked Questions
The primary HTTP header for controlling caching behavior. Tells browsers, CDNs, and proxies whether to cache a response, for how long, and under what conditions to serve or revalidate it.
# Common patterns:
# Static assets (immutable, hash in filename)
Cache-Control: public, max-age=31536000, immutable
# API responses (short browser cache, longer CDN cache)
Cache-Control: public, max-age=60, s-maxage=3600
# Personalized content (don't cache on CDN)
Cache-Control: private, max-age=0, no-store
# Resilient caching (serve stale on errors)
Cache-Control: public, max-age=300, stale-while-revalidate=60, stale-if-error=86400
# Nginx example
location /api/ {
add_header Cache-Control "public, max-age=60, s-maxage=3600";
}
Related CDN concepts include:
- Age Header — A response header added by caches indicating how long (in seconds) the response has been …
- Vary Header — A response header that tells caches which request headers should be included in the cache …
- max-age — Specifies the maximum time in seconds that a response is considered fresh. Applies to all …
- must-revalidate — Once content becomes stale, the cache must revalidate with the origin before serving it. The …
- proxy-revalidate — Like must-revalidate, but only applies to shared caches (CDNs). Browser caches can still serve stale …
- s-maxage — Stands for "shared max-age". Overrides max-age specifically for shared caches (CDNs, reverse proxies). Browser caches …
- stale-if-error — Allows caches to serve stale content when the origin returns an error (5xx) or is …
- stale-while-revalidate — Allows caches to serve stale content immediately while fetching a fresh copy in the background. …