Skip to content
Caching

Lesson 11 of 14 · 20 min

x
11/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

CDN Caching & Edge Delivery

A CDN (Content Delivery Network) caches content at edge servers distributed globally — Cloudflare, AWS CloudFront, Fastly, Azure CDN. A user in Tokyo gets the response from a Tokyo edge node instead of your server in Virginia, often cutting cross-region latency substantially on cache hits — measure your paths rather than assuming fixed milliseconds.

CDNs excel at static content: images, videos, CSS, JavaScript bundles. Configure cache behaviors by path pattern — /assets/ gets a long TTL, /api/ bypasses the cache. Cache purging invalidates edge copies when you deploy new assets. For dynamic content, CDNs support edge-side includes and stale-while-revalidate at the edge.

The key decision is what to cache at the edge vs what must reach your origin server. User-specific API responses (my profile, my orders) should not be cached at the CDN. Public, identical responses (product catalog, blog posts, static pages) are ideal CDN candidates.

Before
All traffic hits origin server
1// User in Tokyo → cross-region RTT to US-East (often ~100–200ms+; measure)2GET https://api.example.com/products3→ Origin server in Virginia4→ Full origin latency on every request
After
CDN serves cached responses at the edge
1// CloudFront distribution config2{3  "pathPattern": "/products/*",4  "cachePolicy": {5    "defaultTTL": 300,6    "maxTTL": 36007  }8}9 10// User in Tokyo → edge hit in the same region (often much lower than origin; measure)11GET https://cdn.example.com/products12→ CDN Tokyo edge (cache hit)13→ Edge latency on hit; miss still pays origin14 15// Purge on deploy16await cloudfront.createInvalidation({17  Paths: { Items: ['/products/*'] }18});

Check your understanding

  • What content fits CDN caching best?Show answer

    Answer

    Public, identical responses — static assets and cacheable pages/APIs that do not vary by user.
  • Why purge/invalidate on deploy?Show answer

    Answer

    Edge nodes may keep old assets until TTL or explicit invalidation.
  • Are edge latency numbers universal?Show answer

    Answer

    No — they depend on geography, cache hit, TLS, and origin fallback. Measure your paths.
Previous

Progress is saved in this browser.

Next Lesson