SD System Design In Depth
0%
lesson system-design caching cdn edge latency web-performance

Edge caching and page load times

5 min read Module 4.1: Caching Strategies & In-Memory State Updated Nov 20, 2026
Cheat Sheet
What Is A CDN? How Does It Work?
ByteByteGo What Is A CDN? How Does It Work? Watch on YouTube ↗

Clear visual walkthrough of how global Edge PoPs, cache hits/misses, and geographic proximity cut round-trip latency for users far from your origin.

0:00 / 4:04

Speed of Light & Why 1 RTT Turns Into 5

System design benchmarks often report origin server latency in isolation: “our API responds in 40 ms.” Yet a user in Mumbai or Sydney opening that same page hosted in us-east-1 (Virginia) stares at a blank screen for over two seconds. The culprit is physics multiplied by protocol waterfalls.

Light travels through glass fiber at roughly 200 km/ms (about 1.5× slower than in vacuum), and real internet cables do not follow straight great-circle paths (add ~1.25×–1.35× routing stretch plus router switching). A 12,800 km fiber path from Mumbai to Virginia therefore has a physical round-trip time (RTT) of roughly 170–220 ms. Before a browser paints a single pixel over HTTP/2, it pays that RTT sequentially:

  1. TCP Handshake (1 RTT): SYN → SYN-ACK → ACK across the ocean (~200 ms).
  2. TLS 1.3 Handshake (1 RTT): Cryptographic negotiation (~200 ms; HTTP/3 QUIC merges TCP+TLS into 1 RTT).
  3. HTML Request & TTFB (1 RTT + Origin SSR): Request travels to origin, origin queries DB and renders HTML (e.g., 140 ms), response returns (~340 ms). Total TTFB is already ~760 ms.
  4. Critical Subresources (1+ RTT): Only after parsing the HTML does the browser discover render-blocking CSS and the hero image, paying another full ocean RTT (~200 ms) plus transfer time before First Contentful Paint (FCP) and Largest Contentful Paint (LCP).

Edge PoP Architecture: HIT, MISS, Origin Shield & SWR

flowchart LR User["User in Mumbai / Sydney"] -->|"12ms Local RTT (TCP+TLS Terminated)"| PoP["Nearest Anycast Edge PoP (BOM / SYD)"] PoP -->|"Warm HIT or STALE (SWR): ~18ms TTFB"| User PoP -->|"Cold MISS (Warm Keep-Alive Pool)"| Shield["Regional Origin Shield"] Shield -->|"Shield MISS (Single Coalesced Fetch)"| Origin["Origin Server (us-east-1)"] PoP -.->|"Async Background Revalidate (SWR)"| Origin

What Edge Caching Actually Moves Closer to the User

1. Local TCP & TLS Termination (Wins Even on a Cache MISS)

Anycast BGP routes the user to a Point of Presence (PoP) 10–20 ms away. Both TCP and TLS 1.3 complete locally in ~25 ms instead of ~400 ms. Even when the requested page is a cold MISS, the PoP forwards the HTTP request over a pre-warmed, persistent keep-alive connection pool to the origin, eliminating cross-ocean handshakes.

2. Static Assets vs. Full-Page HTML Caching

Putting only JS, CSS, and images on a CDN is the most common half-measure. While subresources load quickly, the HTML document still crosses the ocean and runs origin rendering. Because HTML is the root of the browser's critical rendering path, caching public HTML at the edge using s-maxage is what collapses TTFB from 800 ms to under 40 ms:

Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=300, stale-if-error=86400
  • max-age=0 tells the user's browser to revalidate with the nearest PoP (so purges take effect immediately).
  • s-maxage=60 tells shared edge caches (CDNs) to serve the HTML fresh for 60 seconds.
  • stale-while-revalidate=300 lets the PoP serve a slightly expired response in ~18 ms while refreshing from the origin asynchronously in the background—hiding ocean latency from the user completely.
  • stale-if-error=86400 keeps serving the last good cached HTML if the origin suffers an outage.

3. Tiered Caching & Origin Shielding

A global CDN may run 300+ PoPs. If every PoP independently fetches from your origin on a cold start or after a cache purge, a single popular page triggers 300 origin renders. Tiered Caching (Origin Shield) places a small set of regional mid-tier caches in front of the origin. The first PoP miss populates the shield; the other 299 PoPs hit the shield instead of waking your origin database.

Worked Example: Mumbai User Loading a Virginia Origin

Consider a user in Mumbai on 100 Mbps fiber loading a product page hosted in us-east-1 (12,800 km away, ~205 ms WAN RTT, 140 ms origin SSR render, 240 KB hero image):

Architecture HTML TTFB FCP LCP (Hero Image) What the User Feels
No CDN (Direct to Origin) ~770 ms ~1,030 ms ~1,120–1,450 ms+ Noticeable blank pause; degrades past 2.5s (“Poor” CWV) on 4G/3G
Static Assets Only at Edge ~410 ms ~460 ms ~510 ms Faster hero image, but initial blank wait still tied to Virginia
Full Edge HTML (s-maxage + SWR) ~55 ms ~115 ms ~165 ms Instantaneous paint; feels like the server is in the next room

