Guide

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

StepConceptWhat it answers
1Speed of light and round tripsWhy does distance set a latency floor?
2CDN routing and cache hierarchyHow does a user reach a nearby PoP, and how do misses stay cheap?
3Cache control and cache keysWhat does the CDN store, and for how long?
4Purging and versioned URLsHow do you change content already in the cache?
5CDN hit ratio and costWhat does a hit ratio save you, in dollars and origin load?
6Edge compute and securityWhat 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 private or no-store for 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-Control headers 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.

What Is A CDN? How Does It Work?ByteByteGo
Cloudflare: What is a CDN?cloudflare.com MDN: Cache-ControlThe HTTP Cache-Control header holds directives (instructions) in both requests and responses that control caching in browsers and shared caches (e.g., Proxies, CDNs).MDN Web 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 High Performance Browser Networking (Ilya Grigorik)High Performance Browser Networking (O'Reilly)High Performance Browser Networking