Improving Cache Hit Ratios with CloudFront Cache Policies
This lesson explains how Amazon CloudFront caches content at edge locations close to users, reducing latency and the load on the origin, such as Amazon S3, an Application Load Balancer or API Gateway.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how the cache key decides whether a request is served from the cache.
- Distinguish cache policies from origin request policies.
- Describe how Cache-Control headers, cache behaviours and invalidations control freshness.
- Identify what CloudFront adds beyond caching, such as faster dynamic requests and origin failover.
Cache keys and policies
CloudFront stores each response under a cache key, and a later request is served from the cache only if it produces the same key. By default the key is the distribution's domain name and the URL path, so every viewer requesting /images/logo.png receives the same cached object. Two kinds of policy, attached to each cache behaviour, control what else is involved.
A cache policy decides which headers, cookies and query strings are added to the cache key, and the minimum, default and maximum TTLs. Anything in the cache key is also forwarded to the origin, because the origin may return a different response for each value. When the origin sends a Cache-Control header, CloudFront uses its max-age within the policy's minimum and maximum; when it sends none, the default TTL applies.
An origin request policy forwards additional values to the origin without adding them to the cache key. For example, the origin may need the viewer's User-Agent for its access logs, while the response is the same for every browser; putting that header in the cache key would create a separate copy of each object for every browser version. AWS provides managed policies for common cases, such as caching optimised for static content and caching disabled for APIs. The following table summarises the two policy types.
| Policy | What it does |
|---|---|
| Cache policy | Defines the cache key (headers, query strings and cookies that make an object unique) and the minimum, default and maximum TTLs |
| Origin request policy | Forwards extra values to the origin without adding them to the cache key |
Include only the values that change the response, such as Accept-Language for localised pages, to keep the cache hit ratio high. If the cache key includes all query strings and the User-Agent header, nearly every request becomes unique.
Tune the cache key
Controlling freshness
The freshness of content cached by CloudFront can be controlled in several ways. The following list describes them.
- Origins can set
Cache-Controlheaders. - Cache behaviours apply different settings per path, such as long caching for
/static/*and none for/api/*. - Invalidations remove objects now; the first 1,000 paths each month are free.
- Versioned file names such as
app.3f9c2.jsavoid invalidations, because each build gets a new cache key; onlyindex.htmlneeds a short TTL.
aws cloudfront create-invalidation --distribution-id E2ABCDEF123456 --paths "/index.html" "/static/*"Beyond caching
CloudFront provides further capabilities in addition to caching. The following list describes them.
- Dynamic, uncached requests also speed up, because TLS terminates near the user and traffic travels over the AWS backbone.
- Origin Shield adds a central caching layer that reduces load on the origin.
- An origin group of a primary and secondary origin fails over automatically when the primary returns errors.
A low cache hit ratio
A CloudFront distribution's cache hit ratio is only 15%, so most requests reach the origin. The cache policy includes all query strings and all cookies in the cache key.
| Issue | Fix |
|---|---|
Tracking parameters (utm_source) create a separate cache entry per campaign | Include only the query strings the origin actually uses |
| Every user's session cookie creates a separate entry for static images | No cookies in the cache key for /static/* |
| Short TTLs on versioned assets | Long TTLs for hashed file names |
After the change, the hit ratio rises above 90%, origin load falls, and pages load faster. Values the origin needs but that should not vary the cache go in an origin request policy instead.
How can a new version of a file be served before its cached copy expires?
Show answer
Create an invalidation for its path, or better, use versioned file names such as app.v42.js, so each release has a new cache key and old copies never need to be removed.
Exam Tips
Remember for the exam
- To vary cached content by a header, add it to the cache policy, but only if the response really depends on it.
- A low cache hit ratio improves when unnecessary headers, cookies and query strings are removed from the cache key.
- To serve new versions immediately without invalidations, use versioned file names.
- For automatic failover between two origins, use an origin group.
- To reduce load on the origin from many edge locations, enable Origin Shield.