LESSON 13.3
CDNs and media delivery
Edge caches, cache keys and versioned URLs, origin shields, private access, and serving video as cacheable adaptive-bitrate segments.
6 min read
Object storage keeps bytes safe; a content delivery network (CDN) caches them near users, reducing round trips and origin traffic. It's the caching module applied to files at global scale.
A hit stays at the edge
Requests reach a nearby CDN edge. A hit returns from cache; a miss fetches from the origin, often an object store, and caches a copy according to policy.
At 100,000 requests a second and a 95% edge hit ratio, roughly 5,000 reach the next layer. Watch byte hit ratio too: a video miss costs more bandwidth than an icon miss.
The cache key decides what's shared
A cache key includes host, path and selected parameters or headers. Include inputs that change the representation, but avoid irrelevant variation that fragments the cache.
/photo/92?w=200and/photo/92?w=800need different entries if they return different sizes.- Tracking parameters shouldn't create a new cached copy of identical bytes.
- Arbitrary sizes can create unlimited variants. Allow a small set of transformations and normalize their keys.
- Personalized or private responses need an explicit access policy; sharing a key carelessly can leak data.
A Vary header describes representation differences; configure the CDN cache policy to honor the intended inputs too.
TTLs, invalidation and versions
Short TTL
- Changes become visible soon
- Simple expiration
- More origin requests
- Still allows staleness
Pick it when: Mutable manifests and public content that changes frequently.
Purge / invalidation
- Removes a cached version before expiry
- Propagation takes time
- Doesn't clear every browser's cache
Pick it when: Urgent removal and corrections at an existing URL.
Versioned URLs
- Long cache lifetime
- New versions don't wait for old entries to expire
- References must point to the new version
- Old versions need retention rules
Pick it when: Images, scripts and immutable media segments.
Immutable URLs such as /assets/photo92.v3.webp can use long max-age and immutable. The mutable record pointing to version 3 needs shorter freshness. Keep old objects while clients still reference them.
An origin shield is a shared cache between edges and origin. With request collapsing, it combines simultaneous cold misses into fewer fetches, at the cost of another hop on some misses.
Private content
The API authenticates and authorizes a viewer, then issues a short-lived CDN-signed URL or cookie. The CDN must validate access before serving a cache hit, and the origin must reject direct public access. Otherwise cached bytes or a guessed origin URL can bypass the permission check.
Signed access can still share cached bytes when the CDN safely separates authorization from the object cache key. Only exclude signature parameters under such a policy. A leaked signed URL works until expiry; short lifetimes limit that window, while immediate revocation needs additional enforcement.
Video: cache segments, adapt quality
HLS and DASH split video into short segments and describe available renditions in a manifest. Encode aligned segments at several bitrates, perhaps 1, 3 and 6 Mbps. The player chooses the next segment based on estimated throughput and buffered playback, moving down in quality before the buffer empties.
A 4-second segment at 3 Mbps is roughly 1.5 MB, before overhead. Popular segments benefit from CDN reuse. Live manifests need short freshness; immutable segments can use long TTLs. Encoding costs more, but the origin doesn't stream separately to every viewer.
Try it
Video Streaming HLD
Deliver adaptive video through a CDN with versioned segments and controlled private access.