# How a CDN Works: A System Design Interview Primer "Put it behind a CDN" is easy to say. Interviewers sometimes ask you to explain what that actually does. ## The basics A content delivery network runs caching servers in many locations (points of presence, PoPs). A user's request is routed to a nearby PoP, usually by **anycast** IP routing or **DNS-based** steering that returns a close server's address. If the PoP has the object cached, it answers in a few milliseconds; otherwise it fetches from the **origin** (or from a regional mid-tier cache), stores it and returns it. ## Cache keys and freshness - The cache key is normally the URL plus a few chosen headers. Avoid letting cookies or random query strings fragment the cache. - `Cache-Control: public, max-age=31536000, immutable` for versioned assets (`app.3f9a1c.js`): cache forever, change the file name on deploy. - Short TTLs plus `stale-while-revalidate` for HTML or API responses that change. - **Purge** APIs remove specific objects instantly when you can't rename them. ## Origin protection - **Tiered caching / origin shield**: PoPs fetch from one shield location instead of all hitting the origin, so a cache miss storm becomes a single request. - **Request collapsing**: many simultaneous misses for the same object wait on one origin fetch. ## Beyond static files - Video segments (HLS/DASH) are the biggest CDN workload. - Edge compute (Workers, Lambda@Edge) runs small functions at the PoP for redirects, auth checks or A/B routing. - TLS termination and DDoS absorption at the edge. ## Trade-offs to mention Cost per GB, cache hit ratio as the key metric, and the difficulty of caching personalized content (cache the shell, fetch personal data separately). Disclosure: I work on Linkjob AI (https://www.linkjob.ai/), an AI interview assistant.