CDN

Updated Nov 16, 2026 · 5 min read
On this page

    Your servers are in Virginia. A user in Tokyo opens the course site and the page needs 40 files: scripts, styles, images, fonts. Each round trip across the Pacific costs about 150 ms, and the browser can only fetch a few at a time. The page takes four seconds to appear, while users in New York see it in under one. Your servers are not slow; the distance is.

    The fix is to keep copies of those files on servers close to every user. That network of nearby copies is a content delivery network (CDN).

    How it works

    CDN edge versus origin: users in Tokyo and London reach a nearby edge in about 10 ms; a hit is served there, and only a miss crosses the ocean to the origin, about 150 ms.
    CDN edge versus origin: users in Tokyo and London reach a nearby edge in about 10 ms; a hit is served there, and only a miss crosses the ocean to the origin, about 150 ms.
    1. Your domain (say cdn.systemcraft.app) points at the CDN. DNS sends each user to a nearby edge location.
    2. The edge looks up the request in its cache. On a hit it answers immediately.
    3. On a miss it fetches the file from your origin (a blob store bucket or your web servers), returns it, and keeps a copy for next time.
    4. How long it keeps the copy is controlled by headers you send: Cache-Control: max-age=… and friends.

    With a 95% hit rate, your origin sees one request in twenty. That is the second benefit of a CDN after latency: it absorbs load. A video segment watched by a million people is fetched from the origin a few hundred times, once per edge, not a million times.

    What a CDN can cache

    • Static assets: JavaScript, CSS, fonts, icons. Long cache times.
    • Media: images, thumbnails, video segments (HLS/DASH chunks), downloads.
    • Public API responses that are the same for everyone, for a few seconds (a trending list, a public product page).
    • Not: anything personal or behind auth, unless the cache key includes the user and you know exactly what you are doing.

    Modern CDNs also run code at the edge (redirects, A/B bucketing, auth checks on signed URLs), but in an interview the cache is the main point.

    Changing a cached file

    Two ways to change a cached file: give it a new versioned URL, which is an instant miss with nothing to purge, or call the purge API for a fixed URL, which takes time to reach every edge.
    Two ways to change a cached file: give it a new versioned URL, which is an instant miss with nothing to purge, or call the purge API for a fixed URL, which takes time to reach every edge.
    • Versioned URLs (best for assets): the build puts a content hash in the file name, app.3f9a.js becomes app.8c21.js. The new HTML points to the new name, every edge misses on it once, and the old file can stay cached for a year. Mark such files max-age=31536000, immutable.
    • Purge: for URLs that must stay fixed (/logo.png, a public page), call the CDN's purge API. It takes seconds to minutes to reach every edge.
    • Short TTL: for content that changes often but can be a little stale, set max-age=60 and let it expire.

    The HTML page itself usually gets a short or zero cache time, so a new deploy is picked up quickly.

    Trade-offs and costs

    • Staleness: a cached file can be out of date until it expires or is purged. Versioned names avoid this for assets.
    • Cost: CDNs charge per GB delivered and per request. For video, CDN egress is often the biggest line in the bill; it is still far cheaper than serving from your own servers.
    • Cache misses on the long tail: content few people watch is rarely cached at every edge, so those users still pay the origin round trip. An origin shield (one mid-tier cache in front of the origin) reduces repeated misses.
    • Privacy risk: caching a personalized response with a shared key can show one user's data to another. Use Cache-Control: private or no-store for personal responses.
    • Less control: debugging means reading CDN logs and headers (Age, X-Cache: HIT).

    When to reach for it

    • Users are spread across regions and latency matters.
    • The same bytes are read by many users: assets, images, video, software downloads.
    • You need to protect the origin from traffic spikes (a launch, a viral post).
    • You serve large files and want to keep that bandwidth off your application servers.

    Skip it (or keep it out of the path) for personalized, write-heavy or real-time traffic: chat messages, checkout calls, WebSocket connections.

    Example

    A video course platform. Assumptions: 200,000 daily viewers worldwide, each watching 30 minutes at 2 Mbps.

    • Traffic: 200,000 × 30 min × 2 Mbps ≈ 90 TB per day. Serving that from one region would need huge bandwidth and still be slow far away.
    • Design: videos are transcoded into 4-second segments and stored in a blob store (the origin). The player fetches segments from cdn.systemcraft.app/v/{video_id}/{quality}/{n}.ts.
    • Caching: segments never change, so they get max-age=31536000, immutable. The playlist file gets max-age=10 so a fixed video can be swapped quickly.
    • Access control: paid videos use signed URLs that expire after a few hours. The edge checks the signature, so the bucket stays private.
    • Result: with about 95% hit rate, the origin serves around 4–5 TB a day instead of 90, and segments start in tens of milliseconds for viewers in every region.

    Common mistakes

    • Putting dynamic, personalized pages behind a shared cache and leaking one user's data to another.
    • Relying on purges for every deploy instead of versioned file names.
    • Long cache times on HTML, so users get old pages that reference files that no longer exist.
    • Forgetting the CDN in a video or image design. It is usually the single biggest win for media-heavy systems.
    • Not mentioning access control for private media: signed URLs or signed cookies.
    • Assuming 100% hit rate. Plan origin capacity for misses, cold edges and purges.

    Say this in the interview

    "Images and video segments are read by many users and never change, so they go to blob storage behind a CDN. File names include a content hash, so they can be cached for a year and we never purge on deploy. Private videos use signed URLs that expire after a few hours. With a 95% hit rate the origin sees about one request in twenty, and viewers get the first segment from an edge near them."

    Used in