A request from the browser
When a browser requestshttps://assets.example.com/app.js, the path looks like this:
- DNS directs the hostname to the CloudFront distribution.
- The viewer establishes TLS with a nearby CloudFront edge location.
- CloudFront chooses the matching cache behavior.
- CloudFront calculates a cache key from configured request fields.
- A cache hit returns the stored object from the edge.
- A cache miss sends an origin request.
- The origin response returns to CloudFront and may be cached before it reaches the viewer.
The cache key decides reuse
The cache key answers: can these two requests share one cached response? The URL path is part of the key. You can also include selected query strings, headers, and cookies. Including more fields creates more cache variants and usually lowers the hit ratio. For example, if a static image response never changes by cookie, forwarding every cookie can create unnecessary cache entries. If an API response changes by theAccept-Language header, excluding that header can serve the wrong language.
CloudFront separates cache policies from origin request policies. A value can be forwarded to the origin without becoming part of the cache key, but use this carefully. If the origin changes the response based on a forwarded value that is absent from the key, viewers may receive a response generated for someone else.
Cache behaviors and origins
A distribution can have multiple cache behaviors. Path patterns choose which behavior handles a request.Private S3 origins
A secure static-site design keeps the S3 bucket private. CloudFront uses Origin Access Control to sign origin requests, and the bucket policy allows reads from the specific distribution. The viewer can fetch the object through CloudFront but cannot bypass it with a public S3 URL. This keeps the delivery policy, TLS, caching, and optional AWS WAF controls on one path. Springwinter static websites use this model: a private S3 bucket stores the build output and CloudFront serves it globally. See static websites for the deployment flow.TTLs, revalidation, and invalidation
The time to live determines how long CloudFront can reuse an object before checking the origin again. Cache headers from the origin and the behavior’s minimum, default, and maximum TTL settings work together. Use long lifetimes for versioned assets such asapp.8f31c.js. A new deployment publishes a new name, so old and new clients can safely use different files. Use shorter lifetimes or revalidation for HTML documents that point to those assets.
An invalidation tells CloudFront to remove matching cached paths before they expire. Invalidations are useful for urgent changes, but versioned file names are more predictable for routine deployments.
Dynamic requests and security
CloudFront can accelerate dynamic traffic by reusing connections, terminating TLS near viewers, and routing over the AWS network even when a response is not cached. It can also integrate with AWS WAF, signed URLs, signed cookies, geographic restrictions, and edge functions. None of these features removes the need to secure the origin. Restrict direct origin access where possible, validate application authorization, and avoid caching personalized responses under shared keys.What to measure
A high cache hit ratio usually reduces origin load and latency, but it is not the only goal. Track:- Viewer latency and error rates
- Cache hit ratio by behavior
- Origin latency and errors
- Bytes transferred to viewers
- Invalidation frequency
- Requests and transfer cost
Frequently asked questions
How does Amazon CloudFront work?
How does Amazon CloudFront work?
Amazon CloudFront receives a request at an edge location, calculates a cache key, and returns a cached response when available. On a cache miss, CloudFront forwards the request to an S3, load balancer, API, or HTTP origin and can cache the response.
What is a CloudFront cache key?
What is a CloudFront cache key?
A CloudFront cache key identifies response variants using the URL path and configured query strings, headers, and cookies. Smaller correct keys improve cache reuse. Missing a value that changes the origin response can expose incorrect or personalized content to other viewers.
Should I invalidate CloudFront after every deployment?
Should I invalidate CloudFront after every deployment?
Prefer content-hashed file names and long TTLs for static assets. Publish new names when assets change and use shorter caching for HTML entry points. Use invalidations for urgent corrections or stable paths that must refresh before their normal expiration.