Capstone: Design a Video Streaming Service
Overview
A video platform lets people upload videos and lets millions watch them smoothly on any device and connection. The pipeline is write-heavy and slow on the upload side, and read-heavy and bandwidth-hungry on the viewing side, so each side is designed differently.
Detailed Notes
What is the task
Design a YouTube or Netflix style service: upload a video, process it so it plays everywhere, and stream it to viewers with little buffering.
The problem it solves
A raw video can be gigabytes in one format and one quality. Viewers have phones, TVs and browsers with very different screens and network speeds. The system must convert each video into many versions and deliver them from servers near the viewer, because streaming from one data centre would be slow and extremely expensive.
How it works: follow these steps
- Requirements. Functional: upload, process, play, search by title (metadata). Non-functional: playback starts quickly with little buffering, uploads are reliable and resumable, very high availability for viewing, and a new video may take a few minutes to become available (eventual consistency is fine).
- Estimate. Say 500,000 uploads a day, about 6 a second, averaging 200 MB: 100 TB a day of raw video. Transcoding into several qualities roughly doubles that, so plan for around 200 TB a day, tens of petabytes a year. For viewing, 100 million views a day, 5 minutes each at about 3 Mbps (about 112 MB) is around 11 PB a day, so bandwidth, not storage, dominates. That is why a CDN is essential.
- High-level design: upload path. The client asks an API for an upload URL, then sends the file in chunks directly to object storage so the video never passes through application servers. Chunking makes uploads resumable. Chunk checksums confirm nothing was corrupted. A message is then put on a queue.
- Deep dive: transcoding. Workers read the queue and process the video: split it into short segments (a few seconds each), encode every segment into several resolutions and bitrates (for example 240p to 4K), and generate thumbnails. Because segments are independent, many workers process one video in parallel. Output goes back to object storage and a metadata database records status and locations. Failed jobs are retried (they are idempotent) and poison jobs go to a dead-letter queue.
- Deep dive: delivery. Use adaptive bitrate streaming. The video is listed in a small manifest file pointing to the segments of each quality. The player downloads segments over HTTP and switches quality up or down as the network changes, so playback continues without stalling. Segments are static files, so the CDN caches them near viewers; the origin object storage is hit only on a CDN miss.
- Metadata and search. Titles, owners, view counts and status live in a database, with a cache for popular videos; read replicas and partitioning handle growth. Views can be counted through a queue rather than updated on every play.
- Security. Authenticate uploads, use signed, expiring URLs for private video, and encrypt data in transit.
A simple example
Asha uploads a 300 MB video. Her app requests an upload URL and sends 10 chunks to object storage; one fails and is retried alone. A message goes on the queue. Workers split the video into 4-second segments, encode each into 4 qualities and write them to storage, then mark the video as ready. Ben opens it on a slow phone connection: his player fetches the manifest from the CDN, starts with a low quality and switches up as his connection improves.
System design example
Upload: Client -> API -> Object storage (raw, chunked) -> Queue -> Transcoding workers -> Object storage (segments) + Metadata DB
Watch: Client -> API (metadata, signed URL) ; Client -> CDN -> (miss) Object storage
Bottlenecks and fixes:
- Transcoding is slow and CPU heavy: parallelise by segment, scale workers with queue depth.
- Popular video spikes: the CDN absorbs them; pre-warm caches for expected hits.
- Huge egress cost: high CDN hit ratio, and cache the most popular content closest to viewers.
- Metadata read load: cache and replicate.
- Worker crash mid-job: the queue redelivers; idempotent output paths avoid duplicates.
Trade-offs to mention: more renditions give better playback but cost more storage and compute; shorter segments adapt faster but create more files and requests; a long tail of rarely watched videos can be transcoded to fewer qualities or stored on cheaper storage.
Summary
- Upload straight to object storage in chunks; process asynchronously through a queue.
- Transcode in parallel segments into many qualities.
- Adaptive bitrate plus a CDN delivers smooth playback near the viewer.
- Bandwidth dominates the cost, so maximise the CDN hit ratio.
Architecture Diagram
Upload is processed asynchronously; viewing is served from the CDN.
- 1The client uploads chunks directly to object storage.
- 2A queue triggers workers that split and encode the video.
- 3Segments in several qualities are stored with a manifest.
- 4The CDN serves segments and the player adapts quality.
Common Interview Questions
Chapter quiz
Final Assessment · 6 questions
1.100 million new links a month is roughly how many writes per second?
2.In a chat system, why save a message before delivering it?
3.In a news feed, why use a hybrid of fan-out on write and on read?
4.Why must message processing be idempotent with at-least-once delivery?
5.Why does a distributed rate limiter make its check-and-update atomic?
6.In a video streaming service, why is a CDN essential?
0 of 6 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

