Stage 2 · Building blocks · Easy · 12 min

CDNs and edge caching

A CDN keeps copies of files on servers near your users. Learn what a hit and a miss cost, how long a copy lives, and how to replace one when the file changes.

What each interview level is expected to know

Learn first: DNS and geo-routing, HTTP, TCP, UDP and connections

After this lesson you can:

  • Explain what a CDN hit and a miss each cost the user and the origin.
  • Choose how long an edge copy should live, and replace a file the moment it changes.
  • Avoid caching a private page, and protect the origin from a stampede.

Start here

See it in 60 seconds

How a CDN answers from nearby

A CDN keeps copies of files on edge servers near readers, so most requests travel a short way instead of crossing the world.

Scene 1 of 7 · One far-away origin

Without a CDN, a reader in Pune fetches cat.jpg from Virginia, about 13,000 km away.

North AmericaEuropeAsia and AustraliaOrigin server, Virginia, USAOrigin serverVirginia, USAReader in PuneReader in Pune
  • Origin server — Virginia, USA — healthy
  • Reader in Pune — healthy
First request waits about
205 ms
Later requests wait about
205 ms
  1. One far-away origin. Without a CDN, a reader in Pune fetches cat.jpg from Virginia, about 13,000 km away.
  2. Edges near readers. A CDN adds edge servers near readers, here in Mumbai, London and New York.
  3. The nearest edge. The Pune reader asks for cat.jpg, and the request goes to Mumbai, about 120 km away.
  4. First time: a miss. The Mumbai edge has no copy yet, so it asks the origin, about 12,900 km away.
  5. Keep a copy. The origin sends the photo back, and the edge keeps a copy; this first wait is about 205 ms.
  6. Next time: a hit. The next Pune reader gets the copy straight from Mumbai in about 3 ms.
  7. Copies expire. Each copy has a TTL, a time to live; once it runs out, the edge asks the origin again.
  8. Now you try. Pick where a reader lives, and see which edge answers and how long they wait. For example: A reader in Paris reaches the London edge: about 105 ms the first time, then about 6 ms. The London edge is about 340 km away, so only the first request, a miss, travels on to Virginia.

Scene 1 of 7: One far-away origin. Without a CDN, a reader in Pune fetches cat.jpg from Virginia, about 13,000 km away.

Key points

  1. Each reader reaches the nearest edge server, which is often a few hundred km away, though not always.
  2. The first request is a miss: the edge fetches the file from the far-away origin, so it is still slow.
  3. Later requests are hits, served from the copy in a few milliseconds instead of hundreds.
  4. Each copy has a TTL, and a purge can remove it early when the file changes.

Go deeper in the Request flow lab →

In plain words

A reader in Pune asks for cat.jpg, and the only copy is on a server in Virginia, about 12,900 km away. Every request crosses the world and back. A (CDN) is a set of servers in many cities that keep copies of your files. The reader's request goes to a nearby one, here in Mumbai about 120 km away, and is answered from its copy.

On each request, the nearby server checks its own copy first. If it has one, the reader gets the file in a few milliseconds. If not, it fetches the file once from Virginia, keeps a copy, and serves the next readers from it. Each copy expires after a set time, so a changed file reaches readers only when the old copy runs out or is cleared.

The problem it solves

One origin has two limits. Far-away users wait for every file to cross the world. And when a photo or video gets popular, every download lands on the same servers, which can overload them. Copies near the users ease both.

Why it matters

Distance costs time. A round trip halfway around the world is 40,000 km, which takes at least 200 ms in fibre. A CDN keeps copies near readers, so a request served from a copy travels a short way. It also takes most of the load off your own servers.

A worked example

A video file takes 200 ms to load from a server on another continent but only 15 ms from a nearby CDN copy. For 1,000 people watching, serving them all from the CDN instead saves 1,000 × (200 − 15) = 185,000 ms of total waiting time, about 185 seconds.

