System Design · Design Question
Design YouTube
Video upload, transcoding, storage, and streaming via CDN — the interview path from phone camera to smooth playback.
Concrete example: Upload a 4K video to YouTube → store the original in object storage → transcode workers produce 360p/720p/1080p HLS segments → viewers stream adaptive chunks from a nearby CDN edge.
1. Requirements
Functional
- Upload videos; process into multiple bitrates/resolutions.
- Stream with adaptive bitrate (HLS/DASH); thumbnails and metadata.
- View counts (approximate OK); watch history optional later.
Non-functional
- Low startup time; smooth ABR under varying bandwidth.
- Huge storage and egress; CDN mandatory; resumable uploads.
- Processing async — upload returns quickly; status becomes READY later.
Estimation snippet
2. API
3. Data model
4. High-level design
5. Deep dive — processing pipeline
- Validate file; virus/malware scan optional queue.
- Probe duration/codec; reject unsupported.
- Generate ladder: 360p/720p/1080p… + audio.
- Package HLS/DASH segments; create master playlist.
- Extract thumbnails; maybe preview sprites.
Transcoding is CPU/GPU heavy — scale workers on queue depth; never do it inline in the upload request.
6. Playback and CDN
- Signed URLs for private/unlisted videos.
- Origin shield to protect object store on sudden virality.
7. Scaling storage & cost
- Raw + multiple renditions ≈ several times original size.
- Lifecycle: delete raw after processing, or keep for re-encode.
- Cold storage classes for rarely watched content.
8. Views and recommendations (brief)
Count views asynchronously (client beacon → queue → aggregate). Recommendations are a separate system (candidate generation + rank); mention but do not boil the ocean unless asked.
9. Failure cases
- Transcode fail → retry; mark FAILED; notify user.
- Partial upload → resumable protocol; GC incomplete after TTL.
- CDN poison file → versioned paths + purge.
10. Adaptive bitrate details
The player monitors buffer health and bandwidth estimates, switching renditions at segment boundaries. Keep segment duration short enough for quick switches (e.g. 2–6s) but not so short that request overhead explodes.
11. Hot vs cold video
- Viral video: CDN hit ratio high; origin shield protects storage.
- Long-tail: more origin pulls; cheaper storage class OK.
- Pre-warm CDN for known big premieres when product can predict.
12. Edge cases
- User retries upload completion → make completion idempotent for the upload session.
- Viewer changes network speed quickly → switch only at segment boundaries and protect buffer health.
- Video is deleted while cached → authorization plus CDN purge/versioned manifests prevent continued playback.
Interview talking points
- Lead with: Separate upload/transcoding from CDN playback.
- Resumable upload to object store; process async.
- Transcode ladder + HLS/DASH segments for ABR playback.
- CDN in front of segments; signed URLs for private content.
- Metadata DB small; blobs huge — never store video in SQL.
- Scale workers on queue lag; GPU optional.
- View counts via async aggregation.
- Lifecycle policies control storage cost.