Sub topic 8
Cache policies and what goes into the cache key
This chapter assumes you can create a distribution and read a cache behavior, as covered in the previous chapters. You should also know what HTTP headers, cookies, and query strings are.
The cache key decides what counts as the same object
CloudFront stores each cached object under a cache key. When a request arrives at an edge location, CloudFront builds the key from the request and looks for a match. A match is a cache hit. No match is a miss, and CloudFront fetches the object from S3.
The key always contains the domain name of the distribution and the URL path. A cache policy adds optional parts: specific headers, cookies, and query strings. It also sets the minimum, default, and maximum TTLs (time to live, in seconds).
Every extra value splits the cache
Each value you add to the key creates a separate cached copy for every distinct value. For static files from S3, the best key is usually just the path.
Suppose you include all query strings. The requests /app.js?utm=a and /app.js?utm=b are then cached separately, even though S3 returns the same file. Your hit ratio drops and S3 receives more requests.
Include a value only if S3 returns different content because of it. S3 does not read cookies, and it ignores most query strings, so most S3 sites need neither.
Managed policies
CloudFront provides managed cache policies, so you rarely need to write your own.
| Policy | Key contents | TTLs (min / default / max) | Use for |
|---|---|---|---|
| CachingOptimized | Path only, gzip and Brotli enabled | 1 s / 24 h / 1 year | Static sites and assets |
| CachingDisabled | Nothing cached | 0 / 0 / 0 | Dynamic or debug paths |
The CloudFormation example in the previous chapters used the ID 658327ea-f89d-4fab-a63d-7e88639e58f6. That is CachingOptimized.
A custom policy
Sometimes you need one query string in the key. For example, your build tool may request /data.json?v=42, and S3 serves a different object per version through a rewrite you control. This policy includes only v:
Resources:
AssetsCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: assets-version-query
MinTTL: 0
DefaultTTL: 86400
MaxTTL: 31536000
ParametersInCacheKeyAndForwardedToOrigin:
EnableAcceptEncodingGzip: true
EnableAcceptEncodingBrotli: true
HeadersConfig:
HeaderBehavior: none
CookiesConfig:
CookieBehavior: none
QueryStringsConfig:
QueryStringBehavior: whitelist
QueryStrings:
- vSetting both EnableAcceptEncoding options lets CloudFront compress eligible responses and cache each encoding separately, without you adding Accept-Encoding to the key yourself.
Cache policy versus origin request policy
A cache policy controls what is in the key and what is forwarded to the origin. An origin request policy is separate. It forwards extra values to the origin without adding them to the key. With S3 as the origin you seldom need one.
Common errors
- Forwarding all query strings or cookies. Hit ratio falls and every variant is a separate S3 fetch. Allow-list only what S3 needs.
- Adding the
Hostheader to the key for an S3 origin. CloudFront must send S3 its own bucket hostname. Do not forward the viewer'sHost, or S3 will not find the bucket. - Expecting a policy change to clear the cache. Existing cached objects stay under their old keys until they expire or you invalidate them. Invalidation is covered in a later chapter.
- Treating TTLs in the policy as the only freshness setting. The
Cache-Controlheader from S3 also matters. The next chapter covers how the two interact.
Exercise
Your site serves files from S3, and CloudFront is configured to include all query strings in the cache key. Analytics links append ?utm_source=... to page URLs. What happens to your hit ratio, and what is the fix?
Show answer
Each distinct utm_source value creates a separate cache entry for the same S3 object, so the hit ratio drops and S3 receives more requests. Fix it by using a cache policy that excludes query strings, such as CachingOptimized, or one that allow-lists only the query strings S3 actually needs.