Cache Behaviors and TTLs
~10 min read
How CloudFront decides what to cache, for how long, and what makes two requests 'the same' for caching purposes.
A Cache Behavior is a rule tied to a URL path pattern (e.g. /images/, /api/, or * for everything) that controls how CloudFront handles matching requests: which origin to use, whether to cache at all, the TTL (minimum, maximum, and default, either set explicitly or inherited from the origin's Cache-Control headers), and critically, the cache key — which parts of the request (query strings, specific headers, specific cookies) are included when CloudFront decides whether two requests are 'the same' and can share a cached response.
By default, modern CloudFront cache policies exclude query strings, headers, and cookies from the cache key unless you explicitly add them — meaning /page?ref=twitter and /page?ref=facebook would return the exact same cached response unless you explicitly tell CloudFront to vary the cache by the ref query parameter. This default is usually what you want (maximizes cache hit rate) but can surprise you if your application actually needs different responses for different query parameter values.
Origin Cache-Control and Expires headers can drive CloudFront's caching behavior directly if you don't override them with an explicit CloudFront cache policy — this lets your origin application control caching per-response (e.g. a CMS marking one page as cacheable for an hour and another as no-cache) without needing to reconfigure CloudFront itself for every content type.
💬 Deep Dive with AI
Key points
- •Cache behaviors are matched by URL path pattern, most specific pattern wins
- •The cache key determines what counts as 'the same request' — by default excludes query strings/headers/cookies
- •TTLs can be set explicitly in CloudFront or driven by the origin's Cache-Control/Expires headers
- •Including unnecessary elements in the cache key fragments the cache and hurts hit rate