# Content Delivery Networks

> How the web delivers static assets globally at the speed of light.

Subject: Internet | Lesson 22 of 35 | 4 min read
Interactive lesson: https://codewithanimation.in/internet/cdns
Previous: [Web Servers vs. App Servers](https://codewithanimation.in/internet/web-vs-app-servers.md) | Next: [Edge Caching vs. Browser Caching](https://codewithanimation.in/internet/edge-caching.md)

## What it is

A Content Delivery Network (CDN) is a geographically distributed group of servers optimized to deliver static content quickly to users.

**Origin Server**: The primary server where the definitive versions of your website's files live.

**Edge Locations (PoPs)**: The distributed network of proxy servers located in data centers around the world. These servers sit at the 'edge' of the network, physically closer to end-users, and cache copies of the origin server's files.

## Why it matters

Physical distance equals time. Without a CDN, a user in Tokyo requesting a site hosted on an Origin Server in New York suffers high latency.

For frontend developers, a CDN is critical because it drastically reduces the Time to First Byte (TTFB) and accelerates asset delivery. Faster delivery translates to better Core Web Vitals and a seamless user experience.

## How it works

When a user requests a website's asset, DNS routing seamlessly directs the request to the nearest optimal CDN node (PoP).

**Cache Hit**: If the nearest PoP has a fresh copy of the asset, it serves it immediately to the user.

**Cache Miss**: If the PoP does not have the asset, it routes the request to the Origin Server, retrieves the asset, delivers it to the user, and saves (caches) a copy locally for future requests from that region.

## Common mistake: Cache Invalidation

The #1 common mistake for developers is updating a file (like `styles.css`) on the Origin Server but forgetting to invalidate or purge the CDN cache.

As a result, users continue seeing the old, cached version. This is why modern frameworks use 'cache busting' by appending a unique hash to filenames (e.g., `styles.a3f9b.css`).

## Tip: Pro tip

Think of your Origin Server as the main manufacturing factory, and the CDN PoPs as local distribution warehouses in every major city.

Instead of shipping every individual customer's order straight from the factory across the globe, the factory bulk-ships to local warehouses. When a user requests your site, they are served instantly from the warehouse down the street.

## Try it: Try it

Toggle the CDN on and off to see how latency changes across the globe.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/cdns

## Check yourself

### Question 1

What is the primary purpose of a CDN?

- A. To replace the need for an origin database.
- B. To reduce latency by serving cached assets from edge servers located physically closer to the user.
- C. To execute complex backend business logic.
- D. To increase the distance between the server and the user for security.

Correct answer: B

A CDN's primary purpose is to cache assets at edge locations close to users, dramatically reducing latency.

- Why not A: CDNs cache static files for delivery; they do not replace backend transactional databases.
- Why not C: The primary goal of a CDN is fast asset delivery.
- Why not D: CDNs decrease the distance to the user to improve speed.

### Question 2

What happens during a 'cache miss' on a CDN?

- A. The CDN edge server requests the asset from the origin server, serves it to the user, and caches it for future requests.
- B. The user receives a 404 Not Found error immediately.
- C. The request is dropped to protect the origin server.
- D. The user's browser is forced to guess the content.

Correct answer: A

On a cache miss, the CDN retrieves the asset from the origin server, delivers it, and caches it to ensure the next request in that region is a cache hit.

- Why not B: The CDN will check the origin server first.
- Why not C: CDNs forward legitimate requests on a cache miss.
- Why not D: Browsers cannot guess missing files.

### Question 3

Which of these is a common misconception about using a CDN?

- A. CDNs can help mitigate DDoS attacks.
- B. Updating a file on the origin server instantly updates it for all users worldwide.
- C. CDNs are useful for serving images and video files.
- D. CDNs can reduce the load on the origin server.

Correct answer: B

CDNs cache files based on time-to-live (TTL) rules. Simply changing a file on the origin won't update the CDN until the cache expires or is explicitly purged.

- Why not A: This is true; CDNs provide robust DDoS protection.
- Why not C: This is true; large media assets benefit immensely.
- Why not D: This is true; the CDN handles the majority of requests.

## Remember this

- CDNs reduce latency by bringing content physically closer to the user.
- A 'cache miss' requires a round-trip to the Origin Server.
- Always use file hashing to avoid cache invalidation headaches.
