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.
- Origin server — Virginia, USA — healthy
- Reader in Pune — healthy
- First request waits about
- 205 ms
- Later requests wait about
- 205 ms
- One far-away origin. Without a CDN, a reader in Pune fetches cat.jpg from Virginia, about 13,000 km away.
- Edges near readers. A CDN adds edge servers near readers, here in Mumbai, London and New York.
- The nearest edge. The Pune reader asks for cat.jpg, and the request goes to Mumbai, about 120 km away.
- First time: a miss. The Mumbai edge has no copy yet, so it asks the origin, about 12,900 km away.
- Keep a copy. The origin sends the photo back, and the edge keeps a copy; this first wait is about 205 ms.
- Next time: a hit. The next Pune reader gets the copy straight from Mumbai in about 3 ms.
- Copies expire. Each copy has a TTL, a time to live; once it runs out, the edge asks the origin again.
- 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
- Each reader reaches the nearest edge server, which is often a few hundred km away, though not always.
- The first request is a miss: the edge fetches the file from the far-away origin, so it is still slow.
- Later requests are hits, served from the copy in a few milliseconds instead of hundreds.
- Each copy has a TTL, and a purge can remove it early when the file changes.
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 (A content delivery network: many servers around the world that keep copies of files close to the people who ask for them.) (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
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 (One of a CDN's many servers, placed near readers. It answers from its own copy, or fetches the file once from the origin server.) in Mumbai and the origin in Virginia.
Reach the nearest edge
A CDN runs servers in many cities. Each site is called an edge location, or point of presence (PoP). (The Domain Name System: the way the internet turns a name, such as example.com, into the address of a server.) 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.
A miss, then a hit
Pick a case to follow belowWith this case: With no CDN, both readers waited about 200 ms, and the origin served both requests.
Reader, in Pune Reader in Pune CDN edge, in Mumbai CDN edge in Mumbai not used Origin, in Virginia, NOW Origin in Virginia 1 served NOW - Reader — in Pune — healthy
- CDN edge — in Mumbai — healthy — not used
- Origin — in Virginia — healthy — NOW — 1 served
- Request
Step 1 of 2Step 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 waited200msReader 2 waited–Requests at the origin1in VirginiaCopy at the edgenono CDN in useStep 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 (A small, fast store that keeps copies of popular data, so most reads skip the slower database.) 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.
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.
When a file changes
Pick a case to follow belowWith 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 Pune Reader in Pune CDN edge, in Mumbai CDN edge in Mumbai version 1 · old Origin, in Virginia, NOW Origin in Virginia version 2 NOW - Reader — in Pune — healthy
- CDN edge — in Mumbai — healthy — version 1 · old
- Origin — in Virginia — healthy — NOW — version 2
- File
Step 1 of 2Step 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 edgeversion 1Watch: oldThe next reader sees–nobody has asked yetRequests at the origin0Step 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.
| Way | How it works | The cost |
|---|---|---|
| Wait for max-age | Each copy runs out on its own | Readers can see the old file for as long as max-age |
| Purge | Every edge drops its copy | A burst of misses at the origin, and some CDNs take minutes |
| New file name | The changed file gets a new address | Every 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
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 (How long a cached copy is kept before it is thrown away.) 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 (A burst of requests that all miss the cache for the same key at once and hit the database together.): 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 (The share of reads the cache answers by itself, without asking the database.): 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
Interview
What interviewers look for
- In a design with images or video, put a CDN in front of (A huge, cheap store for files such as photos and videos. Each file is saved whole under a key, such as video.mp4.). 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?
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
Apply it
Judge a design
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.