Sub topic 1
How a CDN and edge caching work
Prerequisites
This chapter assumes you know the following:
- Basic Amazon S3 concepts: buckets, objects, and keys.
- HTTP requests, responses, and status codes.
- How to run AWS CLI commands. This chapter only needs
curl.
What a CDN does
A content delivery network (CDN) is a set of servers spread across many geographic locations. It keeps copies of your content close to the people who request it. Amazon CloudFront is the AWS CDN.
Without a CDN, every viewer's request travels to your S3 bucket in one AWS Region. A viewer far from that Region waits longer because of network distance. Every request also counts against the bucket.
With a CDN, a viewer's request goes to a nearby server called an edge location. If that edge location already holds a copy, it answers immediately. Otherwise it fetches the object from your bucket, stores a copy, and returns it.
Key terms
- Viewer: the browser or app that makes the request.
- Edge location: a CloudFront site that caches content near viewers.
- Origin: the source of truth for your content. In this tutorial the origin is an S3 bucket.
- Cache hit: the edge location has a fresh copy and answers without contacting the origin.
- Cache miss: the edge location has no usable copy and must fetch one from the origin.
The request flow
The first request for an object at an edge location is a miss. Later requests at that location are hits until the copy expires.
Each edge location keeps its own cache. A hit in one city does not warm the cache in another. CloudFront also places intermediate caching layers between edge locations and the origin, so many misses never reach S3. You do not manage these layers, but they explain why origin traffic is lower than the number of edge locations might suggest.
How long a copy stays valid
A cached copy is fresh for a limited time, called its time to live (TTL). After the TTL ends, the edge location treats the copy as stale. It then asks the origin for a new version before serving it again. If the origin reports the object is unchanged, the edge location keeps its copy and resets the timer.
The origin influences the TTL through headers such as Cache-Control. Later chapters cover how to set TTLs and how to publish updates. For now, remember the trade-off:
| Longer TTL | Shorter TTL |
|---|---|
| More cache hits and lower latency | Updates reach viewers sooner |
| Fewer requests to S3 | More requests to S3 |
| Viewers may see old content longer | Lower hit ratio |
Seeing a hit and a miss
CloudFront adds an X-Cache response header that reports what happened. Run this command twice against a distribution that serves your content. Replace the domain with your own.
curl -sI https://d111111abcdef8.cloudfront.net/logo.png | grep -i -E "^(x-cache|age)"The first response typically shows X-Cache: Miss from cloudfront. A later one shows X-Cache: Hit from cloudfront and an Age header. Age is the number of seconds the copy has been in the cache.
What caching does and does not fix
Caching speeds up repeated reads of content that many viewers share, such as images, scripts, and stylesheets. It helps less for content that is unique per request or that changes constantly, because the edge location rarely holds a reusable copy.
Exercise
A viewer in Tokyo requests an object for the first time through a distribution whose origin is a bucket in Ohio. A second viewer in Tokyo requests the same object a minute later, and the TTL has not expired. Which request is a cache miss, and which one reaches S3?
Show answer
The first request is a miss, and it is the only one that reaches S3. The edge location stores the object, so the second request is a cache hit answered from the edge cache.