Try the interactive Edge Caching & Page Load Time Sandbox above to race these exact paths side by side from Mumbai, Sydney, Tokyo, London, and São Paulo, and trigger cache purges or stale-while-revalidate refreshes in real time.

What You Can (and Cannot) Cache at the Edge

  • Ideal for Edge HTML: Marketing pages, documentation, news articles, e-commerce product/catalog pages, public profiles, and read-heavy JSON APIs.
  • Split Shell + Dynamic Islands: When a product page is 95% public but shows a personalized “Cart (2)” badge, cache the public HTML shell at the edge and hydrate the small user-specific fragment via an authenticated API call or Edge Worker (Edge Side Includes).
  • Never Cache at Shared Edge: Account settings, checkout/payment state, private messages, or any response carrying user-specific PII—always emit Cache-Control: private, no-store.

Production Failure Modes

  • Cache Key Fragmentation: Emitting Vary: User-Agent or including marketing query parameters (utm_source, fbclid) in the cache key creates thousands of near-identical variants, collapsing hit ratio from 95% to under 10%.
  • Cookie-Induced Cache Bypasses: Many CDNs automatically bypass the cache whenever a request carries a Cookie header. Strip analytics cookies at the edge before cache lookup for public routes.
  • Global Purge Storms: Issuing a wildcard purge (/*) across 300 PoPs simultaneously creates a thundering herd against your origin. Use surrogate keys (cache tags), Origin Shielding, and request coalescing so only one fetch per key reaches the origin.
Check yourself Question 1 of 5
Concept Check Why Edge PoPs Still Help on a Cache MISS recall
Section: What Edge Caching Actually Moves Closer to the User

A user in Sydney requests an uncached HTML page (cold cache MISS) from an origin in Virginia behind a global Anycast CDN. Why is TTFB still significantly lower than connecting directly to Virginia?

Edge Caching & Page Load Time Sandbox
Edge PoP: WARM (HIT)

Pick a user far from your origin and click Load Page (A/B Race) to feel the real-time page load difference. Then try Purge Edge Cache (Cold MISS) or Expire TTL (Trigger SWR).

1. WITHOUT EDGE (DIRECT TO ORIGIN) LCP 919 ms 12,858 km fiber · 177 ms RTT × 4 sequential trips User: Mumbai Fiber Origin Server Virginia (us-east-1) 2. WITH EDGE CACHING (HIT) LCP 139 ms 18 ms Origin untouched (0 ms WAN) User: Mumbai Local Anycast BOM Edge PoP x-cache: HIT Origin Server Idle (Offloaded) Served from local RAM (~3 ms)
Origin Direct · 919 ms LCP 919 ms
Waiting for origin across ocean…
ACME STORE CatalogCart (2)
Hero Image
Mechanical Keyboard Pro
In stock · Ships today
Add to Cart · $189
Edge HIT · 139 ms LCP 139 ms
Connecting to nearest PoP…
ACME STORE CatalogCart (2)
Hero Image
Mechanical Keyboard Pro
In stock · Ships today
Add to Cart · $189
HTML TTFB
64 ms vs 687 ms
Edge: Good Origin: Good
First Contentful Paint
120 ms vs 900 ms
Edge: Good Origin: Good
Largest Contentful Paint
139 ms vs 919 ms
Edge: Good Origin: Good
User-Perceived Speedup
6.6× faster -780 ms LCP
Origin Offload: 100% HIT
NETWORK WATERFALL COMPARISON
Direct Origin (Virginia (us-east-1)) 919 ms total
DNS Lookup BYPASS
16 ms
TCP Handshake ORIGIN
177 ms
TLS 1.3 ORIGIN
177 ms
HTML Document ORIGIN
319 ms
Critical CSS ORIGIN
181 ms
Hero Image ORIGIN
196 ms
App Bundle JS ORIGIN
188 ms
Edge Path (BOM PoP) 139 ms total
Anycast DNS POP
7 ms
TCP (Edge PoP) POP
18 ms
TLS 1.3 (PoP) POP
18 ms
HTML Document HIT
23 ms
Critical CSS HIT
24 ms
Hero Image HIT
39 ms
App Bundle JS HIT
31 ms
From Mumbai, IN to US-East (Virginia) (12,858 km), direct origin requests pay ~177 ms per round trip across TCP, TLS, HTML, and subresources (LCP 919 ms). Serving from the BOM Edge PoP collapses LCP to 139 ms (6.6× faster).

Key Takeaways

Speed of light in fiber (~200 km/ms) multiplies across sequential TCP, TLS, HTML, and subresource round trips, turning a 40ms origin into a multi-second page load for distant users.
Terminating TCP/TLS at the nearest Anycast Edge PoP cuts latency even on a cache MISS via warm Edge-to-Origin keep-alive connections.
Caching only static assets still leaves HTML TTFB bottlenecked by ocean RTT; combining s-maxage with stale-while-revalidate collapses both TTFB and LCP.
Use Origin Shielding (tiered caching), cache tags, and normalized cache keys to prevent cold-miss storms and avoid leaking personalized responses.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue