AlgoViz

CDN architecture and design

medium 32 min

How 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

0:00 / 3:09

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

  1. September 1997

    RFC 2187 describes cooperating web caches that ask each other before the origin.

  2. December 2006

    RFC 4786 writes down how to operate services on anycast addresses.

  3. November 2008

    Amazon opens CloudFront.

  4. 2011

    Netflix begins Open Connect, which fills its caches ahead of demand.

  5. 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.

  6. 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.

Step 1 of 3

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?

The milk float round
FARMfridge 1fridge 2fridge 3Three trips to the farm

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

Users in Tokyo and London are led to the Tokyo edge and the London edge. On a miss both edges ask the origin shield. Edges and shield belong to the CDN. The shield asks the load balancer of our system only on its own miss. The load balancer passes the request to the app, which reads from a bucket.
Two layers of cache. Only the shield knows the way to the origin.

Four components

ComponentWhat it doesRemember
Edge servers in PoPsServe users, end TLS, cache, apply security rulesMany and close
OriginHolds the truth, answers dynamic requestsShould accept traffic only from the CDN
Origin shieldSecond cache, collapses missesOptional, one per origin
DNS and routingChoose a near and healthy edgeNear 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

Rorigin=R (1−hedge)(1−hshield)=30,000⋅0.10⋅0.20=600 per secondR_{origin} = R\,(1-h_{edge})(1-h_{shield}) = 30{,}000 \cdot 0.10 \cdot 0.20 = 600\text{ per second}
Assumed: 10,000 requests a second on average, peak 3x, 90% hits at the edges, 80% of the rest at the shield. Without the shield: 3,000.

Many misses, one fetch

Request collapsing. CloudFront describes Origin Shield in this way: as few as one request per object reaches the origin.

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. 1

    A user asks

    The edge looks in its cache: a miss.

  2. 2

    The edge asks

    The shield or the origin is asked for the file.

  3. 3

    Keep and serve

    The file is stored and sent.

  4. 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. 1

    Upload

    You send new content to the CDN's storage.

  2. 2

    Distribute

    Copies travel to chosen locations, normally asynchronously.

  3. 3

    Confirm

    Check that the important locations have it before you announce it.

  4. 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

106 files⋅0.5 MB=500 GB500 GB⋅200 PoPs=100 TBhottest 5%:5 TB10^6 \text{ files} \cdot 0.5\text{ MB} = 500\text{ GB} \qquad 500\text{ GB} \cdot 200\text{ PoPs} = 100\text{ TB} \qquad \text{hottest }5\%: 5\text{ TB}
Pushing everything everywhere takes 100 TB. If 5% of the files get nearly all reads, pull keeps about that share.

Pull or push?

Millions of files with unpredictable access: pull. Video, app and game releases: 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

GeoDNSAnycastURL chosen by the app
DecidesDNS serverInternet routingYour service
SeesResolver or subnetNetwork pathUser, file, load
FailoverAfter the TTLWhen routes adaptAt the next request
ExampleA name per regionOne address worldwideVideo players
They combine: anycast for the DNS servers, GeoDNS for the answer, chosen URLs for video. More in global and local load balancing.

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.

PartAdd it whenDanger
QueryIt changes the contentTracking words create a copy per visitor
Accept-EncodingYou store gzip and Brotli versionsKeep to a few values
Language or deviceThe answer differsNormalize first: five languages, not thousands of header texts
CookieAlmost neverOne copy per user, no hits
AuthorizationNever in a shared cache, unless designed for itLeaks
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

HeaderMeaning
max-age=86400Fresh for 24 hours, in all caches
s-maxage=3600Fresh for 1 hour in shared caches, instead of max-age
CDN-Cache-ControlOnly for CDNs that support it
stale-while-revalidate=60May be served stale for 60 s while it is refreshed
no-cacheKeep it, but check before each use
Vary: Accept-EncodingOne copy per encoding
ETagA 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. 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. 2

    Filled in advance

    Netflix describes proactive, directed caching: new files are added nightly, chosen by the control plane.

  3. 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. 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

ProviderNetwork, as they describe itTypical reasons
CloudFront750+ PoPs, 1,140+ embedded in providers, 15 regional cachesAWS integration, Origin Shield, code at the edge
Cloudflare348 cities, every service in every data centerFree plan, protection, Workers
FastlyFewer, more powerful PoPsVCL and Wasm at the edge, fast purge
AkamaiVery widely spreadMedia delivery, enterprise security
Read on the CloudFront, Cloudflare and Fastly pages in November 2026. A count of PoPs says little about the delay of your users: measure it.

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.

Sources