Case study
Design YouTube-style Video Streaming
Upload, transcode, and stream video globally with adaptive bitrate and CDN edge delivery.
CDNtranscodingHLS
Requirements
- Functional: upload video; transcode to multiple resolutions; stream via web/mobile/TV
- Adaptive bitrate (ABR): HLS/DASH segments; player switches quality by bandwidth
- Search, recommendations, view counts (eventual consistency OK)
- Non-functional: playback start < 2 sec; 99.9% availability for CDN path
- Scale: 100M DAU; 500K uploads/day; avg 5 min video, 500 MB raw
Back-of-envelope Estimation
| Metric | Calculation | Result |
|---|---|---|
| Upload/day | 500K × 500 MB | 250 PB/day raw (before dedup/CDN cache) |
| Upload QPS | 500K / 86,400 | ~6/sec avg |
| Views/day | 100M × 3 videos × 5 min | 1.5B view-minutes/day |
| CDN egress peak | Assume 20M concurrent × 2 Mbps | ~40 Tbps peak (CDN scale) |
| Transcode fleet | 500K × 10 min video × 4 renditions | GPU worker pool; queue-based |
API Design
| Endpoint | Description |
|---|---|
| POST /v1/videos/upload/init | Returns uploadId + pre-signed multipart URLs |
| POST /v1/videos/{id}/complete | Triggers transcode pipeline |
| GET /v1/videos/{id}/manifest.m3u8 | HLS master playlist (CDN cached) |
| GET /v1/videos/{id} | Metadata, thumbnails, view count |
| POST /v1/videos/{id}/view | Async view increment (batched) |
Data Model
| Entity | Storage |
|---|---|
| videos | video_id, user_id, title, status, duration, created_at — SQL |
| renditions | video_id, resolution, bitrate, manifest_path — SQL |
| segments | Blob paths in S3/CDN: /{video_id}/{rendition}/seg_{n}.ts |
| view_counts | video_id, count — Redis + periodic flush to SQL |
High-level Design
Upload ──► S3 (raw) ──► Transcode Queue ──► GPU Workers
│
S3 segments (360p/720p/1080p)
│
CDN origin ──► Edge POPs worldwide
│
Client HLS player (ABR)Deep Dive: Transcoding Pipeline
- Probe raw file: duration, codec, resolution
- Generate renditions: 360p@800kbps, 720p@2.5Mbps, 1080p@5Mbps
- Segment into 2–6 sec .ts chunks; generate .m3u8 playlists
- Thumbnail sprites every 10 sec for seek preview
- Mark video status = ready; purge CDN manifest cache
Deep Dive: CDN & ABR
Player downloads master playlist, picks initial rendition by bandwidth estimate, switches on buffer health. Popular videos cached entirely at edge; long tail fetched from regional origin shield. Signed URLs prevent hotlinking.
First segment optimization
Encode first segment at lower quality for faster time-to-first-frame; upgrade within 2 seconds.
Failure Modes & Monitoring
| SLO | Target |
|---|---|
| Time to first frame p99 | < 2 sec |
| Transcode completion p99 | < 2× realtime |
| CDN availability | 99.99% |
- Alert: transcode queue > 1 hr backlog, CDN origin 5xx, rebuffer rate > 5%
- Dashboard: egress by region, popular video cache hit ratio, upload failure rate