# 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.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/content-delivery-networks

## 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.

- **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.

```python
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:

```text
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?

A. A second database server in New York, so the photos are read from two places in the same city
B. A CDN with edge servers near Australia, so the photos are sent from a nearby copy after the first request
C. A larger New York server, so each photo leaves the building faster
D. Turning off all caching, so every photo is always fetched fresh from New York

**Answer:** B. The slow part is the long trip from New York to Australia. An edge near Australia keeps copies of the photos, so most Australian requests get a nearby answer and the New York server handles far fewer requests.

## 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?

A. The new chart at once, because the origin always wins over every edge copy
B. The old chart until the stored copy expires or is purged from that edge
C. The new chart, because the CDN deletes old copies when the origin file is saved
D. The new chart every time, because every view is sent back to the origin

**Answer:** B. A stored copy stays in use until its TTL ends or someone purges it. Giving the fixed chart a new file name would have avoided this, because the new address is a new request.

## 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?](https://www.cloudflare.com/learning/cdn/what-is-a-cdn/)
- [Akamai: What is a Content Delivery Network (CDN)?](https://www.akamai.com/glossary/what-is-a-cdn)
- [TechTarget: What is a content delivery network?](https://www.techtarget.com/searchnetworking/definition/CDN-content-delivery-network)
- [NOC.org: Content Delivery Networks (CDN) Explained](https://noc.org/learn/what-is-a-cdn)
- [Network Solutions: What is a CDN? How content delivery networks work](https://www.networksolutions.com/blog/what-is-cdn/)
- [Cloudoptimo: How CDN Works: Request Routing, Caching, and Edge Delivery](https://www.cloudoptimo.com/blog/how-cdn-works-request-routing-caching-and-edge-delivery/)
- [CDNetworks: How Content Delivery Networks Work](https://www.cdnetworks.com/blog/web-performance/how-content-delivery-networks-work/)
- [System Design Interview Handbook: 2.5 Content Delivery Networks (CDN)](https://www.systemdesigninterview.com/guides/system-design-interview-handbook/25-content-delivery-networks-cdn)
- [TechInterview: Design a CDN (Content Delivery Network)](https://www.techinterview.org/post/3233460054/design-a-cdn-content-delivery-network/)
- [SpaceComplexity: CDN and Edge Caching for System Design](https://spacecomplexity.ai/blog/cdn-edge-caching-system-design)
- [CodeWithVenu: CDN Fundamentals for System Design](https://codewithvenu.com/blog/SystemDesign/02-Networking-And-Web-Fundamentals/04-CDN-Fundamentals)
- [Netflix Open Connect: Embedded appliances](https://openconnect.netflix.com/en/)
- [ByteByteGo: Netflix, What Happens When You Press Play, Part 2](https://blog.bytebytego.com/p/netflix-what-happens-when-you-press-288)
