Caching & Scale

CDN & Edge

Push public content to the edge for latency and origin offload, and know what must not live there.

Beginner, about 55 minutes to read, 4 open questions to practise.

1Understand

The idea

A CDN (content delivery network) puts public, cacheable bytes close to users so latency drops and your origin does not absorb every image, script, and marketing page hit. In interviews, “use CloudFront” is not the answer. You say what is safe to cache, how Cache-Control and URLs form the cache key, how purges or TTLs handle updates, and what must never sit on a shared edge URL.

CDN caching is a shared cache: one URL, many strangers. Personalized HTML, authenticated JSON, or responses that vary on Authorization or cookies belong on the origin or an app cache with correct keys, not on a default edge path. Origin shield and tiered caches exist because miss storms still hurt; the edge is an accelerator, not your durable database.

Quantify cache hit ratio intent on static (“>95% on hashed assets”) and say how HTML updates propagate, purge, short TTL, or versioned paths.

Intuition

Cacheability quadrants. Ask: is the response identical for all users (public static), identical per URL but not secret (public JSON by id), per-user (authenticated), or uncacheable (always fresh, Set-Cookie on every hit)? Only the first two belong on a shared CDN path with correct headers.

Cache keys are URL + vary headers + query strings unless configured. app.js?v=abc versioning busts cache deliberately. Cache-Control: public, max-age=31536000, immutable for hashed assets; shorter s-maxage for HTML that might change.

Miss path: client → edge POP → (optional origin shield / regional cache) → your origin. Thousands of POP misses must not become thousands of origin hits, shield collapses miss fan-in.

Purge vs TTL. Deploy new bundle.hash.js needs no purge; fix a wrong legal paragraph on a cached HTML page needs purge or short TTL.

TLS at edge: terminate at CDN; origin can restrict to CDN IP ranges. Cookies: Cache-Control: private or bypass cache for session cookies.

Edge compute (Workers, Lambda@Edge) can auth at edge, easy to get wrong if cache key ignores Authorization.

Cookies and `Vary`: session cookies on marketing pages accidentally make entire site uncacheable, split static hostnames. HTTP/2/3 multiplexing reduces connection count but does not replace cache rules.

Interview checklist: classify each URL (public static / public API / private), state cache key ingredients, origin + shield on miss path, and purge vs immutable hash deploy for updates.

What you will be able to do

  • Place a CDN for static/public reads
  • Explain purge vs TTL for updates
  • Keep personalized or secret data off shared edge caches

When this comes up

  • Static assets
  • Cache-Control
  • Origin shield
  • Edge compute

2See it

Architecture sketch

CDN in front of origin

Edge serves public cacheable bytes; origin stays for misses and private APIs.

100%
ClientGatewayServiceStore

Walkthrough

HIT vs MISS

Edge cache for public bytes

1 / 4

100%

User requests /assets/app.js with a content-hashed URL.

Read the walkthrough as text

Case, global e-commerce site.

Traffic: product images, app.[hash].js, public product detail HTML; logged-in cart and “recommended for you” API.

  1. CDN yes: images.cdn.example/..., static JS/CSS with content hashes, optional public PDP HTML if not personalized.
  2. CDN no: GET /cart, GET /recommendations, origin or Redis with user id in key; never one shared CDN URL.
  3. Headers: long TTL + immutable on hashed assets; Cache-Control: public, max-age=300 on public PDP if acceptable staleness; private on anything with session.
  4. Origin: S3 or origin API; origin shield in one region to absorb miss spikes from worldwide POPs.
  5. Update: deploy new JS filename; purge CDN paths for corrected legal footer on marketing page.
  6. Failure: origin down, CDN may serve stale if configured; say what is stale vs 503 for uncached API.

Edge auth pitfall: caching GET /api/me at edge without Vary: Authorization, user A’s profile served to user B.

  1. Measure: origin offload % and edge hit ratio, if misses dominate, shield or fix TTL/key.
  2. Dynamic vs static: ISR/SSG at build for semi-static PDP if same HTML for all viewers.

Phrase: “Hashed immutable assets; cart never on shared CDN URL.”

Napkin math

CDN napkin

cacheable = public assets, Cache-Control max-age

hit ratio target ≥ 95% for static

TTL = 1 day for hashed filenames, minutes for HTML

MISS → origin (or origin shield)

purge / versioned URLs on deploy

Edge for bytes near users. Private APIs stay at origin.

3Answer it

How to answer in an interview

