26. CDNs
1
The picture to keep in your head2
26.1 Why use a CDN?3
26.2 What are edge locations (PoPs)?4
26.3 Pull vs push CDN: what's the difference?5
26.4 How do cache keys and headers work?6
26.5 How does purging work?7
26.6 What is an origin shield?8
26.7 Can the CDN handle dynamic content?9
26.8 How do CDNs deliver video (HLS, DASH)?10
26.9 What happens when the CDN fails?11
Memory card and where the analogy breaks12
Practice: five levels13
Interview questions14
Solutions15
Sources16
Code lab26. CDNs
26.7 Can the CDN handle dynamic content?
3 min read+10 XP
In the warehouse: Modern warehouses don't just store cartons. They assemble gift boxes, add a name tag, and decide which nearby shop gets which van. Some work that used to happen at the factory now happens close to the customer.
In the system: Dynamic and personalised responses can't simply be cached, but a CDN still helps:
- Faster connections to the origin. The edge keeps warm, optimised connections to your origin, so even uncached requests skip slow handshakes from the user's network.
- Micro-caching. Cache "dynamic but shared" responses for 1 to 10 seconds: a homepage during the Big Book Sale, a "top sellers" list. Thousands of requests a second become one origin request every few seconds.
- Split the page. Cache the shared shell (layout, book details) and fetch the personal bits (cart count, "Hi Asha") separately from the browser. Or assemble at the edge with Edge Side Includes.
- Code at the edge. Platforms like Cloudflare Workers, CloudFront Functions and Lambda@Edge run small functions in the PoP: redirects, A/B test bucketing, auth token checks, header rewriting, image resizing, geolocation-based routing, and even rendering pages from edge-replicated data.
- API caching.
GET /books/42(public JSON) can be cached like a page. Personal APIs (GET /me/orders) can't.
Keep edge code small and stateless; anything needing the main database still goes to the origin, and debugging code spread across hundreds of PoPs is harder.
Memory code: "Cache the shared part for seconds, and do the small jobs near the customer."