CloudFront and edge caching
How CloudFront caches at the edge: distributions, origins, behaviors, cache keys, TTLs, invalidation, origin protection and edge code.
S3 buckets, objects and consistencyElastic Load BalancingTLS certificates with ACM
CloudFront and edge caching
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- CloudFront serves responses from edge locations near users and fetches from your origin only on a miss, through a second tier of Regional edge caches.
- The cache key decides what counts as the same object. Every header, cookie or query string you add to it lowers the hit ratio.
- A cache policy controls the key and TTLs. An origin request policy forwards extra values to the origin without adding them to the key.
- Let the origin speak with Cache-Control, within the behavior's minimum and maximum TTL. A minimum above zero overrides no-store.
- Prefer versioned filenames to invalidation. Invalidation cannot reach copies already in browsers.
- Never cache a personalized response under a key that does not identify the user. That serves one person's data to another.
Distance and repetition
A user in Sydney asking a server in Ireland for a logo waits for light to cross the planet and back several times. And the next thousand users in Sydney ask for the same logo. A content delivery network keeps a copy close to them. Caching and CDNs covers the general idea; this is how AWS’s one behaves.
CloudFront has edge locations around the world. A viewer is routed to a nearby one. If it holds a fresh copy, that’s a hit. If not, the request goes to a larger Regional edge cache, and then to your origin. Think of a local shop, a regional warehouse and the factory. Even uncacheable requests gain: the user’s TLS handshake ends nearby and the long haul uses AWS’s network over warm connections.
Distributions, origins and behaviors
A distribution is your CDN configuration with a hostname like d111111abcdef8.cloudfront.net, to which you can add your own names. It has origins, such as an S3 bucket, a load balancer or any HTTP server, and cache behaviors, which match path patterns to an origin and a set of rules:
| Path pattern | Origin | Caching |
|---|---|---|
/assets/* |
S3 | long |
/api/* |
load balancer | disabled |
default (*) |
S3 | short |
Behaviors are checked in order, and the default catches the rest.
The cache key
The cache key is how CloudFront decides two requests want the same thing. By default it’s essentially the host and path. A cache policy can add headers, cookies or query strings, and sets TTLs. Each addition multiplies the number of variants: put a session cookie in the key and every user gets a private cache that never hits.
An origin request policy is the other half. It lists what to forward to the origin without keying on it. Rule of thumb: in the key only what changes the response; forwarded only what the origin needs.
AWS provides managed policies. CachingOptimized suits static files. CachingDisabled suits APIs and personalized pages.
How long
Your origin sends Cache-Control. CloudFront honours max-age and s-maxage (the latter for shared caches only), clamped between the policy’s minimum and maximum TTL. With no header, the default TTL applies: 24 hours unless you change it. stale-while-revalidate and stale-if-error let it serve an old copy while refreshing or while the origin is failing.
One trap: with a minimum TTL above zero, CloudFront caches for at least that long even when the origin says no-store or private. CachingOptimized has a minimum of one second, so keep it away from per-user responses.
Removing things early
An invalidation tells edges to drop paths, or cache tags. The first 1,000 paths a month are free and /assets/* counts as one. But it takes time to spread and does nothing about browser caches. The better design is versioned names, such as app.3f9a1c.js, cached for a year, with a short-lived or no-cache HTML file pointing at them. A deploy is then a new name, and rollback is the old one.
Protecting the origin
If users can reach the origin directly, they skip your cache and your firewall rules. For S3, use origin access control, shown in the static site lesson. For load balancers, VPC origins let CloudFront reach a private one, or restrict the security group to CloudFront’s managed prefix list. For private content, signed URLs or signed cookies limit who may fetch. AWS WAF attaches to the distribution.
Code at the edge
CloudFront Functions run tiny JavaScript on viewer requests and responses: rewrites, redirects, header changes, cache key cleanup, token checks. They run in under a millisecond. Lambda@Edge runs Node.js or Python with more time, memory and network access, and can act on origin requests too. Reach for the small one first.
Paying
Pay-as-you-go CloudFront includes 1 TB out and 10 million requests a month free, and transfer from AWS origins to CloudFront isn’t charged. There are also flat-rate plans, including a free one with smaller allowances (1 million requests, 100 GB) that bundles WAF and DNS (AWS pricing pages, November 2026).
About the video
The embed is a clip from AWS’s re:Invent 2024 talk “Amazon CloudFront: Enhancing web performance one HTTP request at a time” (CDN308), 12:40 to 29:49. The chapters in the span are edge security and L1 caching, caching strategies, edge compute functions, Regional edge cache and security, and the start of origin connectivity. It assumes you know what a CDN is and predates the flat-rate plans. I judged it from chapters and description; there’s no audio-language flag, though title and captions are English.
Nothing was run: no local tool here emulates edge caching. Behaviour, TTL rules and limits come from the CloudFront developer guide, checked in November 2026.
Check yourself
Your hit ratio is near zero for images. The cache policy includes all headers, cookies and query strings. Why does that matter?
Each distinct combination makes a separate cache key, so almost every request is unique. Remove everything from the key that doesn't change the image.
The origin needs the User-Agent for logging but it doesn't change the response. Where do you configure it?
In the origin request policy, so it's forwarded on misses without becoming part of the cache key.
You deploy a new app.js under the same name and some users still get the old one after an invalidation. Why?
Their browsers cached it according to the Cache-Control header. An invalidation only clears CloudFront. A new filename per build avoids the problem.
An API sends Cache-Control private, yet two users see each other's data through CloudFront. What setting might cause it?
A cache policy with a minimum TTL above zero, which makes CloudFront cache despite private or no-store. Use CachingDisabled for personalized paths.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.