12 - Content Delivery Networks (CDNs) & Edge Caching

Executive Summary: The speed of light in vacuum is 300,000 km/s, but in optical fiber it is roughly 200,000 km/s. A data packet traveling from Tokyo to an origin server in Virginia and back spans ~ 21,600 km, imposing an unavoidable physical minimum round-trip time (RTT) of β‰ˆ 108 msβ€”before any router queueing, TLS handshakes, or server processing. Content Delivery Networks (CDNs) solve this fundamental physics limit by deploying hundreds of Points of Presence (PoPs) worldwide, caching static and semi-dynamic content within milliseconds of end users via Anycast BGP routing.


🏒 Real-world case studies: how Netflix Open Connect, Cloudflare and Google Global Cache deliver content, in REAL_WORLD_EXAMPLES.md.

⏱️ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


πŸ§’ The 30-Second Version (ELI5)

Imagine you live in Tokyo and love a specific brand of chocolate manufactured only in a single factory in Switzerland.

In computer terms: The factory is the Origin Server (e.g., AWS US-East), the corner store is the Edge Point of Presence (PoP), and the chocolate bars are static assets (images, videos, CSS, JS bundles).


πŸ• Everyday Analogy

Concept Everyday Retail Analogy Technical Reality in Distributed Systems
Origin Server The central manufacturing plant in Detroit The primary application servers and S3 storage bucket
Edge PoP Local convenience store 2 blocks from your house Data center facility in Tokyo or London with NVMe SSD servers
Anycast Routing Dialing emergency services (911/112); your phone connects to the closest local dispatch center BGP routes IP packets to the topologically closest CDN edge router
Cache Miss (Origin Pull) Corner store is out of stock; manager calls Detroit factory to ship a restock box Edge PoP does not have the asset; fetches it from Origin over WAN
Cache Hit Item is on the shelf; you walk out in 30 seconds Edge PoP serves asset from local RAM/NVMe in 2–5 ms
Cache-Control: max-age "Best Before" date printed on the milk carton Seconds the browser or CDN is allowed to serve cached content
ETag / 304 Not Modified Calling the store and asking: "Is the 2026 edition catalog still the latest one?" Store answers: "Yes, no changes." Edge verifies content hash; Origin sends 0 byte body with HTTP 304
Cache Purge Factory issues an instant recall notice via telegram to destroy obsolete stock immediately Edge nodes globally invalidate cached URLs via API/webhook in <150 ms

🧩 The Problem It Solves

Without a CDN

  1. The Physics Problem (High Latency): A user in Sydney, Australia accessing a website hosted in Frankfurt, Germany experiences ~ 300 ms RTT. Establishing a TCP connection and TLS 1.3 session requires at least 2 round-trips (600 ms), followed by multiple round-trips for HTML, CSS, JavaScript, and fonts before the screen renders (2,000–4,000 ms total page load).
  2. Origin Bandwidth Exhaustion & Costs: A single viral 5 MB video watched by 2,000,000 users consumes 10 Terabytes of bandwidth directly from the origin server, risking network saturation and massive cloud egress bills.
  3. DDoS Vulnerability: Volumetric Layer 7 DDoS attacks (e.g., 500,000 HTTP requests/sec) hit the origin servers directly, crashing application databases and reverse proxies.

With a CDN

  1. Sub-10ms First Byte (TTFB): Edge PoPs terminate TCP and TLS connections within 5–15 ms of the client's local ISP. Cached assets are served directly from edge NVMe drives.
  2. 95%+ Origin Offload: The origin server only serves the asset once per edge PoP. Subsequent millions of users are served entirely by the CDN.
  3. Massive Global DDoS Absorption: Modern CDN networks (Cloudflare, Akamai, CloudFront) have >100–300 Tbps of global edge capacity, absorbing volumetric attacks at the edge without origin degradation.

πŸ–ΌοΈ Picture It: Architecture & Flow

High-Resolution Edge Caching Animation

