Case Study: Video Streaming
Design a YouTube/Netflix-style platform — resumable uploads, the transcoding pipeline, adaptive bitrate streaming with HLS/DASH, CDNs, metadata, and view counting.
1. Requirements
Functional: upload videos, watch them smoothly on any device and network, search and browse, view counts, likes and comments.
Non-functional: smooth playback with fast start (under ~2 s), global scale, very high availability for playback; upload processing can take minutes. Storage and bandwidth cost matters enormously.
2. Estimates
- 1M uploads/day × 300 MB average = 300 TB/day of raw video; ×3–5 after transcoding to many formats.
- 100M daily viewers × 5 videos × 50 MB = 25 PB/day of egress — only a CDN can serve this.
3. High-level design
4. Upload
- The client asks the API for an upload session, then uploads directly to object storage with pre-signed URLs, bypassing app servers.
- Use resumable, chunked uploads (S3 multipart, tus protocol) so a dropped connection doesn't restart a 2 GB upload.
- On completion, an event starts processing; metadata status is "processing".
5. The transcoding pipeline
Raw uploads come in every codec and resolution. Processing converts them into streamable formats:
- Validate and inspect (format, duration, malware/content checks).
- Split the video into chunks (e.g. a few seconds each, at keyframes) so they can be encoded in parallel across many workers — this is a DAG of tasks.
- Encode each chunk into a bitrate ladder: e.g. 240p, 360p, 480p, 720p, 1080p, 4K, using codecs like H.264 (compatibility), VP9/AV1 (smaller files).
- Package into HLS/DASH segments (2–6 s each) plus manifest files.
- Generate thumbnails, captions and audio tracks; mark the video ready.
Popular videos justify expensive encodes (per-title optimisation, AV1); long-tail videos get cheaper ones.
6. Adaptive bitrate streaming
Videos aren't served as one file. With HLS or MPEG-DASH, the player downloads a manifest listing the renditions, then fetches small segments over plain HTTP, choosing the quality for each segment based on measured bandwidth and buffer level.
# master.m3u8 (HLS)
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
360p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720
720p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080
1080p/index.m3u8Start low for a fast first frame, step up as the buffer fills, step down when bandwidth drops — so playback rarely stalls. Because segments are ordinary HTTP files, any CDN can cache them.
7. Delivery: CDN
- Segments are immutable and cache perfectly at the edge (CDN).
- Popular content is pre-positioned in edge caches; Netflix places its own appliances (Open Connect) inside ISPs.
- Long-tail content is fetched from origin on demand (with an origin shield to protect storage).
8. Metadata, search and views
- Metadata (title, owner, status, renditions) in a sharded SQL or wide-column store, heavily cached.
- Search in Elasticsearch, fed by change events.
- View counts: clients send heartbeat events to Kafka; stream processing deduplicates and aggregates; counters update in batches. Approximate and slightly delayed is fine.
- Recommendations are a separate ML system consuming watch events.
9. Other concerns
- Live streaming: ingest (RTMP/SRT/WebRTC) → real-time transcoding → short segments (low-latency HLS), latency of a few seconds.
- DRM for licensed content (Widevine, FairPlay).
- Cost: storage tiers for cold videos, deleting unused renditions, efficient codecs.
- Upload directly to object storage with resumable multipart uploads.
- Transcode in parallel chunks into a bitrate ladder, packaged as HLS/DASH segments.
- Adaptive bitrate: the player picks quality per segment from bandwidth and buffer.
- CDNs serve nearly all bytes; segments are immutable and cache well.
- Metadata, search and view counts are separate, eventually consistent systems.