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
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Which Cache-Control? One line each.
app.3f9a1c.js, a versioned JavaScript bundle.- The "My orders" page.
- A book page that's the same for everyone and changes a few times a day.
- The checkout page.
- A "top sellers right now" list during a sale.
B. True or false? Explain in one line.
- A CDN makes a slow, personalised API response faster by caching it.
Vary: Cookieis a good way to cache pages per user at the CDN.- Purging a URL from the CDN also removes it from users' browsers.
- An origin shield reduces the number of requests reaching the origin.
- Video segments are cached by CDNs the same way as images.
Level 2: Calculate
- A page needs 6 round trips before it shows. How long is that from Guwahati to a Mumbai origin at an assumed 60 ms RTT, and to a Guwahati PoP at 8 ms?
- The site gets 10,000 requests a second. How many reach the origin at a 95% hit ratio? At 99%?
- The homepage weighs 1.5 MB and gets 2 million views a day. How much data is served per day, and how much comes from the origin at a 95% byte hit ratio?
- A 20-minute video is cut into 6-second segments in 5 qualities. How many segment files? How big is one 720p segment at 2.8 Mbps?
- A new cover is requested at 50 PoPs within a minute. How many origin fetches without and with a shield?
Level 3: Build
In Python:
cache_key(url)that lowercases the host, drops tracking parameters and sorts the rest; show 50 tracking variants collapsing to 1 key.parse_cache_control(header)andcdn_ttl(header)that respects-maxage,privateandno-store.- An
Edgecache with TTLs,stale-if-error,purge_urlandpurge_tag. segments(duration, segment_len)andpick_rendition(measured_kbps)over a bitrate ladder.route(cdns_healthy, preference)for multi-CDN failover.
Level 4: Debug
After a "speed up the site" change, a customer emails: "I logged in and saw someone else's name and orders." The change set every HTML page to be cached at the CDN:
python
- Explain exactly how one customer saw another's account page.
- Fix it, and list two other places this rule causes harm.
- What should you do right now, before the fix is deployed?
Level 5: Design
Design content delivery for the bookstore: 5 million daily visitors, mostly in India with some in the Gulf and the UK; a homepage that changes during sales; 2 million book pages; covers; versioned JS/CSS; and 10,000 author-interview videos.
Deliver:
- What's cached at the CDN, with
Cache-Controlfor each type. - Cache keys: what's in them, what's stripped.
- How changes (price updates, cover changes, takedowns) reach users.
- How the origin is protected during a Big Book Sale.
- Video delivery, and what happens if your CDN has a major outage.