Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

CDN Cache
HLD

CDN Cache

Caching at the edge — hundreds of locations close to users, serving content before it ever reaches your origin.

The Layer That Changed the Internet

 without CDN:
   Sydney user ────── 200ms+ ──────► Virginia origin
   every asset, every user, every time

 with CDN:
   Sydney user ── 10ms ──► [Sydney edge] ─(miss only)─► origin
   edge holds copies; origin sees a fraction of traffic

 CDN = geographically distributed reverse-proxy cache.
 your own lesson covers mechanics; this is its caching role.

What Belongs at the Edge

ContentEdge behavior
Static assets (JS/CSS/images/fonts)Cache months, immutable
Video segmentsCache aggressively; origin sees ~1 fetch per segment per region
Product/category pagesShort TTL (30–300s)
API GET responsesTTL seconds where staleness allows
Personalized/dynamicDon’t cache — or fragment carefully

The Origin-Shielding Multiplier

 naive edge fill: 500 edges × miss = 500 origin fetches per object

 with an origin shield (middle-tier cache):
   edges ──► [1-2 shields] ──► origin
   first regional miss fills shield; other edges hit the shield
 
 origin load drops from O(edges) to O(objects) —
 this single setting choice often cuts origin traffic 10–50x

Control From Your Side

 CDNs mostly obey YOUR headers:

 Cache-Control: public, max-age=3600     edge caches 1h
 Cache-Control: private                  edge skips (per-user)
 Surrogate-Control: max-age=86400        edge-only override
                                         (browser gets shorter rule)

 plus platform tools:
 - PURGE by URL/tag      instant removal on deploy/scandal
 - cache keys            vary by host/path/query/cookie/header/geo
 - stale-while-revalidate at edge: zero-lag deploys

Cache-Key Discipline

 what's IN the key decides sharing vs fragmentation:

 /api/products?color=red&sort=price&_=1690123456
    ↑ all query params in key → cache-buster param = total misses

 trim keys deliberately:
   ignore tracking params (_, fbclid)
   normalize case/order
   fragment ONLY on real variance (currency, language)
 
 default keys are almost always wrong for APIs; configure them.

The Numbers That Sell It

 typical production outcomes:
 - static assets:   90–99% edge hit rate → origin nearly silent
 - latency:         p50 drops from ~150ms to ~15ms for far users
 - egress cost:     CDN bandwidth usually cheaper than origin cloud egress
 - outage survival: stale-if-error keeps site up during origin incidents

 caching at the edge isn't an optimization anymore;
 for global products it's table stakes architecture.

Interview Framing

“Serve images to global users” = CDN answer with structure: edge placement rationale (latency + offload), headers/surrogate control, cache-key normalization, purge strategy for bad uploads, shield mention as bonus. Connecting to browser-cache lessons (immutable versioned assets) shows layered-cache fluency.

My Private Notes

Notes are auto-saved locally to this device.