Lesson 31 of 38Case studies
Design a Video Streaming Service
Design YouTube or Netflix-style video with resumable uploads, a transcoding pipeline, adaptive bitrate streaming, and CDN delivery.
Video is most of the internet's traffic, by a wide margin. A streaming service has to accept huge uploads from shaky connections, turn each one into a dozen versions for every screen and network, and then deliver petabytes a day without buffering. This case study is about pipelines and bandwidth rather than databases, which makes it a good contrast to the ones before it.
Requirements
Functional
- Creators upload videos (up to several GB) and see them processed and published.
- Viewers watch on phones, laptops, and TVs, on networks from 1 Mbps mobile to fibre.
- Playback adapts quality to the network without stopping.
- Each video has a title, description, thumbnail, and a view count.
Non-functional
- Playback starts in under 2 seconds and rarely rebuffers.
- Uploads survive network drops (resume, do not restart).
- Processing a new video takes minutes, not hours.
- Extremely read-heavy: a video is uploaded once and watched thousands or millions of times.
Out of scope: recommendations, comments, live streaming (it reuses most of this with lower latency), DRM details, monetisation.
Back-of-the-envelope numbers
| Quantity | Math | Result |
|---|---|---|
| Uploads | 500 hours of video per minute | about 720,000 hours a day |
| Stored per hour of video | all renditions together | about 3 GB |
| New storage | 720,000 × 3 GB | about 2 PB a day |
| Watching | 1 billion hours a day / 24 | about 42 million concurrent streams |
| Average bitrate | 3 Mbps | |
| Peak egress | 42M × 3 Mbps | about 125 Tbps |
No data center can push 125 terabits a second to the whole planet by itself. Delivery must come from a CDN with servers inside ISPs and close to viewers. Netflix runs its own (Open Connect) for exactly this reason.
API
POST /v1/uploads { "filename": "trip.mov", "size": 4294967296 }
201 { "upload_id": "up_33", "chunk_size": 8388608, "urls": "per-chunk presigned PUT URLs" }
PUT <presigned URL for chunk n> (raw bytes, straight to object storage)
GET /v1/uploads/up_33 → { "received_chunks": [0, 1, 2, 4] }
POST /v1/uploads/up_33/complete → { "video_id": "v_901", "status": "processing" }
GET /v1/videos/v_901 → { "title": "...", "manifest": "https://cdn.example/v_901/master.m3u8", "views": 18234 }
POST /v1/videos/v_901/views (heartbeat from the player)Data model
- videos (sharded SQL or a key-value store):
video_id, owner_id, title, description, status (uploading, processing, ready, failed), duration, manifest_url, thumbnail_url, created_at. - renditions:
video_id, resolution, bitrate, codec, storage_prefix. - object storage: raw uploads in one bucket, processed segments in another:
v_901/720p/seg_00042.m4s. - view counts: a counter per video in a fast store, flushed to the
videosrow periodically.
High-level design
Upload. The creator's app starts an upload session and gets an upload_id, a chunk size (8 MB), and presigned URLs.
Deep dive 1: the upload and transcoding pipeline
Chunked, resumable upload. A 4 GB file over hotel Wi-Fi will fail at some point. Splitting it into 8 MB chunks means a failure costs at most one chunk. The client asks the server which chunks it already has and resumes. S3 multipart upload works exactly like this, and the bytes never pass through our API servers.
Press Run (⌘ Enter) to see what happens.Transcoding. The raw file (say 4K ProRes) is useless for most viewers. The pipeline turns it into a bitrate ladder: the same video at several resolutions and bitrates.
Top rungs for TVs and fast connections. Newer codecs (VP9, AV1) reach the same quality with 30–50% fewer bits but cost much more CPU to encode.
Encoding a 2-hour film at six quality levels on one machine could take hours. Instead, the pipeline is a DAG (a graph of tasks with dependencies): split the video into 10-second pieces, transcode every piece at every rung in parallel across hundreds of workers, then stitch the results. Thumbnails, audio tracks, and captions are parallel branches of the same DAG. The work queue gives you retries per task: if one worker dies, only its piece is redone.
Order the processing pipeline for a new upload.
- Tap the steps below in the right order.
Deep dive 2: adaptive bitrate streaming
With HLS (Apple's HTTP Live Streaming) or DASH (the open standard), a video is not one file. It is thousands of small segments, 2 to 6 seconds each, for every rung of the ladder, plus manifests that list them.
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080
1080p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720
720p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1200000,RESOLUTION=854x480
480p/index.m3u8The player picks a rendition for each segment, based on measured download speed and how many seconds are buffered. Segments line up at the same timestamps across rungs, so the viewer never sees the switch happen, only a sharper or softer picture. Watch a player react to a train going into a tunnel:
Start low so the first frame appears fast. The first segment downloads quickly, measuring about 6 Mbps.
Here is a simple version of that decision logic. Real players (hls.js, ExoPlayer, Shaka) blend throughput and buffer level in smarter ways, but the shape is the same.
Press Run (⌘ Enter) to see what happens.Deep dive 3: CDN delivery, metadata, and view counts
CDN. The manifest and segments are static, immutable files with long cache lifetimes, the perfect CDN workload. Popular videos are served almost entirely from edges; the long tail (millions of videos with a handful of views) misses the edge and hits the origin. Platforms push the most popular content to edges ahead of time (Netflix fills its ISP-hosted boxes overnight with what it predicts people will watch).
Metadata is small and read-heavy: cache watch-page data aggressively, shard videos by video_id.
View counts have the same problem as Reddit votes: a viral video gets 50,000 views a second. Players send events to a queue; counter workers batch them per video, dedupe (one view per viewer per session after, say, 30 seconds watched), and periodically flush totals to the database. Displayed counts lag by seconds or minutes, which nobody minds.
Match the problem to the mechanism.
Bottlenecks, failures, and 10x scale
- Transcoding backlog. Autoscale workers on queue depth, use spot instances (jobs are retryable), and prioritise popular creators' uploads.
- Long-tail storage cost. Videos nobody watches keep only a few rungs and move to infrequent-access storage; regenerate other rungs on demand if they become popular.
- Origin overload on a miss storm. A new episode released to millions at once: pre-warm the CDN, and use a mid-tier cache (origin shield) so edges do not all hit S3 at the same time.
- Corrupt or failed transcode. The DAG retries the failed piece; after repeated failures the video is marked
failedand the creator is told. - 10x: everything scales horizontally: more CDN capacity (often the biggest bill), more transcoders, more storage. Switching to AV1 for popular titles cuts bandwidth by a third, which at this scale is worth the extra encoding CPU.
- transcoding
- Re-encoding a video into different formats, resolutions, and bitrates.
- bitrate ladder
- The set of renditions (for example 240p to 4K) a video is encoded into.
- HLS / DASH
- Streaming formats that serve video as short segments plus manifests over plain HTTP.
- adaptive bitrate
- The player picks the quality of each segment based on bandwidth and buffer, switching as conditions change.
- multipart upload
- Uploading a large file as independent chunks that can be retried and resumed individually.
- origin shield
- A mid-tier cache between CDN edges and storage so many edge misses become one origin fetch.