Content Delivery Networks: How a CDN Makes a Global Site Fast
Why distance creates latency, how edge servers cache content, what can and cannot be cached, TTLs and versioned file names, origin shielding and DDoS absorption.
A content delivery network (CDN) is a geographically distributed set of servers that store copies of your content close to users. When a user requests an image, a script or a video chunk, it is delivered from a nearby server instead of from your own servers, which may be on another continent.
If a site hosted in one country loads slowly for users in another, the cause is usually not server capacity, and a more powerful server will not fix it. The cause is distance. These notes explain why, and how a CDN addresses it.
Latency comes from distance
Data travels through fibre at roughly two thirds of the speed of light, and it does not travel in a straight line; it passes through many routers and networks. A round trip between India and the east coast of the United States takes around 200 ms or more.
Loading a page over HTTPS requires several round trips before any content arrives: one for the TCP handshake, one or two for the TLS handshake, then the request and response. After that, a typical page requests dozens of further files. A server that responds in 5 ms still delivers a slow page if every exchange spends 200 ms on the wire. Upgrading the server's CPU changes the 5 ms, not the 200 ms.
Edge servers and the origin
Your own servers, where the content is created and stored, are called the origin. The CDN operates edge servers in many locations around the world, called points of presence (PoPs).
A user's request is directed to a nearby edge server, either by DNS, which returns the address of a close PoP, or by anycast routing, where the same IP address is announced from every PoP and the network delivers the request to the nearest one. If the edge server has the requested file in its cache, it returns it immediately. This is a cache hit, and the round trip is a few milliseconds. If it does not, a cache miss, the edge fetches the file from the origin, returns it to the user and stores it for subsequent requests.
User in Mumbai ---> Edge PoP (Mumbai) ---- cache HIT ----> response in ~10 ms
|
| cache MISS
v
Origin shield (regional cache)
|
| miss
v
Origin (app servers / object storage, e.g. in Virginia)
The first user in a region pays the trip to the origin.
Everyone after that, until the TTL expires, is served from the edge.What a CDN can cache
Caching works for content that is the same for every user.
- Static content: images, CSS, JavaScript bundles, fonts, video chunks, downloadable files. These are ideal and should always be served through a CDN.
- Public pages and API responses that are identical for everyone and change infrequently: a product page, an article, a list of categories. These can be cached for a short time.
- User-specific content: a cart, an account page, a personalised feed. These must not be cached at the edge as shared content, or one user would be shown another user's data. They are marked private or not cacheable.
Controlling the cache: TTL and headers
The origin tells the CDN and the browser how long a response may be reused through the Cache-Control header. The lifetime is the TTL. Until it expires, the cached copy is served without contacting the origin.
A long TTL means more hits and less load on the origin, but changes take longer to appear. A short TTL means fresher content and more origin traffic. When a cached copy expires, the edge can revalidate it with a conditional request using an ETag: if the file has not changed, the origin replies 304 Not Modified with no body, which is much cheaper than sending the file again.
# Fingerprinted static asset: cache for a year, never revalidate Cache-Control: public, max-age=31536000, immutable # HTML page: always check with the origin before reuse Cache-Control: no-cache # Public API response: browser 60 s, CDN 5 min, serve stale while refreshing Cache-Control: public, max-age=60, s-maxage=300, stale-while-revalidate=30 # Personal data: only the user's own browser may store it Cache-Control: private, no-store
Updating cached files: versioned file names
Suppose style.css is cached for a year and you deploy a new design. Users and edge servers continue to use the old file. One option is to purge the file from the CDN, which sends an invalidation to every edge. Purging works but takes time to propagate and does not reach copies already stored in browsers.
The standard solution is versioned file names, also called cache busting. The build process puts a hash of the file's contents in its name, for example style.3f9a1c.css. When the content changes, the name changes, so it is a new URL that no cache has seen. The HTML, which has a short TTL, refers to the new name. Old and new versions coexist safely, each file can be cached forever, and a rollback is simply the HTML pointing to the previous name again.
Pull and push CDNs
With a pull CDN, the edge fetches content from the origin on the first request and caches it. Setup is minimal and only content that is actually requested is stored. The first request in each location is slower. This is the common choice for websites.
With a push CDN, you upload content to the CDN in advance. Nothing is fetched on demand, so even the first request is fast. It requires managing uploads and suits large files released on a schedule, such as software downloads or new video releases.
Origin shield
A CDN may have hundreds of PoPs. When a popular file expires or a new file is released, each PoP independently misses and requests it from the origin, producing a burst of identical requests.
An origin shield is an additional caching layer between the edge servers and the origin. Edges send their misses to the shield, and the shield requests the file from the origin once and serves all the edges. This increases the overall hit ratio and protects the origin from miss storms.
Benefits beyond speed
- Reduced origin load: most requests never reach your servers, which lowers compute and bandwidth cost.
- Availability: if the origin is briefly down, the CDN can continue serving cached content, including stale copies if configured.
- DDoS absorption: an attack is spread across the CDN's large global capacity and filtered at the edge instead of concentrating on your origin.
- TLS termination close to the user, which shortens the handshake, plus compression and image optimisation at the edge.
- Acceleration of dynamic requests: even uncacheable requests benefit from the edge's persistent, optimised connections to the origin.
Limitations and trade-offs
- Stale content if TTLs and invalidation are not planned.
- Risk of caching private data if headers are wrong. Cache keys must include whatever makes responses differ.
- Cost is based on data transferred and can be significant for video.
- Rarely requested content has a low hit ratio and gains little.
- A CDN does not make slow origin logic or uncacheable database queries faster.
Interview summary
- Latency for distant users comes from distance, not from server power.
- A CDN caches content on edge servers near users; the origin is contacted only on a miss.
- Cache static and public content; never cache user-specific responses as shared.
- Control freshness with TTLs, and deploy changes with versioned file names.
- Extras: origin shield to protect the origin, DDoS absorption, availability when the origin fails.
Summary
A CDN removes distance from the request path by serving cached copies from edge servers near the user. It suits static and public content, is governed by Cache-Control and TTLs, and is updated reliably through versioned file names. It also reduces origin load, improves availability and absorbs attacks, which makes it a standard part of any read-heavy design.
Puneet Tiwari
Full Stack Developer
