Lesson 12 of 16
26. 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.

  1. app.3f9a1c.js, a versioned JavaScript bundle.
  2. The "My orders" page.
  3. A book page that's the same for everyone and changes a few times a day.
  4. The checkout page.
  5. A "top sellers right now" list during a sale.

B. True or false? Explain in one line.

  1. A CDN makes a slow, personalised API response faster by caching it.
  2. Vary: Cookie is a good way to cache pages per user at the CDN.
  3. Purging a URL from the CDN also removes it from users' browsers.
  4. An origin shield reduces the number of requests reaching the origin.
  5. Video segments are cached by CDNs the same way as images.

Level 2: Calculate

  1. 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?
  2. The site gets 10,000 requests a second. How many reach the origin at a 95% hit ratio? At 99%?
  3. 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?
  4. 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?
  5. 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:

  1. cache_key(url) that lowercases the host, drops tracking parameters and sorts the rest; show 50 tracking variants collapsing to 1 key.
  2. parse_cache_control(header) and cdn_ttl(header) that respect s-maxage, private and no-store.
  3. An Edge cache with TTLs, stale-if-error, purge_url and purge_tag.
  4. segments(duration, segment_len) and pick_rendition(measured_kbps) over a bitrate ladder.
  5. 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
  1. Explain exactly how one customer saw another's account page.
  2. Fix it, and list two other places this rule causes harm.
  3. 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:

  1. What's cached at the CDN, with Cache-Control for each type.
  2. Cache keys: what's in them, what's stripped.
  3. How changes (price updates, cover changes, takedowns) reach users.
  4. How the origin is protected during a Big Book Sale.
  5. Video delivery, and what happens if your CDN has a major outage.