Designing Cache Behaviours and Cache Policies
This lesson explains how CloudFront decides what to cache and for how long, which determines both performance and correctness, and why most caching problems come from the cache key and TTL settings.
Learning outcomes
After completing this lesson, you will be able to:
- Configure cache behaviours by path pattern.
- Define the cache key and TTLs.
- Use invalidations and versioned file names.
Cache behaviours
A distribution has a default cache behaviour and optional additional behaviours matched by path pattern (for example /api/*, /images/*, *.css) in precedence order. Each behaviour chooses an origin, the viewer protocol policy, allowed methods, a cache policy, an origin request policy, a response headers policy, and edge functions. A typical distribution caches static assets aggressively and passes API requests with caching disabled.
Cache key and TTLs
The cache policy defines the cache key, which identifies a cached object: always the URL path and, optionally, selected query strings, headers and cookies. Every value included creates separate cache entries, so including all headers or cookies can make every request unique and the hit ratio near zero. The policy also sets minimum, default and maximum TTL; origin Cache-Control: max-age or s-maxage and Expires headers control caching within those bounds, and no-store or private prevent caching (unless the minimum TTL is above zero). The origin request policy forwards additional values to the origin without adding them to the cache key, which is how to send a header the origin needs for logging while still caching efficiently. AWS provides managed policies such as CachingOptimized and CachingDisabled.
| Setting | Effect on caching |
|---|---|
| Cache policy: query strings/headers/cookies | Included in cache key; more values = lower hit ratio |
| Origin request policy | Forwarded to origin, not part of cache key |
| Minimum / default / maximum TTL | Bounds on how long objects are cached |
| Origin Cache-Control headers | Object-specific TTL within the bounds |
| Compression (Gzip, Brotli) | Smaller responses; encoding normalised in the cache key |
The following diagram compares two cache keys for the same three requests.

Cache key or origin only?
Invalidations and versioning
To remove content from edge caches before its TTL expires, create an invalidation for paths such as /index.html or /css/*; the first 1,000 paths per month are free and further paths are charged, and a wildcard counts as one path. Invalidations take effect across edge locations in minutes. For frequently updated assets, versioned file names (for example app.3f9a2c.js) are preferred: each release references new names, so caches never serve stale versions, rollback is simple, and no invalidation cost is incurred.
aws cloudfront create-invalidation --distribution-id EDFDVBD6EXAMPLE --paths "/index.html" "/css/*"Updating cached content
Designing behaviours for a web application
A web application serves static assets from S3 and an API from an ALB through one distribution. The default behaviour (*) uses the S3 origin with the CachingOptimized policy and versioned asset names. A /api/* behaviour uses the ALB origin, allows all HTTP methods, uses CachingDisabled, and an origin request policy that forwards all viewer headers except Host, cookies and query strings. A /api/catalogue/* behaviour with higher precedence caches catalogue responses for 60 seconds with only the lang query string in the cache key, offloading most catalogue traffic from the ALB.
How can a header be sent to the origin without adding it to the cache key?
Show answer
With an origin request policy.
Exam Tips
Remember for the exam
- The cache key includes the path plus chosen query strings, headers and cookies.
- Forward values to the origin with an origin request policy to keep them out of the cache key.
- TTL bounds in the cache policy interact with origin Cache-Control headers.
- Use versioned file names; invalidate only when necessary.
- Order behaviours by path pattern precedence; the default (*) is last.