System Design Bits

Support the project

System Design Bits is free and always will be. If it helped, a coffee helps back.

Buy Me a Coffee QR codeBuy me a coffee

Concept 7 · Scaling and Trade-offs·21 min read

Content Delivery Networks (CDNs)

Why moving the bytes closer works wonders for content everyone shares, and almost nothing for content nobody shares.

Big Idea

A CDN keeps copies of your content sitting near the people who ask for it, so distance stops being the thing that decides how fast your site feels. Whether that pays off at all comes down to how many people one copy can answer: enormously, when the bytes are identical for everybody, and close to nothing when every request deserves a different reply.

Why This Exists

One Server Cannot Be Near Everybody

▸

A single origin server, however powerful, is physically far from most of its users, and the speed of light itself puts a floor on how fast a round trip to a distant server can be. No amount of server tuning shortens the distance.

▸

A popular video or a viral image generates an enormous number of near-identical requests for the exact same bytes, and serving every one of them from one origin spends bandwidth and compute on work the origin already did once.

▸

A single point of origin is also a single point of failure and a single bottleneck under load. A launch, a live event, or an attack aimed at that one server can take down every user's experience at once.

Think Of It Like

One Press, Or A Shop In Every Town

Picture a single printing press serving an entire country. For a poster everybody wants, that is an absurd arrangement: each reader mails in an order, the press runs off one more identical sheet, and the post carries it back across the country. Send the master to a print shop in every town instead and the whole problem dissolves, because one plate can serve a queue of any length and nobody is waiting on the mail.

Now make it a certificate with the reader’s own name on it. No local shop can print what it has never seen and could not have guessed, so every order travels back to the one press that holds the records. The shops are still there, still close by, still cheap to run, and for this job they contribute nothing except one more counter to queue at on the way.

The Shape Of It

Origin Pull: One Miss, Then Hits

The first user in a region pays for everyone who follows

Origin pull: cache miss, then cache hitUserRegion A, firstCDN edge nodeRegion A PoPOrigin serverone region, far awayUserRegion A, minutes later1 · request, miss2 · fetch and cache3 · request, hitorigin never hearsabout request 3
Everything good about a CDN lives in the gap between steps 1 and 3. Step 1 is the full price, paid once per region per object. Step 3 is what you actually bought, and the more times it repeats before the TTLTime to live. How long an edge node may keep serving its cached copy before it has to go back to the origin and re-check. A longer TTL means more requests answered locally and a higher chance of serving something out of date; a shorter one is the reverse. It is the one dial that trades freshness against hit ratio. expires, the better the deal was.

Anatomy

The Six Pieces

Edge node (PoP)

The server near the user that answers the request. How many there are, and how close they are, is most of what you are buying.

Origin

The one server or cluster that holds the authoritative copy. The edge is a copy of it, never the source of truth.

Cache key

What an edge node considers 'the same request'. Usually the URL, optionally plus specific headers, and getting it wrong caches one user's response for another.

TTL

How long an edge node may serve its copy before re-checking the origin. The dial between freshness and hit ratio.

Cache hit ratio

The fraction of requests answered from the edge rather than forwarded. The single number that tells you how much the CDN is actually doing.

Invalidation

The way content is removed before its TTL expires: an explicit purge, or a new URL. Both work, only one is instant everywhere.

The cache key is the piece people underthink. It decides which requests count as the same request, and a key that ignores something the response actually varied on, a cookie, an Accept-Language header, an auth token, does not produce a slow site. It produces one user being served another user’s page.

The Concept

How The Edge Actually Works

1
A network of edge servers, not one server

A CDN is a distributed network of edge servers, also called points of presence, or PoPsPoint of presence. One of a CDN’s physical locations: a rack of caching servers in a data centre or inside an ISP’s network, holding copies of content for the users around it. A provider’s PoP count and placement is most of what makes one CDN faster than another in a given country., spread across many geographic regions. Each one holds cached copies of content close to the users in that region, so a request is served from a nearby node instead of crossing an ocean to the origin.

2
Getting to the nearest edge is the network's job

You never pick a PoP, and the URL never changes. Two mechanisms do it for you: DNS that answers with a different address depending on where the question came from, and anycast, where many PoPs announce the same IP and the network delivers to whichever is closest. It is why the same link loads from a machine in Lisbon for one reader and one in Lima for another.

3
Origin pull, or lazy caching