Think of it like…

Think of a chain with one factory abroad and a small shop in each city. A popular item is on the local shelf. For a rare item, the shop orders one from the factory, then keeps it in stock for the next buyer.

Where this analogy stops working

A nearby copy that saves a long trip is the useful part. But a shelf runs out, and a cached file does not: one copy serves every reader. The real risk is age. When the file changes, an edge can serve the old copy until its time limit ends.

Try it

See it working

Live simulationOpen the full lab →

Lab · Building blocks

CDN and edge caching

Users in three regions fetch files through a nearby edge cache. The origin sits in the US. See how popularity, TTL and cache size set the hit rate, what a purge does, and how an origin shield protects the origin.

Try it

Lab time 3.0 s
30 ms90 ms150 msUS, 20 requests/sUS20 requests/sUS edge, hit rate 49%US edgehit rate 49%20/20 filesEurope, 20 requests/sEurope20 requests/sEurope edge, hit rate 62%Europe edgehit rate 62%20/20 filesAsia, 20 requests/sAsia20 requests/sAsia edge, hit rate 61%Asia edgehit rate 61%20/20 filesOrigin (US), holds every fileOrigin (US)holds every file18 fetches/s

The lab is busy, so only some requests are drawn as dots, few enough to follow. The numbers still count every request.

  • US — 20 requests/s — healthy
  • US edge — hit rate 49% — healthy — 20/20 files
  • Europe — 20 requests/s — healthy
  • Europe edge — hit rate 62% — healthy — 20/20 files
  • Asia — 20 requests/s — healthy
  • Asia edge — hit rate 61% — healthy — 20/20 files
  • Origin (US) — holds every file — healthy — 18 fetches/s
  • Request
  • Edge hit
  • Edge miss → origin
  • Answer after a miss
Hit rate
58%
Origin fetches
18/s
Average wait · US
61 ms
Average wait · Europe
73 ms
Average wait · Asia
98 ms
Purges
0

Numbers add up:201 requests = 117 hits + 84 misses; misses = 84 origin fetches + 0 shield hitsThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Files
Popularity

A few files get most requests, as on most websites.

(TTL)
Edges
20 of 60 files

Edge misses go to one shared cache next to the origin first.

By region

RegionHit rateAverage waitFiles cachedTrip to origin
US49%61 ms20 of 2030 ms
Europe62%73 ms20 of 2090 ms
Asia61%98 ms20 of 20150 ms

A miss in Asia costs the most, because Asia is farthest from the origin. Hits cost the same 20 ms everywhere.

Hit rate
0%25%50%75%100%1s2s3s
Show data table
TimeHit rate
1s19%
1s26%
2s43%
2s49%
3s54%
3s58%
Origin fetches per second
012.52537.5501s2s3s
Show data table
TimeOrigin fetches/s
1s42
1s45
2s38
2s21
3s17
3s18

What just happened

  1. 0.0sInfo: Every edge starts empty. Each keeps up to 20 files for 30 s.
What this model simplifies
  • Round trips are fixed and illustrative: 20 ms to the nearby edge, and 30, 90 or 150 ms from each edge to the origin. Real networks vary.
  • Time is slowed so you can watch each request. The share of hits and misses is what matters here.
  • Two misses for the same file at the same moment each go to the origin. Real CDNs often combine them into one fetch.
  • Each region has one edge, and every file has the same size. Real CDNs run hundreds of edges and cache files of very different sizes.
  • A copy counts its TTL from when this cache stored it. A copy from the shield starts a new TTL, so a file can be up to twice the TTL old. Real CDNs pass on the copy's age.
  • Changing the TTL also changes copies already stored. The shield has room for three edges' worth of files, which here is every file.

Also try: Request flow · Caching

Mechanics

How it works