Below is an animated trace of a cold cache origin pull vs. an instantaneous edge cache hit:

CDN Edge Caching Animation


Global Request Flow: Anycast BGP to Origin Shield

sequenceDiagram
    autonumber
    actor User as πŸ‘€ Tokyo User
    participant DNS as 🌐 Anycast DNS
    participant Edge as ⚑ Tokyo Edge PoP
    participant Shield as πŸ›‘οΈ Origin Shield (US-West)
    participant Origin as 🏒 Origin Server (US-East)

    User->>DNS: Resolve 'example.com'
    DNS-->>User: Returns Anycast IP (routes to Tokyo PoP)
    
    rect rgb(22, 27, 34)
    Note over User, Edge: Edge Cache MISS Scenario
    User->>Edge: GET /static/bundle.js (3ms)
    Edge->>Shield: Cache Miss -> Request Shield (Tiered Cache)
    Shield->>Origin: Shield Miss -> Origin Pull (80ms)
    Origin-->>Shield: 200 OK + Cache-Control: s-maxage=86400
    Shield-->>Edge: Returns asset & caches in Shield tier
    Edge-->>User: 200 OK (Age: 0, X-Cache: MISS)
    end

    rect rgb(22, 27, 34)
    Note over User, Edge: Edge Cache HIT Scenario (Next 100,000 Users)
    User->>Edge: GET /static/bundle.js (3ms)
    Edge-->>User: 200 OK (Age: 42, X-Cache: HIT) [0 Origin Traffic!]
    end

πŸ”’ Walk-Through with Real Numbers

Let's calculate the latency and financial impact of deploying a CDN for an international SaaS application:

Latency Math: Tokyo User to US-East Origin

One-way propagation delay = (13,000) / (200,000) β‰ˆ 65 ms β‡’ RTT β‰ˆ 130 ms

Cost Math: AWS Egress Savings


βš™οΈ How It Works Under the Hood

1. Request Routing: How Clients Find the Nearest PoP

  1. Anycast BGP Routing (Modern Standard):

    • Multiple edge data centers across all continents announce the exact same IP address (e.g. 104.16.123.96) via Border Gateway Protocol (BGP) to Tier-1 telecom providers.
    • The Internet's core routing infrastructure automatically routes client packets along the shortest autonomous system (AS) path to the topologically closest edge router.
    • Failover is automatic: if Tokyo PoP goes offline, BGP routes withdraw the announcement and traffic shifts seamlessly to Osaka or Seoul.
  2. GeoDNS (Legacy / Multi-CDN Standard):

    • Authoritative DNS server inspects the client's DNS resolver IP (or EDNS Client Subnet) and returns an IP address corresponding to the nearest geographic data center.

2. HTTP Cache-Control Directives (RFC 7234 & RFC 5861)

The origin server controls edge behavior using HTTP response headers:

Directive Where It Applies Meaning
public Browser & CDN Asset is cacheable by intermediate proxies and CDNs, even with HTTP auth.
private Browser only Sensitive user data (e.g. /profile); CDNs must not cache it.
max-age=N Browser & CDN Asset is considered fresh for N seconds.
s-maxage=N CDN Edge only Overrides max-age specifically for shared caches (CDNs). Allows edge to cache for 7 days while browser only caches for 5 minutes!
no-cache Browser & CDN Must revalidate with origin using ETag before serving (does not mean "don't cache").
no-store Browser & CDN Strictly forbids storing in any cache (RAM or disk). Use for bank balances & tokens.
immutable Browser & CDN Asset will never change (e.g. /app.a1b2c3.js). Browser skips revalidation even on page refresh.
stale-while-revalidate=N CDN Edge Serve stale asset immediately from cache, while asynchronously fetching fresh copy from origin in background. Eliminates cache miss latency spikes!

3. Origin Shielding & Tiered Caching

In a global network with 300 edge PoPs, if a cached video expires, all 300 PoPs would simultaneously query the origin server (a miniature Thundering Herd).