The CDN does not have the content until the first request for it arrives at an edge node. That first request is forwarded to the origin, the response is cached at the edge, and every subsequent request in that region is served from the cache until it expires. No upfront setup, but the first user in each region pays full origin latency.

4
Origin push, or pre-loading

The origin proactively uploads content to edge nodes ahead of any request, so every user gets a cached response from the very first request. This suits content you know in advance will be popular everywhere, like a scheduled major release, at the cost of pushing data to regions that may never ask for it.

5
Static assets are the best case

Images, CSS, JavaScript bundles, fonts and downloadable files are the CDN's ideal workload: the exact same bytes are correct for every single user, so one cached copy serves unlimited requests until the content itself changes, giving a near-perfect cache hit ratio.

6
Video is the same idea at a larger scale

Video files are large, popular ones are requested repeatedly by huge numbers of viewers, and CDNs split video into small chunks so a player requests just the next few seconds from the nearest edge. That also lets quality adapt mid-stream without re-fetching the whole file.

7
Personalized and dynamic responses are the worst case

An account dashboard, a search result unique to one query, an authenticated API call: the response depends on who is asking and what they asked, so there is effectively nothing shared to cache. Most such requests still reach the origin, meaning the CDN adds a network hop without adding a cache hit.

8
TTL and invalidation matter just as much here

A long TTL means a stale asset, an outdated CSS file after a deploy, can keep being served for a while. This is why deployed static assets are given content-hashed filenames: a changed file becomes a genuinely new URL, so it can never collide with a stale cached entry.

9
The edge is also a shield

Because edge nodes absorb the vast majority of repeat requests, only a small fraction of real traffic ever reaches the origin. That both reduces origin load during normal spikes and blunts many denial-of-service patterns, since the flood is spread across a global network rather than aimed at one machine.

10
Edge compute is a thin slice, not a backend

Some CDNs run small pieces of application logic on the edge node itself: A/B test routing, header rewriting, redirects, cheap authorization checks. It is genuinely useful for per-request work that would be silly to send to the origin, but it is a narrow slice of logic, not a replacement for real personalized backend processing.

Key Terms

The Vocabulary Of The Edge

Edge server / point of presence (PoP)

A CDN server located geographically close to end users, holding cached copies of content so requests do not need to travel all the way to the origin.

Origin pull

A caching mode where content is fetched from the origin and cached at the edge only after the first real request for it arrives, requiring no upfront upload.

Origin push

A caching mode where content is proactively uploaded to edge nodes ahead of demand, guaranteeing fast responses from the very first request in every region.

Cache hit ratio

The fraction of requests an edge node serves directly from its cache rather than forwarding to the origin, the key metric for how much load a CDN is absorbing.

Content-hashed filename

A filename that includes a hash of the file's contents, so any change produces a new filename and therefore a new cache entry, avoiding stale-cache problems after a deploy.

Edge compute

Small pieces of application logic executed directly on a CDN's edge nodes rather than at the origin, used for lightweight per-request work like routing or header rewriting.

Anycast

The routing trick that makes one IP address resolve to whichever PoP is nearest the user. It is how a single URL reaches a different machine depending on where you ask from.

Cold cache

An edge node that does not yet hold a requested object, so the request becomes a miss and pays the full trip to the origin. Every region starts cold under origin pull.

Worked Example

Origin Pull vs Origin Push

Origin pull
●

The edge fetches from the origin on the first request, then caches

●

Nothing to configure and nothing to upload ahead of time

●

Content that is never requested in a region is never stored there

One file, 40 edge regions
A new image, 40 regions
requests that miss and hit origin40
requests served from the edgeall the rest
bytes pushed in advance0

One user per region eats the full origin round trip, and if your launch traffic is one simultaneous spike, those misses all land on the origin at the same instant. It is the thundering herd, spread across the map.

VS
Origin push
●

The origin uploads to edge nodes before any user asks

●

The first request in every region is already a cache hit

●

Storage and transfer are paid whether the content is wanted or not

One file, 40 edge regions
A new image, 40 regions
requests that miss and hit origin0
requests served from the edgeall of them
bytes pushed in advance40 copies

You paid to put the file in 40 places before knowing whether 38 of them would ever want it. Worth it for a scheduled release everyone downloads at once, wasteful for a long tail of user uploads.

