CDN architecture and design
medium 32 minHow a CDN is built: edge locations, a shield in front of the origin, routing that finds a good edge, cache keys, and the choice between pulling content on demand and pushing it in advance.
Video lesson · Chapter 1 of 5
Pull a Tokyo photo on its first request
Edge, shield, origin in 80 seconds
1:20Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
The introduction showed a CDN as one box. Inside there are four parts: edge locations that serve users, one or more higher tiers that protect the origin, a routing system that sends users to a good edge, and a control plane that distributes settings, certificates and purges.
Two decisions shape the rest. How content gets in: on demand (pull) or in advance (push). How users get there: by DNS answers, by one address announced from many places, or by the application choosing a URL.
Why it exists
One layer of caches is not enough. With 200 edges, a file that everybody reads is fetched 200 times, once per edge. A rarely read file misses almost everywhere. A second layer brings these requests together, so the origin sees few of them.
Routing decides what nearest means. The wrong answer sends a user across an ocean and wastes every other effort. And when a location fails, routing is what moves its users.
A short history
September 1997
RFC 2187 describes cooperating web caches that ask each other before the origin.
December 2006
RFC 4786 writes down how to operate services on anycast addresses.
November 2008
Amazon opens CloudFront.
2011
Netflix begins Open Connect, which fills its caches ahead of demand.
May 2016
RFC 7871 describes EDNS Client Subnet: resolvers may pass on part of the user's address so that DNS answers fit the user.
June 2022
RFC 9213 defines CDN-Cache-Control, a header only for CDNs.
Words to know
- PoP
- Point of presence: a location with edge servers, storage and links to networks.
- Origin shield
- A cache tier between the edges and the origin.
- Tiered cache
- Edges ask an upper cache, and only upper caches ask the origin.
- GeoDNS
- DNS that gives different answers depending on where the question comes from.
- Anycast
- One IP address announced from many places. Routing leads each packet to one of them.
- Cache key
- The parts of a request that decide if two requests may share a copy.
- Request collapsing
- Many misses for one object become one request to the next layer.
How it works
Step through the idea, or press Play.
Route. DNS or anycast picks an edge
Understand it
Start here
Farm, depot and corner fridge
Imagine milk that comes from one farm. Three corner shops have fridges. How should the milk get there?
Farm, depot and fridge are origin, shield and edge. A pizza chain with one kitchen and many branches has the same shape.
Architecture
The whole picture
Edges, shield, origin
Four components
| Component | What it does | Remember |
|---|---|---|
| Edge servers in PoPs | Serve users, end TLS, cache, apply security rules | Many and close |
| Origin | Holds the truth, answers dynamic requests | Should accept traffic only from the CDN |
| Origin shield | Second cache, collapses misses | Optional, one per origin |
| DNS and routing | Choose a near and healthy edge | Near in network terms |
Inside one PoP
Load balancer
Spreads connections over the servers.
Proxy servers
Programs such as nginx or Varnish speak HTTP.
Cache storage
Memory for the hottest files, SSD for the rest.
Monitoring
Health checks, logs and metrics.
Inside a PoP a file should live on one or two servers, not on all. Consistent hashing of the cache key picks the server, so the PoP behaves like one large cache.
Tiers
Why a shield helps
Two chances to hit
Many misses, one fetch
CloudFront also has regional edge caches between its locations and the origin. Cloudflare calls the idea Tiered Cache, Fastly calls it shielding. Place the shield near the origin, because the distance it adds should be small for it and large for nobody.
Content in
Pull: fetch on demand
How pull works
- 1
A user asks
The edge looks in its cache: a miss.
- 2
The edge asks
The shield or the origin is asked for the file.
- 3
Keep and serve
The file is stored and sent.
- 4
Next user
A hit until the copy expires or is evicted.
Good
- Stores only what is asked for
- Little to manage: set headers
- Great for the long tail
- Blogs, shops, social media, user uploads
Costs
- The first request is slow
- Many misses together load the origin
- Expiry brings the origin back in
- A new PoP starts cold
Content in
Push: place it in advance
How push works
- 1
Upload
You send new content to the CDN's storage.
- 2
Distribute
Copies travel to chosen locations, normally asynchronously.
- 3
Confirm
Check that the important locations have it before you announce it.
- 4
Serve
The first user already gets a hit.
Good
- No cold start for planned content
- Predictable speed
- Origin calm on release day
- Films, game patches, app releases
Costs
- Storage for every copy
- You manage uploads and removals
- Wasted on unpopular files
- Distribution takes time
What storing everything costs
Pull or push?
Users in
Routing with GeoDNS
The DNS server of the CDN looks at where the question comes from and returns the address of a close edge. It sees the resolver, not the user. If a user in Lima asks a resolver in Miami, the answer fits Miami. EDNS Client Subnet lets a resolver pass on part of the user's address.
Good
- Simple and works everywhere
- Full control: weights, health, cost
- Can use measured delays
Costs
- Follows the resolver's place
- Answers are cached for their TTL
- Failover waits for that TTL
Users in
Routing with anycast
Every location announces the same IP address with BGP. Routers on the way choose one path, so a user reaches the location that is closest by routing rules. When a location stops announcing, traffic moves as soon as routing has adapted, with no DNS change. Cloudflare answers proxied names with anycast addresses.
Good
- Works below DNS
- Quick failover
- Attack traffic is spread over many places
Costs
- BGP knows paths, not delay or load
- A route change can break open connections
- Harder to steer by hand
Side by side
| GeoDNS | Anycast | URL chosen by the app | |
|---|---|---|---|
| Decides | DNS server | Internet routing | Your service |
| Sees | Resolver or subnet | Network path | User, file, load |
| Failover | After the TTL | When routes adapt | At the next request |
| Example | A name per region | One address worldwide | Video players |
A complete request
Correctness
The cache key
The default key is usually host, path and query. Everything that changes the answer must be in the key. Everything that does not should be out.
| Part | Add it when | Danger |
|---|---|---|
| Query | It changes the content | Tracking words create a copy per visitor |
| Accept-Encoding | You store gzip and Brotli versions | Keep to a few values |
| Language or device | The answer differs | Normalize first: five languages, not thousands of header texts |
| Cookie | Almost never | One copy per user, no hits |
| Authorization | Never in a shared cache, unless designed for it | Leaks |
Vary: Accept-Encoding tells caches that the answer depends on that header. Not every CDN honours every Vary value, so check yours.Does this part belong in the key?
Freshness
Headers for two audiences
| Header | Meaning |
|---|---|
max-age=86400 | Fresh for 24 hours, in all caches |
s-maxage=3600 | Fresh for 1 hour in shared caches, instead of max-age |
CDN-Cache-Control | Only for CDNs that support it |
stale-while-revalidate=60 | May be served stale for 60 s while it is refreshed |
no-cache | Keep it, but check before each use |
Vary: Accept-Encoding | One copy per encoding |
ETag | A fingerprint for conditional requests |
s-maxage brings the rule that a shared cache must not serve it stale. Cloudflare therefore ignores stale-while-revalidate next to it.How a copy is removed early is its own topic: CDN cache invalidation. What to watch in daily work is in CDN operations.
Case study
Netflix Open Connect
- 1
Own appliances
Cache servers stand in internet exchanges and inside provider networks. Qualifying providers get the hardware free and give power, space and a connection.
- 2
Filled in advance
Netflix describes proactive, directed caching: new files are added nightly, chosen by the control plane.
- 3
Chosen by the service
A steering service picks appliances by file availability, health and network distance, and returns their URLs to the player.
- 4
Small duty
An appliance reports its status and serves files. It stores no viewing history or member data.
Choosing
Providers and design checks
What differs between providers
| Provider | Network, as they describe it | Typical reasons |
|---|---|---|
| CloudFront | 750+ PoPs, 1,140+ embedded in providers, 15 regional caches | AWS integration, Origin Shield, code at the edge |
| Cloudflare | 348 cities, every service in every data center | Free plan, protection, Workers |
| Fastly | Fewer, more powerful PoPs | VCL and Wasm at the edge, fast purge |
| Akamai | Very widely spread | Media delivery, enterprise security |
Do
- Headers for every path
- Hashed file names
- A shield for busy origins
- Collapse identical misses
- Measure hit ratio per path
Avoid
- Personal pages in a shared cache
- Version only in a query that some rule ignores
- TTLs of a few seconds without stale rules
- Caching errors for long
- An open origin
Summary
The whole topic in one minute.
A CDN is edges for distance, a shield for the origin, routing for the choice of edge, and a control plane for settings. Pull suits many files with unknown demand, push suits few large files with known demand, and real systems mix both.
The cache key decides correctness and hit ratio at once: too few parts mix up answers, too many parts split the cache. Collapse identical misses, serve stale while refreshing, and keep the origin reachable only through the CDN.
- Edge for distance, shield for the origin, routing for the choice.
- Pull for the unknown, push for the planned.
- GeoDNS sees resolvers, anycast sees paths.
- The cache key is a correctness decision.
- Two tiers multiply: misses become misses of misses.