System Design Fundamentals
Vote

0% completed

What is CDN?

How a CDN Serves a Request

The Words You Need

What You Actually Get

A Content Delivery Network (CDN) is a distributed network of servers placed in many geographic locations, used to deliver web content to each user from a server near them.

The content is usually static: images, videos, stylesheets, scripts, and other files that look the same for everyone.

The reason a CDN exists is distance. A request travels to a server and the response travels back. If that server is on another continent, the trip takes time no matter how fast the machine is. A CDN keeps a copy of the content close to the user so the trip is short.

Without a CDN every user reaches the one origin server, and with a CDN each user is served from a nearby copy
Without a CDN every user reaches the one origin server, and with a CDN each user is served from a nearby copy

The primary purpose of a CDN is to reduce latency and improve performance by serving content from the server nearest the user. Every other benefit is built on that one idea.

How a CDN Serves a Request

When a user asks for content, the request is routed to the nearest CDN server, called an edge server. Nearest here is not only about geography. The routing also weighs network latency and how loaded each server is.

The edge server then checks whether it already has a cached copy of what was asked for.

If it does, the content is served straight from the cache. This is the fast path, and it is the one you want most requests to take.

If it does not, the edge server fetches the content from the origin server, stores it in its own cache, and then serves it to the user. The next person who asks for that same content gets it from the cache instead.

On a cache hit the edge server answers immediately, and on a miss it fetches from the origin, caches the copy, then answers
On a cache hit the edge server answers immediately, and on a miss it fetches from the origin, caches the copy, then answers

So the first request for a piece of content pays the full trip to the origin. Every request after that is served locally. That is also why a CDN reduces load on the origin server: it stops being the machine that answers everyone.

The Words You Need

Three of the terms are about places.

TermWhat it is
Point of Presence (PoP)A physical location, usually a data center, where a group of CDN servers is deployed
Edge serverA CDN server inside a PoP that caches content and delivers it to nearby end users
Origin serverThe primary server where the original content is stored, and where the CDN fetches it from

A PoP is a place, not a machine. It holds several edge servers, and PoPs are positioned close to end users to keep latency low.

The rest of the terms are about how content gets into the cache, how long it stays, and how it leaves.

Cache warming is preloading content into an edge server's cache before any user requests it. The point is to make sure the very first request is already fast instead of paying for a trip to the origin.

Time to Live (TTL) is a value that determines how long a piece of content stays in the cache before it is considered stale and must be refreshed from the origin server.

Content invalidation is removing or updating cached content when the original on the origin server changes. Without it, users would keep receiving an old version until the TTL happened to run out.

Cache purging is forcibly removing content from an edge server's cache. It is usually triggered manually, or automatically when certain conditions are met.

Anycast is a network routing technique that directs a user's request to the nearest available edge server, chosen by lowest latency or shortest network path.

The origin server holds the original content, PoPs hold groups of edge servers, and each edge server holds a cached copy
The origin server holds the original content, PoPs hold groups of edge servers, and each edge server holds a cached copy

What You Actually Get

1. Reduced latency. Content travels a shorter distance, so pages load faster.

2. Improved performance. Static content delivery moves off the origin server. That frees the origin to spend its resources on generating dynamic content, which lowers its load.

3. Reliability and availability. Many edge servers in many locations give you redundancy built in. If one server becomes unavailable, requests are rerouted to another and content keeps flowing.

4. Scalability. A CDN absorbs sudden traffic spikes and large volumes of concurrent requests, so growth is easier to handle.

5. Security. Many CDNs add protection against denial of service attacks, a Web Application Firewall (WAF), and SSL/TLS termination at the edge.

💡 In an interview, be specific about what you are putting on the CDN. "I would use a CDN" is a preference. "Profile images and video segments are identical for every viewer, so I would serve them from a CDN and keep the origin for the feed API" is a design decision, because it says what is cacheable and why.

Key takeaway: A CDN is a distributed network of servers in many locations that delivers content from the one nearest the user, which is how it reduces latency. Requests go to an edge server inside a PoP, which serves from cache if it can and otherwise fetches from the origin, caches the copy, and serves it. Beyond speed it gives you lower origin load, redundancy, scalability, and security at the edge.

The next lesson, Origin Server vs. Edge Server, looks more closely at the two kinds of server and what each one is responsible for.

On This Page

How a CDN Serves a Request

The Words You Need

What You Actually Get