AlgoViz
Networking
Networking · EasyLesson 12 of 13

CDNs — caching the web

When you load a video or a big image, where does it actually come from? If the only copy lived on one server in one city, everyone on Earth would have to reach across the planet for it — slow for people far away, and exhausting for that one server. A CDN (Content Delivery Network) fixes this by keeping COPIES of popular content in lots of places around the world, so your request is answered by a server NEAR you. It's caching, but the trick here is geography: distance is the enemy, and a CDN beats it by being close.

Distance costs time — so keep a copy nearby

Originthe one true copyEdge · EUcopyEdge · UScopyEdge · AsiacopyEdge · SAcopyYou 📍in the UScopycopynear = fast
The origin holds the one true copy, but copies are pushed out to edge servers near users everywhere. When you (in the US) ask for the content, the nearby US edge answers — close means fast — instead of a server on the far side of the world.

Every message on the internet takes time to travel — and the farther it goes, the longer it takes. A request to a server across the planet has to hop through many machines and back, and even at near-light speed that round trip adds up to a noticeable wait. That travel-time delay is called LATENCY, and distance is one of its biggest causes. A CDN's whole idea is: don't make people travel far. It places many servers — called EDGE servers — in cities all over the world, and keeps copies of popular files (images, videos, whole pages) on them. When you ask for a cat video, you don't reach across the ocean to the original server; the nearest edge server, maybe in your own city, hands you its copy. Less distance, less waiting.

Caching: answer from a kept-handy copy

Underneath, a CDN is doing CACHING — keeping a ready-made copy of something close by so you don't have to fetch it from far away every time. Think of a popular book: instead of everyone in the country mailing the one national library, every town keeps a copy on its own shelf. The first person in town might wait while the town library orders it, but after that, everyone nearby gets it instantly off the local shelf. That's exactly how an edge server works. The first time someone in your area wants a file, the edge fetches it from the ORIGIN — the one true source server — and keeps a copy. Every later request from your area is served straight from that local copy. (System design talks about caching as a general idea — keeping answers handy to avoid repeating work. A CDN is that idea spread across the map: caching where the 'handy' part is being physically near you.)

How do you get sent to the nearest copy? Often DNS

Here's the neat part: you never have to know which edge server is closest — you just type the same web address as everyone else. The routing-to-nearest happens behind the scenes, and it often rides on DNS, the system that turns a name like 'video.example.com' into an address. When your device asks DNS for the address, a CDN-aware DNS can look at roughly where you are and answer with the address of the EDGE server nearest you. So two people in different countries ask for the same name and get pointed at two different nearby copies. You did nothing special; DNS quietly steered you to the closest shelf. And the origin server gets a gift too: most requests are soaked up by the edges, so it isn't pounded by the whole world at once. It only has to feed fresh copies out to the edges. Faster for users, lighter for the origin — everyone wins.

It's a tradeoff

Option👍 Pro👎 Con
Serve everything from one originSimple — one copy, always the latest, nothing to keep in sync.Far-away users wait a long time, and the single server gets overwhelmed when lots of people show up at once.
Use a CDN (copies at the edge)Users are served by a nearby copy — much faster — and the origin is spared most of the load.There are now many copies to keep up to date; right after an update, an edge might serve a slightly stale version until it refreshes.

Questions you might have

If a CDN keeps copies everywhere, which one is the 'real' file?

The real, original file lives on the ORIGIN server — that's the one true source. The edge servers hold COPIES of it for speed. When the origin updates the file, the edges refresh their copies. So there's one source of truth and many fast copies fanned out near users.

How does my computer know to ask the nearby copy instead of the far one?

It usually doesn't decide — DNS does it for you. When you look up the web address, a CDN-aware DNS notices roughly where you are and hands back the address of the closest edge server. You type the same name as everyone else; DNS quietly points you at your nearest copy.

Isn't this the same caching that system design talks about?

It's the same core idea — keep a copy of something handy so you don't redo the work of fetching it. The networking flavor is that 'handy' means physically CLOSE: the win is geography. A CDN is caching spread across the world map, beating the delay that comes from distance.

What if the content changes — won't people get an old copy?

It can happen briefly. Right after the origin updates a file, the edges still hold the old copy until they refresh. CDNs handle this with rules about how long a copy stays valid before it's re-fetched, and the ability to force a refresh. So there's a small tradeoff: huge speed, at the cost of an edge sometimes being a little behind for a moment.

🧠A CDN keeps copies of popular content on edge servers near users all over the world — beating the delay of distance by caching close to you, and sparing the origin server — with DNS quietly routing you to the nearest copy.
✅ Check yourself4 quick questions — prove the idea stuck.Start →🧭 Take it furtherA new system you haven't read about — would you reach for this idea, and what does it cost?Try it →

Best read after: DNS — the internet's phonebook