Content Delivery Network (CDN)

A Content Delivery Network (CDN) is a geographically distributed network of edge servers (Points of Presence, or PoPs) that cache and serve content from locations close to each visitor. Instead of every user sending requests across continents to your origin server, the CDN answers from a nearby city — dramatically cutting round-trip latency and offloading bandwidth from your origin.

How a CDN Works

When a visitor requests a page or asset, Anycast routing or Geo-DNS sends the request to the closest healthy CDN edge server:

  • Cache Hit: The edge server already holds a fresh copy of the asset and returns it directly to the user in ~10–30 ms, never touching your origin server.
Cache hit: a visitor in Mumbai asks the CDN edge server in Mumbai, which answers from its cache in about 15 ms; the origin server in US-East isn’t asked Cache hit: a visitor in Mumbai asks the CDN edge server in Mumbai, which answers from its cache in about 15 ms; the origin server in US-East isn’t asked
Cache hit: the nearest edge server answers from its cache, here in about 15 ms, and the origin is never asked · Made with Akriti
  • Cache Miss: The edge server fetches the asset from your origin server (or object storage bucket like S3/R2), delivers it to the user, and stores a copy locally so subsequent visitors in the same region get a fast cache hit.
Cache miss: a visitor in Mumbai asks the CDN edge server in Mumbai, which doesn’t have the asset yet, so it fetches it from the origin server in US-East and keeps a copy Cache miss: a visitor in Mumbai asks the CDN edge server in Mumbai, which doesn’t have the asset yet, so it fetches it from the origin server in US-East and keeps a copy
Cache miss: the edge fetches the asset from the origin, here across the world in about 180 ms, and keeps a copy for the next visitor nearby · Made with Akriti

Behind the scenes, CDNs combine edge caching, global load balancing, TLS termination near the user, and automatic Brotli/gzip compression.

Push CDN vs. Pull CDN

CDNs populate their edge servers in two main ways:

Dimension Pull CDN (Most Common) Push CDN
How content reaches the edge Edge fetches from your origin automatically on the first cache miss You proactively upload assets to CDN storage whenever content changes
First-request latency First visitor in each region experiences one cache miss Fast from the very first request (pre-warmed)
Best for Websites, APIs, static bundles, blogs, high-traffic pages Large media files (video, installers, game patches) where misses are expensive
Maintenance Minimal — governed by HTTP Cache-Control headers Higher — requires deployment scripts to push and delete files

Controlling What a CDN Caches

Edge servers decide what to cache and for how long using HTTP response headers sent by your origin:

  • Cache-Control: public, max-age=31536000, immutable: Ideal for fingerprinted static assets (main.9f3a2c.css, hashed images) that never change at the same URL.
  • Cache-Control: public, s-maxage=300, stale-while-revalidate=60: Tells the CDN (s-maxage) to cache HTML or public API responses for 5 minutes while serving a slightly stale copy during background refresh.
  • Cache-Control: private, no-store: Prevents the CDN from caching personalized, authenticated, or sensitive responses.

When content changes before its TTL expires, you either version the filename (cache busting, the safest approach) or trigger a CDN cache purge via API. (For broader invalidation trade-offs, see Cache Invalidation.)

Benefits of Using a CDN

  1. Lower Latency: Shortening the physical network distance cuts both TCP/TLS handshake time and transfer latency — especially for visitors far from your origin region.
  2. Origin Offload & Cost Savings: When 90%+ of static asset requests are served from the edge, your origin servers and database handle far fewer requests and lower egress bandwidth bills.
  3. DDoS Protection & Security: Large CDN edges absorb volumetric DDoS attacks, enforce Web Application Firewall (WAF) rules, and terminate TLS before malicious traffic ever reaches your backend load balancer.
  4. High Availability: If one edge PoP goes offline, Anycast BGP routing shifts traffic to the next-nearest healthy PoP automatically. Many CDNs can also serve stale cached pages briefly if your origin goes down (an AP-like trade-off in CAP theorem terms).

Implementing a CDN

Setting up a pull CDN in front of a web app typically takes four steps:

  1. Choose a provider: Cloudflare, Amazon CloudFront, Fastly, or Akamai.
  2. Point DNS to the CDN: Update your domain’s nameservers or add a CNAME record pointing to the CDN distribution.
  3. Configure TLS & caching rules: Enable HTTPS at the edge, set Cache-Control headers on your origin, and ensure personalized routes are excluded from shared caching.
  4. Verify cache hits: Inspect response headers (CF-Cache-Status: HIT on Cloudflare or X-Cache: Hit from cloudfront on AWS) in browser DevTools to confirm requests are hitting the edge.

FAQs

What types of content can a CDN cache? CDNs excel at caching static files (images, video, fonts, CSS, JavaScript, and static HTML). They can also cache public, non-personalized API responses (such as product catalogs or documentation search indexes) using short TTLs and s-maxage headers. Personalized or authenticated responses should be marked Cache-Control: private, no-store.
How does a CDN differ from a load balancer? A CDN caches and serves content from globally distributed edge PoPs close to users to cut geographic latency and offload origin traffic. A load balancer typically sits in front of your backend servers inside a region to distribute dynamic requests evenly across healthy instances. Most production systems use both together.
How does a CDN handle SSL/TLS certificates? CDN providers provision and automatically renew edge TLS certificates (often free via Let’s Encrypt or managed CAs). Terminating TLS at the nearby edge cuts several cross-continent round trips during the HTTPS handshake compared to terminating at a distant origin.
When can a CDN serve stale data? If you overwrite a file at the same URL without versioning its filename or purging the CDN cache, edge PoPs continue serving the cached version until its max-age TTL expires. Using content-hashed filenames (like app.a1b2c3.js) eliminates stale-asset bugs completely.