"A fast server means a fast website for everyone." Not if your users are far away. Every request still has to travel to your server and back, and distance adds delay that no amount of CPU can remove. Amazon CloudFront fixes that by moving your content closer to the people asking for it.
What CloudFront is
CloudFront is AWS's content delivery network (CDN). It keeps copies of your content in edge locations: hundreds of points of presence in cities around the world. When someone requests a file, they're served from an edge location near them instead of from your Region.
You set it up as a distribution: a CloudFront address (or your own domain) that sits in front of your content.
Origins: where the content really lives
The origin is the source of truth. CloudFront fetches from it when it doesn't have a copy. Common origins:
- an Amazon S3 bucket with images, scripts, downloads or a static website
- an Application Load Balancer or EC2 servers running your application
- any other HTTP server, inside or outside AWS
One distribution can have several origins, with rules such as "/images/ goes to S3, /api/ goes to the load balancer."
How caching works
- The first user near an edge location requests a file. The edge doesn't have it, so it fetches it from the origin and keeps a copy.
- The next users near that edge get the copy straight from the cache. The origin isn't contacted.
- When the copy expires, the edge checks with the origin again.
You control how long copies live with cache settings (the TTL, time to live) and with the caching headers your origin sends.
What if a file changes before it expires? You have two options:
- Invalidate it, which tells CloudFront to drop the cached copies.
- Better, give the new version a new name, such as
app.v2.js. Users get the new file immediately, old copies simply age out.
A common mistake is caching everything with a long lifetime, including pages that differ per user. Decide what belongs in the cache key (for example certain headers, cookies or query strings) so one person never receives another person's page.
More than static files
CloudFront also helps content that can't be cached:
- Dynamic requests enter at the nearest edge and travel to your origin over the AWS network, which is usually faster and steadier than the public internet.
- It terminates HTTPS at the edge, with free public certificates from AWS Certificate Manager.
- It absorbs common network attacks at the edge, and you can attach AWS WAF rules to filter malicious requests before they reach your servers.
Where AI fits
AI applications bring two delivery problems: answers are slow to generate, and they arrive in pieces.
- Streaming. CloudFront can pass a response through as it is produced, so the user sees a model's answer appear word by word instead of waiting for the whole thing. A common pattern is CloudFront in front of a Lambda function URL with response streaming enabled.
- Caching. Some AI output is the same for everyone: a generated product description, a summary of a public document, an image. Cache those at the edge and you pay for the model once instead of on every view. This works for cacheable requests, typically GET requests with the same URL; personalised or one-off answers should not be cached.
Quick recap
- CloudFront is a CDN: copies of your content at hundreds of edge locations.
- The origin (S3, a load balancer, any HTTP server) holds the real content.
- The edge caches it close to users; control lifetime with TTLs, and prefer new file names over invalidations.
- For AI, stream answers through the edge and cache the ones that repeat.