What is a CDN? How content delivery networks make websites fast

A CDN keeps copies of your images, videos and pages on servers close to your users, so they don't wait for a trip across the world. Here is how it works, what it caches, and what it can't do.

A simple world map. One origin server sits in the United States. Small yellow edge servers sit in Mumbai, London, São Paulo, Tokyo and Sydney. Users near Mumbai connect to the Mumbai edge with short lines, and a long faded dashed line from India to the origin is crossed out.
Fig. 1 — Instead of every user travelling to one far-away server, a CDN brings copies of the content close to them.

Imagine a popular bakery in Mumbai whose cakes are loved all over India. If every customer had to travel to that one shop, people in Chennai and Delhi would spend hours on the road for a slice. So the bakery opens small outlets in every big city, stocked each morning from the main kitchen. Customers walk to the nearest outlet and get the same cake in minutes.

A CDN does this for websites. This guide explains what a CDN is, how it decides what to serve, and where it fits in a system design.

What is a CDN?

A CDN, or content delivery network, is a large group of servers spread across many cities around the world. They keep copies of a website’s content, such as images, videos, scripts and sometimes whole pages, and serve each user from the server closest to them.

Three words come up again and again:

  • Origin: your own server, where the real, original content lives. The main kitchen.
  • Edge server: one of the CDN’s servers in a city near users. An outlet. A city with edge servers is often called a point of presence (PoP).
  • Cache: the copies kept on each edge server, ready to hand out.

Well-known CDN providers include Cloudflare, Akamai, Amazon CloudFront and Fastly. Big video and social apps often run their own as well.

Why distance matters

Data travels through cables at a large but limited speed. Light in optical fibre covers roughly 200 kilometres per millisecond. A round trip from Mumbai to a server in the eastern United States is around 13,000 km each way, so the trip alone takes well over 100 milliseconds before the server does any work. Loading a page often needs many such trips.

An edge server in the same city might be only a few milliseconds away. Multiply that saving by every image and script on a page, and the page feels dramatically faster. This delay is called latency, and cutting it is the main reason CDNs exist.

How a CDN works: hit and miss

When a user asks for a file, say logo.png, here is what happens:

  1. The user’s browser is sent to the nearest edge server. This is usually done through DNS: the CDN answers the lookup with the address of a nearby edge.
  2. The edge checks its cache.
  3. Cache hit: it has a fresh copy, and returns it straight away. The origin is never asked.
  4. Cache miss: it doesn’t have it (or its copy is too old), so it fetches it from the origin, returns it to the user, and keeps a copy for the next person nearby.
Two rows. Top row, cache hit: a user in Chennai asks the Chennai edge server for logo.png and gets it back in about 10 milliseconds; the origin is greyed out and not asked. Bottom row, cache miss: the first user asks, the edge does not have the file, fetches it from the far-away origin, which takes much longer, stores a copy, and returns it. A note says the next user nearby gets a hit.
Timings are illustrative. The first request in a city pays the long trip; everyone after it gets the nearby copy.

The share of requests answered from the edge is called the cache hit ratio. A high hit ratio means faster pages and much less load on your origin servers.

How long does a copy stay? TTL and invalidation

Copies can’t live forever, or users would see an old logo after you change it. Two tools control freshness:

  • TTL (time to live): the origin tells the CDN how long a copy is good for, usually through the Cache-Control header (for example, max-age=86400 means one day). After that, the edge checks with the origin again.
  • Purge (invalidation): you tell the CDN to throw away a copy right now, for example after an urgent fix.

A common trick is to put a version or fingerprint in file names, like app.3f9a2c.js. When the file changes, its name changes, so the CDN treats it as a brand-new file and the old copy simply stops being asked for. This lets you set very long TTLs safely.

For the general rules of keeping cached data fresh, see cache eviction and invalidation.

What should go on a CDN?

Good fit Use with care Usually not cached
Images, fonts, CSS, JavaScript HTML pages that change sometimes Your bank balance, your cart
Videos and audio API answers that are the same for everyone Anything personal to one user
Downloads, software updates Search results Payment and login requests

The rule of thumb: cache what is the same for many people. Anything personal still goes to your origin, though it can still pass through the CDN.

Where a CDN fits in a system design

In most designs, the CDN is the first thing a user’s request touches:

User → CDN edge → (on a miss) load balancer → app servers → database

Every hit at the edge is one request your own servers never see. For a site full of images or video, the CDN may serve the large majority of all bytes, which is why it is usually the cheapest way to handle huge traffic. Static files often live in object storage, with the CDN in front of it.

Common misconceptions

“A CDN hosts my website.” Usually not. Your origin still holds the real content. The CDN holds copies and passes other requests through.

“A CDN makes everything faster.” It speeds up content that can be cached and served nearby. A slow database query behind a personal page is still slow; the CDN can only shave off part of the network trip.

“Once I purge, the old file is gone everywhere instantly.” Purges spread across many locations and take a short time. Browsers may also keep their own copy. Versioned file names avoid this problem.

Key takeaways

  • A CDN is a network of edge servers around the world that keep copies of your content near users.
  • It cuts latency because data travels a much shorter distance.
  • A cache hit is served from the edge; a cache miss goes to the origin once, then gets cached.
  • Freshness is controlled with TTLs, purges and versioned file names.
  • Cache what’s the same for many people; keep personal data at the origin.

Frequently asked questions

What is a CDN in simple words?

A CDN is a set of servers in many cities that store copies of a website’s files. When you visit the site, you get the files from a server near you instead of one far away, so the site loads faster.

Is a CDN the same as a cache?

A CDN is a kind of cache, spread across many locations and placed in front of your website. Caches can also live in your browser, in your app servers or next to your database.

Do small websites need a CDN?

Not always, but it often helps, and many providers have free plans. If your users are spread across countries or your pages have many images, a CDN is one of the easiest speed-ups you can get.

What is the difference between a CDN and a load balancer?

A load balancer spreads requests across your own servers in one place. A CDN serves copies of content from many places around the world, close to users. Large systems use both.

What does “origin” mean in a CDN?

The origin is your own server or storage where the original content lives. The CDN fetches from the origin when it doesn’t have a fresh copy.

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.