Both end up with the same file on the same edge nodes. The difference is who pays the cold-start cost: pull charges it to the first real user in each region, push charges it to you, in advance, for every region whether or not anyone shows up. Pull is the default for a reason, and push earns its keep only when you can name the moment the traffic arrives.

The Shape Of It

The Floor Under Every Round Trip

Round-trip latency against distance, before your server does any work at all

Round-trip latency versus distance to the server0130260010,00020,000edge node in your metro: single-digit msTypical real RTT, with routing overheadTheoretical floor: light in fiberDistance to the server (km)Round-trip time (ms)

Light in fiber travels at roughly two thirds of its speed in a vacuum, which works out to about one millisecond of round trip per hundred kilometres. A user in Sydney talking to a server in Virginia is around 16,000 km away, so physics alone charges them about 160 ms before a single byte of application work happens, and real routing, which never follows a straight line, pushes that past 200 ms in practice.

Nothing you do to the server moves that number. A faster database, a bigger instance, a tighter query, all of it is optimising the small slice of the bar that is not distance. Putting a copy of the response 50 km from the user is the only change that moves the dominant term, and that is the entire argument for a CDN.

Counterpoint

What Benefits, And What Does Not

Great fit
●

Static assets: images, CSS, JavaScript bundles, fonts

●

Video and audio streaming, chunked and cached per segment

●

Content identical for every viewer, like a public landing page

●

Large downloads where bandwidth cost, not just latency, is the problem

Poor fit
●

Personalized dashboards and account pages, unique per user

●

Search results and query-specific API responses

●

Anything requiring fresh, per-request authentication or state

●

Writes, which have to reach the origin by definition

One honest exception to the right-hand column: on a large CDN a forwarded request is not simply an extra hop. The edge terminates TLS next to the user, so the handshake’s several round trips are short ones, and it reaches the origin over already-open connections on the provider’s own backbone rather than whatever path the public internet picks that day. Sold as dynamic site acceleration, it is a real but modest win, and it is worth knowing the shape of it: you are shortening the path for uncacheable traffic, not removing it.

Underneath that, the test is one question: would the next person asking be happy with this exact response? If yes, the edge can hold it, and that is where the order-of-magnitude wins live. If no, the edge can only forward it, and no amount of edge presence turns a response built for one person into one that can be reused.

Reference

How Each Kind Of Content Caches

ContentCache KeyWorkable TTLHow It ChangesWhy
Versioned JS/CSS bundleURL aloneA year, immutableNew hash, new URLThe ideal case: the URL changes whenever the bytes do
Product imageURL aloneDays to weeksPurge, or a version in the pathRarely changes, read constantly, identical for everyone
Video segmentURL aloneEffectively foreverNever; segments are immutableChunked so a viewer only pulls the seconds they watch
Public landing pageURL, maybe localeMinutesPurge on publishSame for everyone, but has to reflect edits quickly
Logged-in dashboardWould need the userNot cachedN/ANo two responses are alike; the edge is pure overhead
Search resultsWould need the querySeconds, if at allN/AThe long tail of queries is never requested twice
Read the table top to bottom and the pattern is the same one every time: the more the URL alone determines the bytes, the longer the TTL can be. Cacheability is not a property of the file type, it is a property of how much of the response the URL already tells you.

Failure Mode

The Stale Asset After A Deploy

You ship a new version. The HTML is fresh, the server is fine, the logs are clean, and some fraction of your users see a broken, half-styled page. Their browser is loading new markup against a styles.css that an edge node fetched an hour ago and is contractually entitled to keep serving for another twenty-three.

The reason it affects only some users is the part worth internalising: every edge node caches independently, and each one’s TTL started when that node happened to fetch the file. There is no moment at which a deploy is visible everywhere. There are hundreds of separate countdowns, started at different times, that you cannot see from the origin.

A purge API fixes it after the fact, and every CDN has one, but it is a repair, not a design. The design is to never reuse a URL for different bytes: build output named app.4f9c2b.js changes its name whenever its contents change, so the old entry is simply never requested again and the new one has nothing stale to collide with.

That leaves exactly one file that must not be cached hard: the HTML that names the hashed assets. Give it a short TTL or revalidate it every time, cache everything it points at for a year, and the stale-asset problem stops existing rather than being managed.

Counterpoint

A Shield, Not A Capacity Plan

A CDN is a genuine first line of defence. Repeat requests die at the edge, so a spike that would have flattened one machine is instead spread across a global network, and a large share of crude denial-of-service traffic never reaches the origin at all. Sites routinely report that the overwhelming majority of their requests never touch their own servers.

