Skip to content
System Design Lab
Design to Scale

Content Delivery Network (CDN)

Cache content at edge locations close to users, so most requests never cross an ocean or touch your origin — and design pages so they can actually be cached.

Beginner 4 min read· Updated 2026-11-18

What you will learn

  • Explain why distance dominates latency for far-away users.
  • Describe how edges cache, expire and purge content.
  • Design cacheable responses with Cache-Control and stable URLs.
  • Recognise when personalisation defeats the CDN.

Before you start

Overview

A publisher in Paris does not ship every book from Paris: it keeps stock in warehouses near its readers. Most orders leave from the nearest warehouse; only unusual titles come all the way from Paris.

Light in fibre needs ~70 ms to cross the Atlantic and back, more to Asia, and every request may need several round trips. A CDN places caching servers (edges) in hundreds of locations. A request goes to the nearest edge; on a hit it is answered locally, on a miss the edge fetches from your origin and keeps a copy for as long as the response's caching headers allow.

Run it

Users in Europe, the US and Asia browse product pages served from Frankfurt. Compare p50/p95 and Origin busy between A and B, purge an edge, then see C, where every page is personalised.

Loading simulation…

Making responses cacheable

Header / techniqueEffect
Cache-Control: public, s-maxage=600Edges may keep it 10 minutes (browsers follow max-age).
stale-while-revalidate=60Serve the old copy while refreshing in the background.
Versioned asset URLs (app.3f9c.js)Cache forever (immutable); a new deploy gets a new URL.
Vary kept minimalEvery Vary value multiplies the copies the edge must store.
Cache-Control: private, no-storePersonal data never cached at the edge.

Example

Shared page cached at the edge; personal data fetched separately
@GetMapping("/products/{id}")
ResponseEntity<ProductPage> product(@PathVariable long id) {
    return ResponseEntity.ok()
        .cacheControl(CacheControl.maxAge(Duration.ofMinutes(1)).sMaxAge(Duration.ofMinutes(10)).cachePublic()
            .staleWhileRevalidate(Duration.ofMinutes(1)))
        .body(pages.render(id));                     // same for everyone: no prices per user, no cart
}

@GetMapping("/me/cart-summary")                     // small, personal, never cached by the CDN
ResponseEntity<CartSummary> cart(Principal user) {
    return ResponseEntity.ok().cacheControl(CacheControl.noStore()).body(carts.summary(user));
}

Trade-offs

BenefitsCosts
LatencyHits are answered a few milliseconds from the user.Misses add a hop; uncacheable content gains nothing.
Origin loadThe origin serves a small fraction of traffic and survives spikes.A global purge or expiry can send a wave of misses to the origin.
FreshnessTTLs and purges control how long content lives.Content can be stale up to the TTL; purges take time to propagate.

Production considerations

  • Track hit ratio per path and region; a drop usually means a header or URL change.
  • Use origin shielding (a mid-tier cache) so a miss in many edges is one origin request.
  • Split pages into a cacheable shell and small personal API calls.
  • Rate-limit and protect the origin: the CDN also absorbs attacks, but only for cached paths.

Further reading

Related topics