When the prompt mentions global users, static assets, or latency:

  1. Classify resources, public static, public cacheable API, private personalized.
  2. CDN placement, what URLs; what headers (TTL, immutable, private).
  3. Cache key, query strings, cookies, auth headers.
  4. Origin & shield, where durable bytes live; miss collapse.
  5. Update path, hashed assets vs purge.
  6. What stays on origin, auth, writes, personalized reads.

Explicitly reject caching logged-in JSON on a public edge URL.

Say: “Cache key is URL + vary headers.” · “Origin shield collapses POP misses.” · “Purge for legal HTML fixes.”

Answer checklist

1. What is public and cacheable vs private? 2. Cache keys / URLs and TTLs / purge. 3. Origin and origin-shield under miss. 4. TLS and cookies at the edge. 5. What stays on origin (auth, personalized HTML/JSON).

Tradeoffs & numbers

  • Long CDN TTL: great hit rate; slow correction without purge.
  • Purge APIs: fast fixes; rate limits and propagation delay across POPs.
  • Origin shield: fewer origin hits; extra hop and cost.
  • Stale-while-revalidate: smoother UX; brief wrong content risk.
  • CDN as only storage: no durable audit or admin edits at source, keep origin/S3 truth.
  • Edge auth: lower origin load; complex cache key and security review.
  • When NOT CDN: websocket API, write-heavy RPC, per-user JSON, origin or regional app cache.
  • Long HTML TTL: legal/pricing risk, pair with purge runbook.
  • CDN as only copy: always durable origin bucket behind edge.

Hit ratio target: state “>95% on hashed assets” and origin offload metric. Purge drill: legal HTML fix path in under 5 minutes. - When NOT CDN: authenticated JSON, WebSockets, per-user HTML, origin or app cache with user-scoped keys.

Kinds of questions (4)

Same pattern, different interview surfaces. Recognise which variant you are in before you answer.

  • Static asset CDN

    Example. Global traffic for `app.[hash].js` and product images. Define cache key (path + query), TTL/purge story, and what personalized or authenticated responses must never share an edge URL.

    How. CDN with long TTL and immutable hashed filenames; origin is S3 or build artifact bucket. Immutable hashed asset URLs; short TTL or purge for HTML; origin shield if miss rate spikes.

  • Cacheable public pages

    Example. Marketing landing pages and public product HTML. Define cache key (path + query), TTL/purge story, and what personalized or authenticated responses must never share an edge URL.

    How. Public Cache-Control + short/medium TTL; purge on legal or pricing corrections. Immutable hashed asset URLs; short TTL or purge for HTML; origin shield if miss rate spikes.

  • Origin shield / tiered cache

    Example. Worldwide POP misses hammering a small origin cluster. Define cache key (path + query), TTL/purge story, and what personalized or authenticated responses must never share an edge URL.

    How. Regional shield cache collapses miss fan-in before origin; say miss path. Immutable hashed asset URLs; short TTL or purge for HTML; origin shield if miss rate spikes.

  • Edge auth pitfalls

    Example. Caching `/api/profile` at edge with session cookie. Define cache key (path + query), TTL/purge story, and what personalized or authenticated responses must never share an edge URL.

    How. Private/no-store or vary on Authorization; personalized JSON stays on origin/app cache with user key. Immutable hashed asset URLs; short TTL or purge for HTML; origin shield if miss rate spikes.

Common mistakes

Failure modes the AI coach watches for when you practise open questions.

What belongs on the edge

Avoid. Personalized feed JSON on one shared CDN URL.

Prefer. Hashed static assets on CDN; private feed from API/Redis.

Weak sketch

Bad

100%

Stronger sketch

Good

100%
  • Personalized on CDN

    Why it happens. Shared edge caches serve one response to many users, wrong key leaks private data.

    How it fails. CDN caches `GET /recommendations` on one URL; every user sees the first visitor’s list.

    Fix. Mark private/no-store; cache only public URLs; put user id in app-layer keys if caching personalized data.

  • No purge story

    Why it happens. Long TTL without purge leaves wrong legal text, prices, or broken assets live at the edge.

    How it fails. Fix typo in cached HTML; users worldwide still see old page for 24h with no purge plan.

    Fix. Purge API or versioned URLs; use short TTL where content must change unpredictably.

  • CDN as only origin

    Why it happens. CDNs are caches, not durable systems of record, eviction and misconfig lose data.

    How it fails. Only copy of uploaded images lives at CDN; origin bucket deleted to “save cost.”.

    Fix. S3/object store or DB as source of truth; CDN pulls from origin; rebuildable from canonical store.

4Practise

Concept check

A short quiz on this lesson before open questions. Optional, and it does not gate practice.

Open questions

Ready to practise?

Write an answer, get AI coaching, then revise. You are not building a real system.

Practise with an open question

All systems lessons