Content Delivery Networks: Serving Copies of Your Content Close to Users
Learn how a CDN keeps copies of files near visitors, why that speeds up pages, and how stale copies are managed.
In short
A content delivery network, or CDN, is a network of servers around the world that keeps copies of a website's files near visitors so pages load faster.
11 min readFree
What You Will Learn
Some websites load fast for everyone. Others feel slow when the visitor is far from the server. A content delivery network, or CDN, is one common fix. In this lesson you will learn what a CDN is, how it answers a request, what it is best for, and where it sits in a system. You do not need any earlier knowledge of networks.
Key words
- Content delivery network (CDN)
- A group of servers spread across many places. It keeps copies of a website's files close to the people who ask for them.
- Origin server
- The main server that holds the original files and runs the application. It is the source of truth, meaning its version is the correct one.
- Edge server
- A CDN server placed near users. It stores copies and sends them out to nearby visitors.
- Cache
- A store of copies kept so that the next request can be answered faster.
- Latency
- The wait between asking for something and getting the first answer. A separate lesson covers it in depth.
- Static content
- Files that are the same for every visitor, such as images, CSS style files, JavaScript code, and videos.
What Is a CDN?
A CDN is a network of servers spread around the world. Its job is to keep copies of a website's files near the people who open the site. Without a CDN, every visitor asks the same origin server. If the origin sits in one city and the visitor sits on the other side of the planet, each file has to travel that whole distance.
A CDN changes that path. The origin still holds the master copy of each file. The CDN keeps extra copies in many locations, so most requests are answered by a server that is much closer to the visitor.
Origin server
The original files and the application live here. When a file changes, the change is made here first.
Edge servers
The CDN's servers in many cities and network meeting points. Groups of them are often called points of presence, or PoPs. They hold copies and answer nearby visitors.
DNS
The Domain Name System, a naming service that turns a web address into a server address. In a CDN, it helps send the visitor's browser to a suitable edge server. A separate lesson explains how it works.
How Does a CDN Answer a Request?
The core idea is simple. An edge server first checks whether it already has a copy of the file. If it does, it answers at once. If it does not, it fetches the file from the origin, keeps a copy, and then answers. Each later visitor in that area can get the copy from the edge.
- 1
The browser asks for a file
A page asks for an image, a style file, or a video. The browser sends the request using the file's web address.
- 2
The request reaches a nearby edge
The naming service points the browser to a nearby edge server, not to the far origin.
- 3
The edge looks in its cache
The edge server checks whether it has a copy of that exact file that is still allowed to be used.
- 4
The edge answers from its copy or fetches one
If a usable copy exists, the edge sends it. If not, the edge asks the origin, stores the reply if the rules allow it, and then sends it to the visitor.
Sources describe the same loop in different words. The edge checks its copies, sends what it has, and goes to the origin only when needed.
- 1
Keep the copy for later
Copies are kept for a set time. While they are fresh, nearby visitors get them without the origin being involved.
Cache hit
The edge already has a usable copy. It sends the copy straight away, and the origin does no work for that request.
Cache miss
The edge does not have a usable copy. It must fetch the file from the origin or from a larger parent cache. This visitor waits longer, but later visitors in the area usually get a hit.
A Small Model of Hits and Misses
The next program is a toy model, not a real CDN. It has one fake origin that counts its own trips. The edge keeps a dictionary of copies. The first request for each file is a miss. Repeat requests are hits. Notice that the origin is asked only twice, even though four requests arrive.
origin_fetches = 0
def fetch_from_origin(path):
global origin_fetches
origin_fetches += 1
return 'copy of ' + path
cache = {}
def edge_request(path):
if path in cache:
return 'HIT ' + cache[path]
body = fetch_from_origin(path)
cache[path] = body
return 'MISS ' + body
for path in ['/logo.png', '/logo.png', '/logo.png', '/news.css']:
print(edge_request(path))
print('origin fetches:', origin_fetches)
Output
MISS copy of /logo.png HIT copy of /logo.png HIT copy of /logo.png MISS copy of /news.css origin fetches: 2
Tip
Count the misses to see how much work reached the origin. A high share of hits means the origin does little work for those files.
What Is a CDN Best For?
A CDN works best for static content. Images, style files, script files, and videos are often the same for every visitor. One copy can therefore serve many people. Sources list HTML pages, JavaScript, stylesheets, images, and videos as things a CDN can deliver. Pages that change for each user are a poor fit, as the next sections explain.
Lower latency
Files come from a nearby edge, so the trip is shorter and pages can show sooner for distant visitors.
Less origin load
Cache hits never reach the origin. The origin uses its power for dynamic work, such as building a page for one user or running a database query.
Better availability
Some sources note that cached content can still be served when the origin is down, so a short outage is less visible to readers.
The benefits have a cost. A copy can be out of date. A sources of truth change at the origin, but the edge may keep showing the old copy for a while. That is called stale content.
Two terms describe how long a copy stays in use. A time to live, or TTL, is the number of seconds an edge keeps a copy before checking again.
Headers sent by the origin set the TTL. For example, a header with max-age set to 86400 tells the CDN to keep the copy for 24 hours. A short TTL keeps copies fresh but sends more visitors back to the origin. A long TTL saves work but can keep old content longer.
Tip
Ask two questions before you cache a file: is it the same for every visitor, and how bad is it if a reader sees the old version for a while? A logo can wait. A live score cannot.
There is a safe pattern for files that change. Give each new version of a file a new name, such as one with a hash of its contents in the name. A new name is a new address, so edges fetch it at once. Old copies simply stop being asked for.
Where Does a CDN Sit in the Request Path?
A request in a typical system travels through several layers. The CDN is the first stop for files that can be cached. Only requests the CDN cannot answer go on to the application servers behind it. Sources describe the CDN as sitting between the origin and the browser.
- 1
The browser sends the request
The visitor opens a page. The browser asks for the page and for each file the page needs.
- 2
The CDN edge receives it first
The edge is the first server the request meets, because the naming service sent the browser there.
- 3
Cached files stop at the edge
Images, style files, and videos with a usable copy are answered here. The application servers never see these requests.
- 4
Other requests move on
Requests for personal or live data go through to the origin. The application servers build the answer, and a database may be used.
- 5
The reply travels back
The answer returns through the edge. If the rules allow, the edge keeps a copy for the next visitor.
Every choice about caching changes two things at once: speed and freshness. Caching more files, or keeping them longer, makes pages faster and reduces origin load. It also makes stale content more likely. Caching fewer files does the opposite.
Long TTL with new file names
Good for versioned static files such as scripts and styles. Speed is high and nothing needs manual purging.
Short TTL for public pages
Good for pages that change now and then. Readers get fresh content, and the origin still sees some traffic.
No caching for private data
Good for a person's account or cart. Headers tell caches not to store these replies at all.
Worked Example: A News Page
Take a news site whose origin servers are in one country. A reader in a distant country opens an article. The article page needs a logo, a style file, a photo, and a short video. Follow what happens to each file, one at a time.
- 1
The reader's browser finds an edge
The naming service sends the browser to a nearby edge server, so the trip is shorter than a trip to the origin.
- 2
The logo is a hit
Readers in that region have already loaded the logo. The edge has a usable copy and sends it at once.
- 3
The photo is a miss the first time
No one nearby has opened this photo yet. The edge fetches it from the origin, keeps a copy, and sends it to the reader.
- 4
The next nearby reader gets a hit
When another reader in the same region opens the same article, the edge sends its stored photo. The origin is not asked again while the copy is fresh.
- 5
The article text may still come from the origin
Text that changes often, or text tailored to one reader, may skip the cache. Those requests travel on to the application servers.
Netflix uses a large version of this idea for video. Its Open Connect appliances can be placed inside internet providers' networks, and So for Netflix, the first viewer does not always trigger a fetch from the origin. The shared idea is still the same: copies sit near the viewers.
Quick check
A small online store in New York keeps all product photos on one server in New York. Customers in Australia say pages with many photos load slowly. What should the store add, and why does it help?
What Common Mistakes Should You Avoid?
Forgetting the update plan
A long TTL with no way to purge or change file names can leave readers with old files for a long time.
Caching private pages
A page with one person's data must not be stored at shared edges. Mark such replies so the CDN does not keep them.
Very short TTLs on everything
If copies expire at once, the edge keeps asking the origin, and the benefit of the CDN shrinks.
Very long TTLs on changing content
Files that change often need shorter TTLs or a clear way to refresh them.
Check Your Understanding
Here is a second scenario. It uses the idea of TTL and stale copies from earlier in the lesson.
Quick check
A news site keeps its chart images in edge servers with a long TTL. An editor fixes a wrong chart but keeps the same file name. Readers near an edge that already has the old chart open the article. What do they see?
Summary
A content delivery network keeps copies of files on edge servers near users. A request first meets an edge. A cache hit is answered from the edge at once. A cache miss sends the request to the origin, and the edge stores a copy for later visitors.
A CDN fits in front of the application servers. It works best for static content, and it cuts latency and origin load. The price is stale content, which you manage with TTLs, invalidation, and new file names. Choosing what to cache and for how long is the key design decision.
Tip
When you design a page, sort its files into two groups: shared files that suit caching, and personal or live data that does not. Put the shared group behind the CDN.
Sources
- Cloudflare: What is a CDN?
- Akamai: What is a Content Delivery Network (CDN)?
- TechTarget: What is a content delivery network?
- NOC.org: Content Delivery Networks (CDN) Explained
- Network Solutions: What is a CDN? How content delivery networks work
- Cloudoptimo: How CDN Works: Request Routing, Caching, and Edge Delivery
- CDNetworks: How Content Delivery Networks Work
- System Design Interview Handbook: 2.5 Content Delivery Networks (CDN)
- TechInterview: Design a CDN (Content Delivery Network)
- SpaceComplexity: CDN and Edge Caching for System Design
- CodeWithVenu: CDN Fundamentals for System Design
- Netflix Open Connect: Embedded appliances
- ByteByteGo: Netflix, What Happens When You Press Play, Part 2
Last updated on 17 November 2026
Related lessons
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.