0% completed
What is CDN?
On This Page
What a CDN Is
Why Serving Everyone From One Place Is Slow
Key Terms: PoP, Edge Server, and Origin Server
How a CDN Serves a Request
How Users Reach the Nearest Edge Server
Controlling What Stays in the Cache
Benefits of a CDN
What to Put on a CDN
Key Takeaways
Practice Questions
A video website stores all its videos on servers in New York. A viewer in Paris presses play. The video starts slowly and pauses to load, even though the servers in New York are fast and not busy.
The problem is distance. Every piece of the video must cross the Atlantic Ocean, and each round trip between Paris and New York takes about 80 ms. No faster server can remove that travel time.
The fix is to keep copies of the video close to the viewers. This lesson explains the system that does that, how it serves each request, and the terms used to describe it.
What a CDN Is
A Content Delivery Network (CDN) is a distributed network of servers placed in many geographic locations. It is used to deliver web content efficiently by serving each user from a server near them.
A CDN mostly delivers static content, which is content that is the same for every user. Examples are images, videos, stylesheets, scripts, and files for download.
The primary purpose of a CDN is to reduce latency and improve performance, by serving content from the server nearest to the user. Most other benefits of a CDN come from this idea.
Why Serving Everyone From One Place Is Slow
Suppose every request goes to one origin server, the server that stores the original content. Two problems appear for a global audience.
- Distance. Users far from the origin wait longer, because each request and response travels farther.
- Load. One server must answer every user in the world, so it can become overloaded during busy times.
A CDN solves both. It keeps copies of the content near users, and it answers most requests itself, so the origin does much less work.
Key Terms: PoP, Edge Server, and Origin Server
Three terms describe where things are in a CDN.
| Term | What it is |
|---|---|
| Point of Presence (PoP) | A physical location, usually a data center, where a group of CDN servers is deployed |
| Edge server | A CDN server in a PoP that caches content and delivers it to nearby users |
| Origin server | The primary server where the original content is stored, and where the CDN fetches it from |
A PoP is a place, not a single machine. Each PoP usually holds several edge servers for capacity and redundancy. PoPs are placed close to large groups of users to keep latency low. A large CDN has hundreds of PoPs around the world.
The origin server belongs to the website. The PoPs and edge servers belong to the CDN provider. Common CDN providers include Cloudflare, Akamai, Amazon CloudFront, and Fastly.
How a CDN Serves a Request
Here is what happens when the viewer in Paris requests the video on a website that uses a CDN.
- The request is routed to a nearby edge server. The CDN picks a good edge server for the user, usually in a PoP in or near Paris. The choice is based on network latency and how busy each server is, not only on distance.
- The edge server checks its cache.
- If the content is cached, the edge server serves it immediately from the cache. This is the fast path.
- If the content is not cached, the edge server fetches it from the origin server in New York. It stores a copy in its cache, and then serves it to the user.
So the first request for a piece of content in a region must wait for the trip to the origin. The requests after it are served from the nearby edge server. This is also why a CDN reduces the load on the origin server: the origin no longer answers every request.
How Users Reach the Nearest Edge Server
CDNs use several routing methods to send each user to a good edge server.
- DNS-based routing. The CDN's DNS server answers each lookup with the IP address of a suitable edge server for that user.
- Anycast. Many edge servers share the same IP address. The network sends each request to the nearest one, based on the lowest latency or the shortest network path.
The DNS load balancing lesson explains both methods. The Origin Server vs. Edge Server lesson covers routing in more detail.
Controlling What Stays in the Cache
A CDN must decide how long to keep each copy, and what to do when the original changes. These terms describe those decisions.
Time to Live (TTL). TTL is a value that sets how long a piece of content stays in the cache. After that time, the content is considered stale, and it must be refreshed from the origin server. The origin usually sets the TTL with a response header, like this one.
Cache-Control: public, max-age=86400
Here, max-age=86400 means the content may be cached for 86,400 seconds, which is one day.
Cache warming. Cache warming means preloading content into edge servers' caches before any user requests it. It makes sure the very first request is already fast. Teams often warm the cache before a big launch.
Content invalidation. Content invalidation means removing or updating cached content when the original content on the origin server changes. Without it, users would keep getting the old version until the TTL runs out.
Cache purging. Cache purging means forcibly removing content from an edge server's cache. It is usually triggered manually, or automatically when certain conditions are met.
Many teams avoid stale files in a simpler way. They put a version in each file name, like app.v42.js. When the file changes, its name changes too. The CDN treats it as new content, so no invalidation is needed.
Benefits of a CDN
A CDN brings five main benefits.
- Reduced latency. Content travels a shorter distance, so pages and videos load faster.
- Improved performance. The CDN delivers the static content, so the origin server has more resources for dynamic content, and the origin's load drops.
- Reliability and availability. A CDN has many edge servers in many places. If one server becomes unavailable, requests are sent to another, and users still get the content.
- Scalability. A CDN handles sudden traffic spikes and very large numbers of requests, so a website can grow more easily.
- Security. Many CDNs add protection against denial of service attacks, which try to overload a website with fake traffic. They can also run a Web Application Firewall (WAF), which blocks harmful requests, and handle SSL/TLS encryption at the edge.
Here is the effect on the origin in numbers. Suppose a website receives 1,000,000 requests per hour, and the CDN serves 95 percent of them from its caches. Only 50,000 requests per hour reach the origin server.
What to Put on a CDN
A CDN works best for content that is the same for many users and changes rarely.
- Good fits: images, videos, stylesheets, scripts, fonts, and files for download.
- Sometimes a fit: public pages that change slowly, like a news article, cached for a short TTL.
- Poor fits: personal content, like a user's shopping cart or account page, which is different for every user. The CDN can still pass these requests to the origin, but it should not cache them.
Key Takeaways
- A CDN is a distributed network of servers in many geographic locations that delivers web content efficiently.
- Its primary purpose is to reduce latency and improve performance, by serving content from the server nearest to the user.
- A PoP is a physical location with a group of CDN servers. An edge server in a PoP caches and delivers content. The origin server stores the original content.
- A request goes to a nearby edge server. The edge server serves cached content immediately. Otherwise it fetches the content from the origin, caches it, and serves it.
- TTL sets how long content stays cached. Cache warming preloads content, content invalidation removes outdated copies, and cache purging forcibly removes content.
- CDNs reduce latency, lower the origin's load, improve reliability and scalability, and add security.
A CDN moves copies of content close to users, so distance stops slowing users down. The next lesson, Origin Server vs. Edge Server, looks more closely at these two kinds of server.
Practice Questions
Try each question first, then open the answer.
1. A user in Paris requests a video. The website's origin server is in New York, and the site uses a CDN. What most likely happens?
<details> <summary>Show answer</summary>The request goes to an edge server in or near Paris. If that edge server has the video cached, it serves the video immediately. If not, it fetches the video from the origin in New York, caches a copy, and serves it. Later viewers near Paris get the video from the cache.
</details>2. A website sets a TTL of one day on its logo, and then changes the logo. Users still see the old logo. Name two ways to fix this.
<details> <summary>Show answer</summary>Invalidate or purge the cached logo, or give the new logo a new file name. Invalidation or purging removes the old copy from the edge servers before its TTL ends, so they fetch the new logo. A new file name, like logo-v2.png, is treated as different content, so the edge servers fetch it fresh.
3. A company launches a new video at 9:00 and expects millions of views in the first few minutes. How can it make sure the first viewers do not wait for trips to the origin?
<details> <summary>Show answer</summary>By warming the cache before the launch. The team preloads the video into edge servers' caches before 9:00. The first viewers are then served from nearby edge servers. The origin also avoids a sudden large number of cache misses at launch time.
</details>4. A website receives 1,000,000 requests per hour, and its CDN serves 95 percent of them from cache. How many requests per hour reach the origin server?
<details> <summary>Show answer</summary>50,000 requests per hour. 95 percent are served by the CDN, so 5 percent reach the origin. 5 percent of 1,000,000 is 50,000. The origin handles one twentieth of the traffic it would handle without the CDN.
</details>5. Which of these should not be cached by a CDN: (a) product images, (b) a user's shopping cart page, or (c) the website's JavaScript files?
<details> <summary>Show answer</summary>(b) the shopping cart page. It is different for every user, and it changes often, so a cached copy could show one user's cart to another. Product images and JavaScript files are the same for everyone and change rarely, which makes them good fits for a CDN.
</details>Reading Progress
0%
On This Page
What a CDN Is
Why Serving Everyone From One Place Is Slow
Key Terms: PoP, Edge Server, and Origin Server
How a CDN Serves a Request
How Users Reach the Nearest Edge Server
Controlling What Stays in the Cache
Benefits of a CDN
What to Put on a CDN
Key Takeaways
Practice Questions