Sub topic 10
Delivering video through a CDN
This chapter assumes you know HTTP caching basics (Cache-Control, cache hits and misses), object storage, and the segment and playlist layout from the packaging chapter. It covers how a content delivery network (CDN) serves video segments so that viewers get low latency and your origin storage stays lightly loaded.
Why video needs a CDN
A viewer's player fetches many small files: a playlist, then a segment every few seconds. If every request went to your origin (the object store holding the packaged files), you would pay for long round trips and the origin would carry the full viewing load. A CDN is a network of edge caches placed close to viewers. The first request for a file in a region misses the cache and goes to the origin. Later requests are served from the edge.
Segments are immutable and popular titles are watched by many people, so video caches very well.
Cache keys and TTLs
Segments and playlists behave differently, so give them different cache rules.
| File type | Changes after publish? | Suggested caching |
|---|---|---|
| Video segments (`.ts`, `.m4s`) | No | Long TTL, for example one year, marked `immutable` |
| Master playlist (VOD) | Rarely | Moderate TTL, for example minutes to hours |
| Media playlist (VOD) | No, once finished | Long TTL |
| Live media playlist | Every few seconds | TTL of about one segment duration or less |
Set the headers at the origin so the CDN and the browser agree:
Cache-Control: public, max-age=31536000, immutableIf you must replace a file, publish it under a new path (for example, include a version or content hash). Purging caches is slow and unreliable compared with changing the URL.
Keep the cache key small. If the key includes query strings that vary per viewer, such as a session token, every viewer gets a separate cache entry and the hit ratio collapses. Put authorization in a signed token that the CDN validates but does not include in the key, or in a cookie that is excluded from the key.
Origin shield and request collapsing
With many edge locations, a new segment can miss at hundreds of edges at once, and each one asks the origin. An origin shield is a middle cache tier that all edges consult before the origin. It turns hundreds of misses into one origin fetch. Request collapsing does the same within a single cache: concurrent requests for the same uncached object wait for one origin fetch instead of each starting their own.
Measuring the result
The key number is the cache hit ratio, the share of requests answered without contacting the origin. Track it separately for segments and playlists, since a healthy overall ratio can hide a badly cached playlist. Also watch origin egress, time to first byte at the edge, and the miss rate for newly published titles.
Two common errors:
- Caching an error response (such as a 404 for a segment not yet uploaded) for too long, so viewers keep failing after the file appears. Use a short negative-cache TTL.
- Letting the origin return
Cache-Control: no-cacheby default on object storage, which makes the CDN revalidate every request.
Warming and placement
Popular new releases can be pre-fetched, or warmed, into the cache before launch by requesting the first segments of each rendition. Rarely watched titles in the long tail will miss often, so the origin must handle those at reasonable latency. Multi-CDN setups add resilience and let you steer traffic by cost, at the price of more configuration to keep consistent.
Exercise
A viewer-specific token is appended as a query string to every segment URL, and your cache hit ratio drops to near zero. What is the likely cause, and how do you fix it?
Show answer
The query string is part of the cache key, so each viewer's token creates a separate cache entry that is never reused. Remove the token from the cache key (or move it to a header or cookie excluded from the key) and have the CDN validate it at the edge, so all viewers share one cached copy of each segment.
Exercise
Why should a finished VOD segment have a long TTL while a live media playlist has a very short one?
Show answer
A finished segment never changes, so it can be cached for a long time safely. A live playlist is rewritten every few seconds as new segments appear, so a long TTL would show viewers a stale list and they would miss the newest segments.