ayushsalampuriya.xyz Revise

System Design · Core

CDN

Edge caches that put static (and some dynamic) content near users: PoPs, Cache-Control, invalidation, and origin shield.

1. What a CDN is

A Content Delivery Network is a fleet of edge servers (points of presence) distributed worldwide. Users fetch assets from a nearby PoP instead of your origin in one region.

User (Delhi) --> Edge PoP (BOM) --hit--> cached object \ miss --> Origin (or shield) --> fill PoP

2. What to put on a CDN

Do not blindly cache personalized HTML or authenticated API responses without Vary / cache keys that include the right dimensions.

3. Request flow

  1. DNS for cdn.example.com returns an edge Anycast/VIP near the user.
  2. Edge looks up object by cache key (URL + selected headers).
  3. Hit → serve; Miss → pull from origin (or parent shield), store, serve.
  4. Respect TTL / revalidation (ETag, If-None-Match).

4. Cache-Control and related headers

Header / directiveRole
Cache-Control: max-age=...Freshness lifetime for shared/private caches
public / privateCDN may store vs browser-only
no-store / no-cacheDo not store / revalidate before use
s-maxageTTL specifically for shared caches (CDN)
ETag / Last-ModifiedConditional revalidation
VarySeparate entries by header (e.g. Accept-Encoding)
Fingerprinted asset: /app.9f3a21.js → Cache-Control: public, max-age=31536000, immutable HTML shell: /index.html → Cache-Control: no-cache (always revalidate)

Fingerprint filenames so you can cache forever and deploy new bytes under a new URL. That beats emergency purges for routine releases.

5. Invalidation and purge

Purge is not instant worldwide and can be expensive. Prefer versioned URLs; purge for mistakes and urgent takedowns.

6. Origin shield

Many edges miss at once (popular new object) Without shield: thundering herd → Origin With shield: edges → Shield PoP → Origin (one miss folded)

A designated mid-tier cache collapses duplicate origin fetches, lowers origin load, and can improve cache hit ratio. Trade-off: extra hop on some misses.

7. Dynamic and security notes

8. Interview placement

In almost every media-heavy design (YouTube, Drive, Instagram), draw CDN in front of object storage. State TTL strategy, versioning, and purge for bad assets. Mention origin shield if origin QPS is a concern.

9. Cache key design

The cache key is usually the full URL plus selected headers from Vary. Query-string noise (utm_*) can fragment the cache — strip tracking params at the edge when safe. Prefer content-hashed paths for static assets so keys naturally change on deploy.

Good key: https://cdn.example.com/static/app.9f3a21.js Bad key: /app.js?utm_source=twitter (unless normalized)

10. Measuring CDN health

Quick revision

  • CDN = edge PoPs caching content near users.
  • Best for static and segment-based media; careful with personalized data.
  • Cache-Control, ETag, Vary control freshness and variants.
  • Fingerprint assets for long TTL; short TTL or revalidate for HTML.
  • Purge for emergencies; versioning for routine deploys.
  • Origin shield collapses miss stampedes onto origin.
  • Signed URLs protect private objects at the edge.