Traffic and messaging
CDNs and the edge
Serve content from points of presence near users. A CDN cuts latency, absorbs attacks and cuts bandwidth costs, and its cache keys, headers and purge rules decide how well it works.
Light travels about 200,000 km per second, and that sets a minimum delay between users and servers. A content delivery network (CDN) moves content, and increasingly compute, to hundreds of locations close to users. It also absorbs attacks, cuts the bandwidth bill and shields your origin from traffic spikes. Nearly every large website and app sits behind one.
A CDN is a geographically distributed cache with a lot of networking attached. If you already know caching, most of the ideas here are the same ones applied to the edge of the network.
The pages below go in order. First, why distance matters and how a CDN routes requests. Then how to control what gets cached, how to change it, what it costs and what the edge can do.
Concepts on this topic
| Step | Concept | What it answers |
|---|---|---|
| 1 | Speed of light and round trips | Why does distance set a latency floor? |
| 2 | CDN routing and cache hierarchy | How does a user reach a nearby PoP, and how do misses stay cheap? |
| 3 | Cache control and cache keys | What does the CDN store, and for how long? |
| 4 | Purging and versioned URLs | How do you change content already in the cache? |
| 5 | CDN hit ratio and cost | What does a hit ratio save you, in dollars and origin load? |
| 6 | Edge compute and security | What can run at the edge, and how does it defend you? |
Common mistakes
- Serving only static files. Put everything behind the CDN, including APIs. Even uncached requests get TLS termination near users and reused connections.
- Purging everything at peak. It sends all traffic to the origin at once.
- Letting personal data be cached publicly. Use
privateorno-storefor anything per-user. - Ignoring hit ratio. A deploy that adds a header to the cache key can drop hit ratio from 95% to 30% and quadruple origin load. Watch it as a first-class metric.
Design rule of thumb
In a read-heavy design, such as a URL shortener, an image feed or video, the CDN shows up early. Say what goes through it, what its TTLs and keys are, how you invalidate, and roughly what hit ratio to expect. Then use that hit ratio to size the origin.
Key takeaways
- Distance sets a latency floor, and a CDN is how you move content closer.
- Tiers and an origin shield keep a new object's origin fetches to roughly one per object.
- Cache keys and
Cache-Controlheaders decide hit ratio, and a missing key part can leak private data. - Fingerprinted URLs and tag purges let content stay cached and still change quickly.
- Egress and origin load both scale with the miss rate.
Further learning
ByteByteGo's short animated explainer covers PoPs, anycast and caching before you read the docs.
Fastly: Working with surrogate keysSurrogate keys are unique identifiers that you assign to groups of content for processing. While there are many use cases for surrogate keys…docs.fastly.com
Related
Caching
Where to put a cache, how to read and write through it, how to size it from hit-rate math, and how to keep it from serving stale data or melting down.
Case study: video streaming
YouTube and Netflix-style delivery, with resumable uploads, parallel transcoding into a bitrate ladder, HLS and DASH adaptive streaming, and CDN economics.
DNS
How names become addresses, which record types matter, how caching and TTLs shape change speed, and why DNS failures look like outages of everything else.