37. Video Streaming (YouTube / Netflix)
Business context. YouTube (UGC): upload volume, creator monetization, recommendations. Netflix: a curated catalog where egress cost and playback quality (rebuffering) drive both margin and retention. Rebuffering is a leading indicator of churn.
Requirements. Upload → process → watch; adaptive bitrate on any device; playback start < 2 s; rebuffer ratio < 0.5%. Scale: 500 hours uploaded per minute (UGC); hundreds of millions of hours watched per day.
Design.
Upload: client ─▶ resumable chunked upload (presigned URLs) ─▶ object storage (raw)
─▶ event ─▶ transcoding DAG (orchestrated workflow):
split into segments ─▶ parallel encode (codecs × resolutions × bitrates: H.264/HEVC/VP9/AV1)
─▶ package (HLS/DASH manifests) ─▶ thumbnails, captions (ASR), content moderation, fingerprinting
─▶ object storage (processed) ─▶ CDN / ISP-embedded caches
─▶ metadata DB (video, owner, status) ─▶ search index, recommendation features
Watch: client ─▶ API (auth, entitlements, DRM license) ─▶ manifest URL ─▶ CDN segments
client ABR algorithm picks bitrate per segment from measured throughput/buffer
playback telemetry (start time, rebuffers, bitrate) ─▶ QoE analytics
Deep dives.
- Transcoding is embarrassingly parallel: split the video into GOP-aligned chunks and encode them on many workers (spot instances; checkpoint per chunk). Popular content gets premium encodes (AV1, per-title or per-shot optimization); the long tail gets cheaper encodes. Spend compute where the views are.
- CDN strategy: popularity follows a power law. Preposition the head catalog in edge and ISP caches during off-peak hours (Netflix Open Connect); long-tail content is served from regional tiers or origin. See the CDN guide for multi-tier caching and range requests.
- ABR: buffer-based and throughput-based algorithms trade start time, quality, and rebuffering against each other. Server-side, the requirement is just "segments of multiple bitrates, cacheable".
- View counts: approximate, aggregated via streaming, deduplicated for fraud. Monetization-grade counts are reconciled in batch.
Strategic lens. Cost is dominated by egress, then encoding compute, then storage. Each has a lever (codec efficiency, cache hit ratio, ISP peering, tiering of rarely watched originals to cold storage). Tie quality investments to retention data: one codec upgrade that saves 30% of bytes at equal quality may be worth more than an entire feature team.
Follow-ups: live streaming (low-latency HLS, chunked transfer, glass-to-glass latency vs scale trade-off); DRM; copyright detection (perceptual fingerprints); recommendations (§25.1).
🏗️ Flow: one title, from upload to a phone on a train.
Upload (resumable, 8 MB parts) ─▶ raw bucket ─▶ "video_uploaded"
─▶ Workflow (DAG):
inspect → split into GOP-aligned chunks (≈ 2-10 s)
→ parallel encode on spot fleet: ladder {240p..4K} × codecs {H.264, VP9/AV1}
(per-title/per-shot ladder for popular titles; checkpoint per chunk; retry failed chunk only)
→ stitch + package (CMAF segments, HLS/DASH manifests) → DRM encrypt
→ QC (VMAF quality, black frames, A/V sync) → publish to origin
─▶ Popularity forecast → pre-position on edge/ISP caches at night
Playback: app → API (entitlement, geo license, DRM license) → manifest (CDN) → segments
ABR: buffer low → step down bitrate; network good → step up; telemetry → QoE pipeline
🌍 How it's built in production.
- Netflix per-title encoding (2015) and later dynamic optimizer (per-shot): instead of one bitrate ladder for everything, each title (and each shot) gets a ladder tuned to its complexity. Cartoons need far fewer bits than action films. Quality is measured with VMAF, Netflix's perceptual quality metric.
- Netflix Open Connect: Netflix-built cache appliances placed inside ISPs and at internet exchanges, filled overnight with the content predicted to be popular. Most traffic never crosses the wider internet.
- YouTube Argos (2021): a custom video transcoding chip (VCU), built because transcoding at YouTube's upload volume justified custom silicon. Build vs buy pushed all the way to hardware.
- Disney+ Hotstar handled more than 25 million concurrent viewers for cricket matches. Live-event scaling is about pre-warming, ladder-based capacity planning for known match moments, and graceful degradation (dropping non-critical features at peak).
- Facebook Live: request coalescing at edge caches for live segments (§22.7).
⚠️ Corner cases.
- Live-stream start herd: millions join in the first 60 s. Pre-warm the CDN, coalesce requests, and stagger client start with jitter.
- Live manifest caching: manifests update every few seconds. TTLs that are too long leave clients behind live, and TTLs that are too short hit origin hard. Use short TTLs with request coalescing.
- Copyright takedown: content must disappear from every CDN tier quickly. Use tag-based purge (surrogate keys, see the CDN guide) plus entitlement checks at manifest fetch time.
- Geo-licensing: the title is licensed in France but not Germany, and VPN users get around it. Do entitlement checks at the API with IP intelligence, and keep the CDN token per session.
- A failed chunk in a 3-hour encode: retry only that chunk. A design that restarts the whole encode wastes hours of compute.
- Seek and scrub: range requests and thumbnails (trick-play sprites) need their own caching path.