Cloud Infrastructure

How Content Delivery Networks Speed Up Websites

Content delivery networks keep copies of site content in many locations so pages load from a nearby server. Learn how caching, edge servers and routing work.

On this page
  1. The distance problem
  2. The basic idea
  3. Following one request
  4. What gets cached
  5. Keeping copies fresh
  6. Other benefits
  7. Limits to keep in mind
  8. How it relates to edge computing
  9. What this means for visitors

The distance problem

When you open a web page, your device sends requests to a server and waits for the answer. Each round trip takes time, and part of that time depends on distance. Signals in fiber travel quickly but not instantly, and a request may pass through many routers on the way. If the server is on another continent, every round trip is noticeably slower than if it were in your own city.

A single web page can involve dozens of requests for images, scripts, style sheets and fonts. Those delays add up. This is the kind of delay explained under network latency, and a content delivery network, or CDN, is one of the main tools for reducing it.

The basic idea

A CDN is a network of servers spread across many locations. Instead of every visitor fetching files from the website’s single home server (the origin), the CDN keeps copies of files on servers closer to visitors. These nearby servers are often called edge servers or points of presence.

When you load a page, your request is directed to a nearby CDN server. If it already has a copy of the file, it sends it straight to you. If not, it fetches the file from the origin, passes it to you, and typically keeps a copy for the next visitor.

Following one request

Roughly, this is what happens:

  1. You type a site address or click a link. Your device looks up the name through the domain name system, as described in how the internet works.
  2. The lookup returns an address belonging to the CDN, not to the origin server. The CDN uses its routing setup to point you to a server that is nearby or otherwise well placed.
  3. Your browser connects, usually over an encrypted connection, so HTTPS encryption can be handled by the CDN server.
  4. The CDN server checks its storage. A cache hit means the file is there and still valid, so it is sent immediately. A cache miss means it must be requested from the origin first.
  5. After the first visitor in an area triggers a miss, later visitors in that area tend to get hits.

Because the heavy lifting is shared, the origin sees far fewer requests.

What gets cached

CDNs are best at content that is the same for many visitors:

  • images, video segments and audio files
  • style sheets and scripts
  • fonts
  • downloadable files and software updates
  • complete pages that rarely change, such as many articles

Content that is personal, such as account pages or shopping carts, is harder to cache because each user sees something different. Many CDNs can still help with such sites by speeding up the connection path or caching the non-personal parts, while the personal parts come from the origin.

Keeping copies fresh

A copy is only useful if it is not out of date. Website owners set rules telling caches how long a file may be reused, often called a time to live. After that period, the CDN checks whether the origin has a newer version. Owners can also ask the CDN to clear specific files when they change. Another common technique is to give files new names when their content changes, so old and new versions never get confused.

Setting these rules involves a trade-off. Long cache times give faster loads and less origin traffic, but a risk of people seeing outdated content for a while. Short cache times keep things fresh but reduce the benefit.

Other benefits

Speed is the headline, but CDNs often provide more:

  • Handling traffic spikes. When many people request a popular file at once, the load is spread across many servers instead of overwhelming one.
  • Resilience. If one location has trouble, traffic can be routed elsewhere, though problems with the CDN itself can affect many sites at once, as discussed under cloud outages.
  • Protection against floods of traffic. Because a large network can absorb a lot of requests, many CDNs also offer defenses against attacks designed to overwhelm a site.
  • Lower bandwidth costs for the origin. Less data leaves the origin server.
  • Optimization. Some CDNs compress files or convert images to smaller formats suited to the visitor’s device.

Streaming services and large software publishers depend heavily on this approach, because distributing big files from one location would be slow and expensive.

Limits to keep in mind

A CDN is not a cure for every slow site. If a page is slow because of heavy scripts, large uncompressed images or slow database queries on the origin, a CDN will only partly help. Dynamic content that cannot be cached still has to travel to the origin. Cache settings that are misconfigured can show visitors stale pages or, worse, serve the wrong content, so setup matters.

There is also a dependency: relying on one CDN means relying on its availability, and some organizations use more than one for that reason.

How it relates to edge computing

The CDN idea of putting something useful near users has been extended. Edge computing goes further by running programs, not just storing files, at those nearby locations, so small pieces of logic can run close to the user. Many CDN providers now offer both.

What this means for visitors

Most of the time, you will never notice a CDN. Pages simply feel quicker, videos start sooner and downloads are steadier. When a site is hosted far away but still loads promptly, there is a good chance copies of its content are sitting on a server near you.

The complete guide to cloud infrastructure

How Cloud Infrastructure Works Behind the Scenes
Read the guide

Keep exploring

Guides from other topics

All topics →

How Mobile Apps Are Built

A plain-language walk through the stages of mobile app development, from the first idea and design sketches to testing, release and ongoing maintenance.

Mobile Apps 5 min read