Lesson 2 of 4 Amazon CloudFront and AWS Global Accelerator

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.

SettingEffect on caching
Cache policy: query strings/headers/cookiesIncluded in cache key; more values = lower hit ratio
Origin request policyForwarded to origin, not part of cache key
Minimum / default / maximum TTLBounds on how long objects are cached
Origin Cache-Control headersObject-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.

How the cache key decides the hit ratio. The cache key is always the URL path, plus any query strings, headers and cookies the cache policy includes. Including unique values, such as utm_ tracking parameters or all cookies, makes every request a different cache entry; including only the values that change the response, such as w and h, lets requests share entries.
The URLs are illustrative.
Sort into groups

Cache key or origin only?

Exam skills 5.2.3 · 5.3.3

Decide whether each value should be part of the cache key or only forwarded to the origin.

Log in to try this activity Interactive activities are free with an account.

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/*"
Scenario questions

Updating cached content

Exam skills 5.2.3 · 5.3.3

Choose how to replace content that CloudFront has cached.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.