Origin Shield (Tiered Cache) places an intermediate caching tier between edge PoPs and the origin:

[Client Tokyo]  ──> [Edge PoP Tokyo]  ──┐
                                         β”œβ”€β”€> [Origin Shield (US-West)] ──> [Origin Server]
[Client Sydney] ──> [Edge PoP Sydney] β”€β”€β”˜

4. Cache Invalidation Strategies

  1. Content-Hash Versioning (Best Practice):
    • Assets are compiled with cryptographic content hashes in filenames: main.d41d8cd9.js.
    • Set Cache-Control: public, max-age=31536000, immutable.
    • Never requires purging: updates simply deploy new filenames!
  2. Surrogate-Keys / Cache-Tags (Fastly / Cloudflare):
    • Origin attaches header: Surrogate-Key: product-123 e-commerce-catalog.
    • When product 123 changes, backend sends API request: PURGE /tags/product-123.
    • CDN instantly invalidates all associated cached URLs across all global PoPs in <150 ms.

βš–οΈ Trade-Offs & Decision Matrix

Dimension Dynamic Content (e.g. Live Cart) Static Content (e.g. Images, CSS)
Cacheability Non-cacheable (Cache-Control: private, no-store) Highly cacheable (public, max-age=31536000, immutable)
CDN Value Dynamic Content Acceleration (TLS termination, connection reuse, HTTP/3) Direct edge offload, near-zero origin bandwidth
Purge Requirement None (never cached) Content hash in filename or tag-based purge
Failure Risk Stale private data leaked to other users if misconfigured! Outdated UI assets if cache headers lack versioning