A CDN answers a reader from a copy near them, and goes back to your origin only when it has no copy. The pictures follow this page's example: a reader in Pune, an in Mumbai and the origin in Virginia.

  1. Reach the nearest edge

    A CDN runs servers in many cities. Each site is called an edge location, or point of presence (PoP). or network routing sends each user to a nearby one, so the data travels a short distance.

    A short distance means a short wait. In the pictures, a round trip to Mumbai takes 5 ms and a round trip to Virginia takes about 200 ms. These are round example times, not measurements.

  2. A miss, then a hit

    Pick a case to follow below

    With this case: With no CDN, both readers waited about 200 ms, and the origin served both requests.

    Reader, in PuneReaderin PuneCDN edge, in MumbaiCDN edgein Mumbainot usedOrigin, in Virginia, NOWOriginin Virginia1 servedNOW
    • Reader — in Pune — healthy
    • CDN edge — in Mumbai — healthy — not used
    • Origin — in Virginia — healthy — NOW — 1 served
    • Request
    Step 1 of 2

    Step 1 of 2 · Origin

    Done: Reader 1 asks the origin for cat.jpg

    What happens
    The request crosses from Pune to Virginia and back, about 200 ms.
    Why
    With no CDN, the only copy of the file is at the origin.
    What changes
    The origin has served 1 request.
    Next
    Reader 2 asks the origin for cat.jpg
    Reader 1 waited
    200ms
    Reader 2 waited
    –
    Requests at the origin
    1
    in Virginia
    Copy at the edge
    no
    no CDN in use

    Step 1 of 2: Origin. Reader 1 asks the origin for cat.jpg. The request crosses from Pune to Virginia and back, about 200 ms.

    The edge checks its for the file. In the picture, two readers ask for cat.jpg, one after the other. Compare no CDN with a CDN.

    With no CDN, both readers make the long trip, and the origin serves both. With a CDN, reader 1 misses: the edge has no copy yet, so it fetches the file from the origin and keeps it. That first reader waits a little longer than with no CDN.

    Reader 2 is a hit. The edge answers from its copy, and your origin does no work. The more readers ask for the same file near one edge, the more of those requests are hits.

  3. How long a copy lives

    The origin says how long an edge may keep a copy, in a Cache-Control header on the file. In the picture it is max-age=86400: one day, in seconds.

    A long max-age gives more hits and less work for the origin. The cost is that a changed file can stay old at the edges for that long. A page built for one user must not be kept for others, so mark it with Cache-Control: private.

  4. When a file changes

    Pick a case to follow below

    With this case: Doing nothing, readers kept getting version 1. They can see it for up to a day, the max-age of the copy.

    Reader, in PuneReaderin PuneCDN edge, in MumbaiCDN edgein Mumbaiversion 1 · oldOrigin, in Virginia, NOWOriginin Virginiaversion 2NOW
    • Reader — in Pune — healthy
    • CDN edge — in Mumbai — healthy — version 1 · old
    • Origin — in Virginia — healthy — NOW — version 2
    • File
    Step 1 of 2

    Step 1 of 2 · Origin

    Done: cat.jpg changes at the origin

    What happens
    You upload a new cat.jpg: version 2. The edge still holds version 1, with hours left on its max-age.
    Why
    The origin does not tell the edges that a file changed.
    What changes
    The edge and the origin now disagree.
    Next
    The next reader gets the old file
    Copy at the edge
    version 1
    Watch: old
    The next reader sees
    –
    nobody has asked yet
    Requests at the origin
    0

    Step 1 of 2: Origin. cat.jpg changes at the origin. You upload a new cat.jpg: version 2. The edge still holds version 1, with hours left on its max-age.

    What these pictures simplify. These pictures use one edge, two readers and round example times: 5 ms to the edge and back, 200 ms to Virginia and back. A real CDN has hundreds of edges, and times depend on the network. The CDN lab above runs many readers at once.

    Your origin has a new version of cat.jpg, but the edge still holds the old one. Switch between the three ways to handle it.

    Doing nothing is the mistake. The edge has no way to know the file changed, so readers get the old picture until the copy's max-age runs out.

    A purge tells every edge to drop its copy, so the next request fetches the new file. Depending on the CDN, a purge takes from seconds to minutes to reach every edge. Right after it, many edges ask the origin at once.

    A new file name, such as cat.v2.jpg or app.3f2a.js, is a new address, so no edge holds an old copy of it. Build tools often put a hash of the content in the name. Then the file can keep a long max-age, because any change comes with a new name.

