Cache Behaviors and TTL Settings
Define path-based cache behaviors, set minimum, default, and maximum TTLs, and use cache-control headers to fine-tune caching.
Cache Behaviors and TTL Settings is a free AWS Solutions Architect lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Cache Behaviors: What Are They?
Cache behaviors are the rules that tell CloudFront how to handle requests for different URL path patterns. Each cache behavior maps a path pattern (e.g., /images/*, /api/*, *.css) to a specific origin and caching configuration.
A distribution has one Default Cache Behavior (matching all paths not caught by more specific behaviors) and up to 25 additional path-based behaviors. CloudFront evaluates behaviors in order from most specific to least specific, then falls through to the default.
Path Pattern Matching
Path patterns support wildcards: * matches any combination of characters including slashes, and ? matches any single character. Examples:
/images/*— all URLs starting with /images/*.jpg— all requests ending in .jpg anywhere in the path/api/v2/*— all API v2 routes/static/??.css— static CSS files with exactly two characters before .css
Behaviors are evaluated in the order listed in the distribution configuration. Put more specific patterns first. The default behavior (*) always matches last.
Cache Policy vs Origin Request Policy
CloudFront separates caching logic into two policy types:
- Cache Policy: defines what CloudFront uses as the cache key—the combination of headers, query strings, and cookies that determine whether a cached object matches a request. Also sets TTL bounds.
- Origin Request Policy: defines which headers, query strings, and cookies are forwarded to the origin—even if they are not part of the cache key (to send auth headers to the origin without varying the cache per token)
AWS provides managed policies (e.g., CachingOptimized, CachingDisabled) that cover most use cases, or you can create custom policies.
TTL Settings in CloudFront
CloudFront respects three TTL values from the Cache Policy:
- Minimum TTL: the shortest time CloudFront caches an object, regardless of origin headers (default 0)
- Default TTL: the time CloudFront caches an object when the origin does not send a
Cache-ControlorExpiresheader (default 86,400 seconds = 1 day) - Maximum TTL: the longest CloudFront will cache an object, capping the origin's
Cache-Control max-agedirective (default 31,536,000 = 1 year)
These three values bound the actual cache duration sent by origins via Cache-Control headers.
Cache-Control Headers from Origins
When your origin sends a Cache-Control: max-age=3600 header, CloudFront caches the object for 3,600 seconds—as long as this value falls within the Cache Policy's Minimum and Maximum TTL bounds. If the origin sends Cache-Control: no-cache or Cache-Control: no-store, CloudFront will check with the origin before serving the cached copy each time.
For static assets that rarely change, set a long max-age (e.g., 31536000 = 1 year) and use cache busting—include a content hash in filenames (e.g., app.a3f4b5.js)—so the URL changes when content changes, automatically invalidating the old cached version.
# S3 object metadata with long cache TTL
aws s3 cp app.a3f4b5.js s3://my-bucket/ \
--cache-control 'max-age=31536000, immutable' \
--content-type 'application/javascript'Separating Static and Dynamic Behaviors
A powerful cache behavior pattern separates static and dynamic content:
/static/*,*.css,*.js,*.jpg→ S3 origin, CachingOptimized policy (high TTL, no cookies/query strings in cache key)/api/*→ ALB origin, CachingDisabled policy (always fetched from origin, all headers/cookies forwarded)/*(default) → ALB origin, moderate caching
This separates the high-cacheable static tier from the dynamic API tier, maximising cache hit ratios for static content while ensuring API responses are always fresh.
Cache Invalidation
When you update content in S3 or your origin and want to immediately serve the new version from CloudFront without waiting for TTL expiry, you create a cache invalidation. Specify paths to invalidate (e.g., /images/logo.png or /images/*) and CloudFront removes those objects from all edge caches.
Invalidations have a cost: the first 1,000 paths per month are free; additional paths cost per-path. Wildcard invalidations (e.g., /*) count as one path. Best practice: use versioned filenames for static assets (cache busting) instead of frequent invalidations to reduce cost and delay.
# Create a cache invalidation for updated images
aws cloudfront create-invalidation \
--distribution-id EDFDVBD6EXAMPLE \
--paths '/images/logo.png' '/css/main.css'Cache Key Components
The cache key is the unique identifier CloudFront uses to look up a cached response. By default, the cache key is only the URL path. Including additional components increases the number of distinct cache entries:
- Query strings:
/search?q=awsand/search?q=s3are separate cache entries ifqis in the cache key - Headers: including
Accept-Encodinglets CloudFront cache gzip and non-gzip versions separately - Cookies: including session cookies creates per-user cache entries, effectively disabling caching
Minimise cache key components for maximum cache efficiency. Only include what genuinely produces different response content.
Compression at the Edge
CloudFront can automatically compress text-based objects (HTML, CSS, JavaScript, JSON) using gzip or Brotli before delivering them to viewers. This reduces payload size by 60–80% and improves page load times without any changes to your origin.
To enable compression: ensure the Cache Policy includes Accept-Encoding in the cache key (CloudFront needs to cache gzip and non-gzip versions separately), and enable Compress Objects Automatically in the cache behavior. CloudFront compresses objects larger than 1,000 bytes and smaller than 10 MB.
Cache Hit Ratio and Monitoring
The cache hit ratio is the percentage of requests served from the CloudFront cache without going to the origin. A high ratio (80%+) means lower origin costs and better performance. Monitor it via the CloudFront console Cache Statistics report or via the CacheHitRate CloudWatch metric.
Ways to improve the cache hit ratio: increase TTL values, reduce the number of headers/cookies in the cache key, use query string normalisation (forward only the query strings your application actually uses), and set appropriate Cache-Control headers at the origin.
# Get CloudFront metrics for cache hit rate
aws cloudwatch get-metric-statistics \
--namespace AWS/CloudFront \
--metric-name CacheHitRate \
--dimensions Name=DistributionId,Value=EDFDVBD6EXAMPLE \
--start-time 2026-06-19T00:00:00Z \
--end-time 2026-06-20T00:00:00Z \
--period 3600 \
--statistics Average \
--region us-east-1Per-Behavior Origin and Protocol Settings
Each cache behavior can point to a different origin, enabling a single CloudFront distribution to serve content from multiple backends. For example:
/static/*→ S3 origin (private bucket via OAC)/api/*→ ALB origin in us-east-1/media/*→ MediaPackage CDN origin for video streaming
Each behavior also independently configures Viewer Protocol Policy, Allowed HTTP Methods, and function associations (CloudFront Functions or Lambda@Edge). This makes a single distribution a flexible, multi-purpose delivery layer.
Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: cache behaviors map URL path patterns to origins and caching rules, Min/Default/Max TTL bounds control how long content is cached with the origin's Cache-Control headers taking precedence when present, and cache invalidations immediately clear stale content from all edge locations. Minimise cache key components to maximise hit ratio. Next up we explore signed URLs, signed cookies, and geo-restriction.
Frequently asked questions
Is the “Cache Behaviors and TTL Settings” lesson free?
Yes — the full text of “Cache Behaviors and TTL Settings” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Cache Behaviors and TTL Settings”?
Define path-based cache behaviors, set minimum, default, and maximum TTLs, and use cache-control headers to fine-tune caching. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache Behaviors and TTL Settings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- CloudFront Distributions and Origins
- Cache Behaviors and TTL Settings
- Signed URLs, Signed Cookies, and Geo-Restriction
- CloudFront with WAF and Lambda@Edge