Every request to the market now crosses to us-east-1, however far away the customer is. A content delivery network keeps copies of responses in edge locations in hundreds of cities, so most requests are answered close by. AWS's is CloudFront.
A distribution
A distribution is a domain, d….cloudfront.net, in front of an origin: a bucket, a website, a load balancer, any web server. Put the market's S3 website behind one (SITE=monster-market-4utdi4.s3-website-us-east-1.amazonaws.com):
DIST=$(aws cloudfront create-distribution --origin-domain-name $SITE --query Distribution.Id --output text)CloudFront talks to a website origin over plain HTTP; viewers still reach CloudFront over HTTPS. With one origin, the distribution has one default behaviour, cached with CachingOptimized. Every change spreads to all the edges: InProgress for a few minutes, then Deployed.
aws cloudfront get-distribution --id $DIST --query "Distribution.[Status,DomainName]" --output textHits and misses
Keep the domain:
CF=$(aws cloudfront get-distribution --id $DIST --query Distribution.DomainName --output text)curl -i prints the headers with the body:
curl -si https://$CF/The first request for a path is X-Cache: Miss from cloudfront: the edge asks the origin and keeps the response. The next ones are Hit from cloudfront, with Age counting how long the copy has been kept. The edge keeps it for the TTL: the origin's Cache-Control: max-age if it sends one, within the limits of the cache policy.
| Managed cache policy | Keeps a copy | For |
|---|---|---|
| CachingOptimized | a day by default, from 1 second to a year | files that rarely change |
| CachingDisabled | never | APIs, anything personal |
The cache key is the path, plus whatever the policy adds (query strings, headers, cookies). Every extra part means fewer hits.
Behaviours
A behaviour sends a path pattern to an origin, with a cache policy and a viewer protocol policy: allow-all, redirect-to-https or https-only. Patterns are checked in order, like listener rules; the default behaviour takes the rest.
Clearing the cache
A copy stays until its TTL ends, even after the file changes. An invalidation removes paths from every edge:
INV=$(aws cloudfront create-invalidation --distribution-id $DIST --paths "/*" --query Invalidation.Id --output text)aws cloudfront get-invalidation --distribution-id $DIST --id $INV --query Invalidation.Status --output textInProgress, then Completed. For files that change often, a new name per version (app.v2.css) avoids invalidations.
Not to be confused with Global Accelerator: two fixed IP addresses that carry any TCP or UDP traffic over AWS's network to the nearest healthy region, caching nothing.