Skip to content

CDN

A CDN is a worldwide network of servers that store copies of content closer to users so that data can be delivered faster. The main reason it exists is physics. Data travels through cables at a speed limited by the speed of light, so a request from India to a server in the US takes a noticeable amount of time just because of distance, and every extra network hop and every round trip needed to set up a connection makes it worse. Instead of making every user fetch content from one faraway server, the content is copied to many locations around the world, so each user gets it from a nearby one. A shorter distance means lower latency, and lower latency means pages, images and videos load faster.

Why use CDN? First benefit is; since users receive content from a nearby server with fewer hops. The second is reduced load on the origin, because most requests are answered by edge servers and the origin only handles misses, so it needs less capacity and costs less to run. The third is availability and scalability: a traffic spike, such as a viral video or a sale, is absorbed by the whole network instead of overwhelming one server, and if the origin has a short outage, edges can often keep serving cached content. The fourth is security, since the origin's real address can be hidden behind the CDN, and the CDN's large capacity can absorb DDoS attacks, with many also offering a web application firewall and managed TLS certificates.

Core Components

The origin server is where the original content actually lives. It is the source of truth, and it is usually the company's own servers or cloud storage. Everything else in the CDN is derived from it.

An edge server is a CDN server located near users, which keeps cached copies of content from the origin. When a user requests something, they are served by the nearest edge server and not by the origin. The locations where edge servers sit, usually inside internet exchange points and ISP networks, are called points of presence (PoPs). Large CDNs such as Cloudflare, Akamai, Amazon CloudFront and Fastly operate hundreds of these locations across the world.

Flow

When a user opens a page, their browser first resolves the domain through DNS. For a site using a CDN, the DNS answer does not point to the origin. It points to the CDN, and the CDN's DNS uses GeoDNS, latency-based routing or anycast to return the address of the edge server nearest to the user. This is where DNS, load balancing and CDNs all connect.

The browser then sends its request to that edge server, and one of two things happens. On a cache hit, the edge already has a fresh copy and returns it immediately, and the origin is never contacted. On a cache miss, the edge does not have the content or its copy has expired, so it fetches it from the origin, returns it to the user, and stores a copy for the next user who asks. The first user in a region pays the cost of the miss, and everyone after benefits from the hit. The share of requests served from the cache is called the cache hit ratio, and it is the main measure of how well a CDN is working.

Pull and Push CDNs

In a pull CDN, the edge servers fetch content from the origin only when a user first requests it, and then cache it. This is the most common model because it needs almost no setup and only stores content that is actually used, but the first request for each item is slower. In a push CDN, the content owner uploads content to the CDN in advance and decides what is stored and where. This avoids the slow first request and suits content that rarely changes and is known ahead of time, such as software downloads or a video release, but it requires more management and can waste storage on content nobody requests.

Rules in CDN

What gets cached

CDNs work best with static content that is the same for every user: images, videos, CSS, JavaScript files, fonts and downloadable files. Videos and large files are the biggest gain, since they use the most bandwidth. Dynamic content, such as a personalized dashboard or the result of a database query, is harder to cache because it differs per user or changes constantly. Many CDNs still help with dynamic content by keeping optimized, persistent connections to the origin and by routing traffic over faster paths, and some can cache short-lived dynamic responses or run small pieces of code at the edge.

Caching and TTL

How long an edge may keep a copy is controlled by the origin through HTTP headers such as Cache-Control and Expires, which set a TTL in the same way DNS records do. A long TTL gives a high hit ratio and less load on the origin, but users may see outdated content after it changes. A short TTL keeps content fresh but sends more requests to the origin. A common technique is to put a version or hash in the file name, such as app.3f9a1.js, so that a new version is treated as a completely new file and the long TTL on the old one does not matter.

Sometimes content must be updated before its TTL expires, for example when a wrong price or a broken image has to be fixed immediately. For this, CDNs provide a purge (invalidation) feature that tells all edge servers to drop a specific file or a group of files. This is known to be hard in practice because the copies are spread across hundreds of servers worldwide, so a purge takes some time to propagate, and frequent purging reduces the benefit of caching. Versioned file names are usually preferred to purging for this reason. Since users may receive an old copy until the TTL expires or a purge completes. Dynamic and personalized content cannot be cached the same way, so the benefit is smaller for those. There is also a cost, since CDNs usually charge for the amount of data transferred, and a poorly configured cache with a low hit ratio can still send a lot of traffic to the origin. Finally, relying on one CDN provider creates a dependency, and a provider outage can take down many sites at once, so some large systems use more than one CDN.

Origin Shield and Tiered Caching

If hundreds of edge servers each miss on the same new file at the same time, they would all hit the origin together. To prevent this, many CDNs add an extra caching layer between the edges and the origin, called an origin shield or tiered cache. Edges ask this middle layer first, and only the middle layer talks to the origin, so the origin sees one request instead of hundreds. This greatly reduces origin load and improves the hit ratio.