Ways to replace a changed file
WayHow it worksThe cost
Wait for max-ageEach copy runs out on its ownReaders can see the old file for as long as max-age
PurgeEvery edge drops its copyA burst of misses at the origin, and some CDNs take minutes
New file nameThe changed file gets a new addressEvery page must link to the new name

Use it when

  • Files that are the same for every user: JavaScript, CSS, images and video pieces. One copy at each edge serves everyone nearby.
  • Public answers that change slowly, such as a product list or a short-link redirect: cache them for a few seconds or minutes.
  • Soaking up a flood of traffic: a large CDN has far more capacity than one origin, so an attack is spread across many edges.

Skip it when

  • Private data that differs for each user, such as a bank balance: a cached copy could reach the wrong person, or be out of date.
  • Data that must be current on every read, such as the stock left in a sale: a cached copy can be seconds old.

Check yourself

What happens on a CDN cache miss?

Judgement

Trade-offs and how it fails

What you give up

Performance
A hit is answered nearby, in a few milliseconds where a far-away origin would take hundreds. A miss saves nothing: the edge still has to fetch the file from the origin.
Consistency
An edge serves its copy until the ends, so users can see an old file after you change it. A long TTL means more hits and older copies. A short TTL means fresher copies and more trips to the origin.

Failure modes

  • Origin : after a purge, every edge misses at once and asks the origin for the same files. The burst can overload it. An origin shield, one cache between the edges and the origin, absorbs most of it.
  • Caching a private page by mistake: a page built for one user is stored and served to others. Mark private answers with a header such as Cache-Control: private.
  • A low : if most requests miss, you pay for the CDN and the origin still does the work.

In the real world

  • Netflix runs its own CDN, Open Connect, and places its servers inside internet providers' networks to deliver video.
  • Cloudflare's documentation says it caches static files such as images, CSS and JavaScript by default, but not HTML.

Trade-offs to be ready to discuss: Cdn cost vs origin load

Check yourself

Why is a CDN a poor fit for a page that is different for every user?

Interview

What interviewers look for

  • In a design with images or video, put a CDN in front of . Say so early: "Clients fetch media from the CDN, not from my servers."
  • State what you cache and for how long: "Images for a day, the product list for 30 seconds, nothing private."
  • Say how a changed file reaches users: a version in the file name, or a purge.

Say it out loud: Can a CDN cache answers from an API, not only files?

You ship a new app.js, but some users still get the old file from the CDN. Which fix works without waiting for the cache to expire?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorExpected
Serve static files from a CDN.
MidExpected
Cache headers, invalidation, and what can be cached.
SeniorExpected
Dynamic content at the edge and the economics of hit rate.
StaffUseful
CDN strategy and cost across regions.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

Across all its edges, a CDN gets 10,000 requests a second for your files, with a hit rate of 95%. How many requests a second reach your origin?

Apply it

A file is sent with Cache-Control: max-age=3600. You change it at the origin and do nothing else. How long might users still get the old file?

Judge a design

A team puts the CDN in front of every page with a 1-hour TTL. That includes /account, which shows each user's own orders. What is the risk?

Next: rate limiting, how a service caps each caller so that one of them cannot use up its capacity.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • StandardRFC 9111: HTTP CachingIETFThe rules every CDN follows: Cache-Control, freshness, and checking a stale copy with the origin.
  • Official docsHTTP cachingMDN Web DocsThe same rules with examples: shared versus private caches and how to bust a cached file.