The trap is reading that number as headroom. The share the edge absorbs is the cacheable share, and the uncacheable remainder, logins, checkouts, searches, writes, grows with the spike just as fast as everything else. Ten times the traffic is also ten times the logins. The CDN divides origin load by a constant; it does not change what happens when the input goes up.

Edge compute shifts that line a little, and only a little. Routing an A/B test, rewriting a header, checking a token, redirecting by country: all genuinely better done at the PoP than 10,000 km away. Querying your database from 300 locations is not the same kind of idea, because the data did not move, and now the hard part is 10,000 km from the logic instead of next to it.

Seen In The Wild

Edges You Use Every Day

▪

Cloudflare and Akamai run global CDN networks serving a large share of the web's static assets, images and even API traffic for millions of sites, absorbing traffic that would otherwise all hit individual origin servers. Akamai alone reports more than 4,100 edge locations across 120 or so countries, which is the scale the word 'nearby' is doing work at.

▪

Netflix built its own purpose-built CDN, Open Connect, placing caching appliances directly inside ISP networks so video travels the shortest possible path to viewers, a scale problem generic CDNs were not built to solve as cheaply.

▪

YouTube relies on Google's global edge infrastructure to cache and serve video chunks close to viewers worldwide, letting a video uploaded once be watched by millions without every view routing back to one data center.

▪

Amazon CloudFront and Fastly cache static assets and accelerate some dynamic traffic for e-commerce and media sites, cutting page load time by answering from an edge location near the shopper.

Key Points

What To Carry Forward

✓

A CDN reduces latency and origin load by caching content at edge nodes physically close to users.

✓

Origin pull caches lazily on first request; origin push uploads ahead of demand for a guaranteed fast first request.

✓

Static assets and video benefit enormously because the same bytes are correct for every viewer, giving near-perfect hit ratios.

✓

Personalized or dynamic content barely benefits, since a response unique to one user has nothing to share with the next request.

✓

TTL and invalidation still matter at the edge, and content-hashed filenames are the standard fix for stale assets after a deploy.

✓

CDNs absorb traffic spikes and blunt some attack traffic simply by serving repeat requests before they reach the origin.

✓

Every edge node caches independently, so there is no single moment when a deploy becomes visible everywhere.

✓

Edge compute handles cheap per-request logic at the PoP, but it is not a place to move your backend.

Common Mistakes

Where This Usually Goes Wrong

✕

Assuming a CDN speeds up everything, then being surprised when a personalized API endpoint sees no improvement after being put behind one.

✕

Setting a long TTL on assets without content-hashed filenames, so users keep getting a stale, broken version of a file after a deploy until the TTL finally expires.

✕

Treating origin pull and origin push as interchangeable, when push is only worth its upfront cost for content you are confident will actually be requested broadly.

✕

Forgetting that the first request in each region under origin pull still pays full origin latency, and expecting a brand-new asset to be instantly fast everywhere.

✕

Relying on a CDN instead of origin capacity planning, when a spike in genuinely dynamic, uncacheable traffic still arrives at the origin at full force.

✕

Caching a response that varied on a header or a cookie without putting that in the cache key, which serves one user's content to the next user who asks.

“The lamppost on the corner is my edge node. Every dog in this postcode writes to it, so I get the whole day’s news in four seconds without walking the block, and I have never once needed to meet any of them. What is not on the lamppost is which of my two humans is holding the bowl tonight. Nobody posts that. For that one I make the full trip home, every evening, and I am never told in advance.”

Try It Yourself

Find The Edge In A Site You Already Use

Open your browser’s network tab on any site you use daily. Find a static asset, an image, a CSS file, a JS bundle, and look at its response headers for cache-control, age, or an x-cache header naming a hit. Then find one clearly personalized response, like an account page, and notice it has either no caching header or a very short one. Write one sentence explaining why the difference has to be that way.

Quiz Review

Check your understanding

Question 1 of 5

Why does a CDN help almost nothing for a personalized dashboard endpoint, even if the endpoint is popular?

  • ✓Caching only helps when many requests can be served the same cached response. A personalized dashboard returns different data for every user, and often different data on every request, so there is no shared content for the edge node to have cached from a previous request. Nearly every request still has to be forwarded to the origin, and the CDN adds a hop with no cache hit to show for it.