🚫 Common Mistakes & Production Pitfalls

  1. Accidentally Caching User PII (Authorization or Set-Cookie):

    • Pitfall: Edge CDN caches an API response that contains Set-Cookie: session_id=XYZ. Subsequent users receive the cached session cookie and log into someone else's account!
    • Fix: Never cache responses containing Set-Cookie or Authorization headers unless explicitly stripped at the edge. Always set Cache-Control: private, no-store for authenticated routes.
  2. Omitting Vary: Accept-Encoding:

    • Pitfall: First request is from a client supporting Brotli (br). CDN caches Brotli compressed bytes. Second client only supports gzip. CDN serves Brotli bytes to gzip client, causing binary garbled text crashes.
    • Fix: Origin must emit Vary: Accept-Encoding.
  3. Purging Entire Cache (/*) During Incidents:

    • Pitfall: A junior engineer flushes the entire global CDN cache (PURGE /*) to clear a bug.
    • Result: 100,000,000 global requests immediately miss the cache and hammer the origin server simultaneously, causing a total site blackout.
    • Fix: Always use targeted URL or tag-based purges. Use "Soft Purge" (marks assets as stale and serves them while revalidating in background).

πŸ’» Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. An Origin Server with simulated 120ms Pacific fiber latency and ETag calculation.
  2. An Edge PoP cache implementing RFC 7234 cache rules and stale-while-revalidate.
  3. An automated test showing latency drop from 120 ms down to 0.00 ms and 100% origin offload.
  4. Real-time Surrogate-Key cache purge invalidation.

Run it locally in your terminal:

python demo.py

🌍 Where You'll See It in Production


🎯 Top Interview Questions & Deep Answers

Q1: "How does a CDN accelerate dynamic, non-cacheable API requests (e.g. POST /checkout)?"

Interviewer Goal: Testing whether the candidate understands CDNs beyond naive static image caching.

Model Answer: "Even when a response is completely non-cacheable, a CDN accelerates dynamic requests via Dynamic Content Acceleration (DCA):

  1. Edge TLS & Connection Termination: Establishing TCP and TLS 1.3 handshakes with an origin across the ocean takes 300–500 ms. The client instead terminates TCP and TLS with the local Edge PoP in 5 ms.
  2. Persistent Origin Connection Pools: The CDN maintains pre-warmed, keep-alive TCP/TLS connection pools back to the origin server. When the dynamic request arrives, it travels over an already established connection with zero handshake overhead.
  3. Optimized Route Selection: CDNs monitor real-time internet congestion and route traffic across their private fiber backbones rather than the public, unoptimized BGP Internet.
  4. Modern Transport Protocols: Edge PoPs communicate with clients over HTTP/3 (QUIC/UDP), mitigating packet loss on mobile networks."

Q2: "What is the difference between max-age and s-maxage, and how do you use them in production?"

Interviewer Goal: Checking production mastery of RFC 7234 HTTP caching specifications.

Model Answer: "max-age applies to all caches, including private browser caches and shared proxy/CDN caches. s-maxage (shared max-age) specifically overrides max-age for shared public caches like CDNs, and is completely ignored by individual private browser caches.

In production, this allows a powerful pattern: Cache-Control: public, max-age=60, s-maxage=86400


Q3: "What is Origin Shielding, and why is it essential for massive global websites?"

Interviewer Goal: Testing distributed caching architecture at extreme global scale.

Model Answer: "In a large CDN with 300+ Edge PoPs worldwide, when a high-traffic object expires, all 300 PoPs experience a cache miss around the same time. Each PoP would issue an independent origin pull, resulting in a Thundering Herd on the origin database.

Origin Shielding (or Tiered Caching) introduces a designated intermediate caching layer located close to the origin server:

  1. Edge PoPs around the world do not query the origin directly; they query the Origin Shield.
  2. If Tokyo and London PoPs both miss, Tokyo pulls from the Shield. The Shield pulls once from Origin and caches it.
  3. When London queries the Shield 100 ms later, the Shield serves the asset directly without touching the origin. This collapses 300 potential origin pulls into exactly 1, reducing origin load by up to 99%."

Q4: "How does stale-while-revalidate work, and why does it improve perceived user performance?"

Interviewer Goal: Checking modern web performance optimization techniques.

Model Answer: "Under standard caching, when an asset's TTL expires, the next user who requests it suffers a cache miss penaltyβ€”they must wait for the full origin round-trip (e.g. 150–300 ms).

stale-while-revalidate=N defines a grace period after expiration:


Q5: "Explain the difference between Strong and Weak ETags in HTTP caching."

Interviewer Goal: Testing low-level protocol understanding and conditional HTTP requests.

Model Answer: "An ETag (Entity Tag) is an HTTP validator representing a specific version of a resource:

  1. Strong ETag (ETag: "686897696a7c876b7e"):
    • Guarantees byte-for-byte identity. If even 1 bit changes (e.g., whitespace or metadata), the ETag changes. Mandatory for byte-range requests (like video resumption).
  2. Weak ETag (ETag: W/"686897696a7c876b7e"):
    • Indicates semantic equivalence rather than byte-for-byte identity. For example, an HTML page rendered with identical content but compressed with Gzip vs. Brotli, or with reordered JSON keys, can share the same Weak ETag. When revalidating with If-None-Match, if the ETag matches, the server returns 304 Not Modified with an empty body, saving bandwidth."

βœ… Check Yourself: Flash Quiz

1. Why does setting Cache-Control: public, max-age=31536000 without filename hashing create deployment nightmares?
Because user browsers will store the file locally for a full year (31,536,000 seconds) and will never check the network or CDN for updates. If you push a bug fix, existing users will remain broken until they manually clear their browser cache. Always use content hashes in filenames (e.g., bundle.a1b2c.js) when using long max-age.
2. How does Anycast BGP direct a client to the nearest CDN Edge PoP?
All edge PoPs announce the same identical IP address to upstream Tier-1 internet carriers. Border Gateway Protocol (BGP) routing automatically directs packets along the path with the fewest network hops, leading to the topologically nearest edge router.
3. What does HTTP status code 304 Not Modified mean, and why does it save bandwidth?
It indicates that the client or CDN's cached copy is still fresh after checking the ETag or If-Modified-Since header. The server sends back only headers and zero response body bytes, saving transmission time and egress bandwidth.