How CDN Cache Keys and Freshness Control Work
How Cache Keys Work
The base key combines scheme (http/https), host, path, and normalized query parameters. CDNs add selected HTTP headers via the Vary response header to create variants. Example: https://example.com/product?id=123 with Vary: Accept-Encoding stores separate gzip and brotli compressed versions. Every additional dimension multiplies variants and fragments cache memory, directly reducing hit ratio.
Freshness Control
The Cache-Control: s-maxage directive tells shared caches (CDNs) how long to consider objects fresh, independent of browser max-age. Typical values: 60s for dynamic content, 31536000s (1 year) for immutable assets. Stale while revalidate allows serving slightly stale content while refreshing in background, keeping p99 latency flat (20-50ms) during origin refreshes.
Revalidation
When cache entries expire, CDNs use conditional requests with If-None-Match (ETag) or If-Modified-Since (timestamp). If content unchanged, origin returns 304 Not Modified (few bytes vs full response), extending freshness without retransmitting data. This reduces bandwidth by 90%+ for stable content.