HTTP semantics, caching and delivery · mid
CDNs, cache keys and edge computing
A CDN serves cached responses from servers close to users and shields your origin, and you control it through cache keys (URL, selected headers), lifetimes (s-maxage, stale-while-revalidate), purges or tag invalidation, and by never letting personalised responses into the shared cache.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Benefits: lower TTFB (edge near users; India has PoPs in many metros), less origin load, absorbing traffic spikes (sales, cricket matches), TLS termination, HTTP/3, image resizing at the edge.
- Cache key = what makes two requests 'the same': usually host + path + query. Strip tracking params (utm_*, gclid, fbclid) and sort params to raise the hit rate; add only the Vary headers you need.
- Lifetimes: s-maxage for the CDN, max-age for browsers; stale-while-revalidate and stale-if-error keep pages up while the origin is slow or down. Origin shield collapses many edge misses into one origin request.
- Invalidation: content-hashed URLs for assets (no purge needed), and purge by URL or by tag for HTML and API responses.
- Never cache personalised responses publicly: use private/no-store, or split the page into a cached public shell and a private client-side fetch. Watch Set-Cookie on cacheable responses.
- Edge functions (middleware/proxy at the CDN) can do redirects, A/B bucketing, geo routing and auth checks, but they have limited APIs and a database far away makes them slow.
The red pen
Common traps: mistake → fix
Mistake: "Put the CDN in front and everything gets faster"
Fix: HTML and APIs are only cached if your headers allow it; otherwise the CDN just proxies.
Mistake: "Cache the logged-in homepage at the edge"
Fix: without a correct cache key one user's data is served to others.
Mistake: "Vary: User-Agent is fine"
Fix: it creates thousands of variants and the hit rate collapses.
Easy to hard
Questions interviewers ask, easy to hard
- How does a CDN improve performance? What does it cache?
- What is a cache key and how would you raise the CDN hit rate?
- How do you invalidate CDN caches after a deploy or a price change?
- Two users saw each other's data after you added a CDN. What went wrong?
- When does running code at the edge help, and when does it hurt?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.