Case Study: Design YouTube — Video Streaming Platform
“YouTube giống một đài truyền hình quốc gia — nhưng thay vì một kênh chiếu cho tất cả, mỗi người xem một kênh khác nhau, cùng lúc hàng triệu người. Và ‘đài truyền hình’ đó phải phát mượt mà trên mọi thiết bị, mọi tốc độ mạng, mọi quốc gia.”
Tags: system-design case-study video-streaming youtube cdn transcoding alex-xu Student: Hieu Prerequisite: Tuan-02-Back-of-the-envelope · Tuan-03-Networking-DNS-CDN · Tuan-08-Message-Queue Lien quan: Tuan-06-Cache-Strategy · Tuan-07-Database-Sharding-Replication · Tuan-13-Monitoring-Observability · Tuan-15-Data-Security-Encryption Reference: Alex Xu, System Design Interview Vol 1 — Chapter 14: Design YouTube
0. Context & Why — Tai sao Video Streaming kho?
0.1 Analogie: Dai truyen hinh quoc gia thoi dai so
Hieu, hay tuong tuong mot dai truyen hinh quoc gia. Truoc day, dai phat mot kenh duy nhat — tat ca moi nguoi cung xem mot chuong trinh. Don gian.
Bay gio tuong tuong moi nguoi xem mot kenh khac nhau, cung luc. Nguoi xem phim hanh dong 4K, nguoi xem clip hai 360p tren dien thoai 3G, nguoi tai video len, nguoi dang stream live. Tat ca dien ra dong thoi voi hang trieu nguoi.
Do chinh la YouTube. Va do la ly do no la mot trong nhung bai toan system design kho nhat.
0.2 Nhung thach thuc cot loi
| Thach thuc | Giai thich | Vi sao kho? |
|---|---|---|
| Data volume | Video la data type nang nhat — 1 phut video 1080p ~ 150MB | Khong gi trong web lon bang video |
| Bandwidth | Streaming can bang thong lien tuc, khong phai burst nhu text/image | Moi user tieu thu 2-5 Mbps lien tuc |
| Diversity | Muon van thiet bi: TV 4K, dien thoai cu, tablet, laptop | Phai transcode ra nhieu resolution + codec |
| Global | User o moi noi tren the gioi | CDN phai phu khap, latency phai thap |
| Cost | CDN bandwidth la chi phi #1 — len toi hang ty USD/nam | Toi uu chi phi CDN la song con |
| Upload vs Watch | Upload it nhung nang (processing), Watch nhieu nhung nhe (serving) | Hai pipeline hoan toan khac nhau |
0.3 YouTube theo so lieu thuc te (tham khao)
| Metric | Con so | Y nghia |
|---|---|---|
| DAU | ~2 ty | Gan 1/4 dan so the gioi |
| Video duoc upload moi phut | 500 gio | Khong bao gio xem het |
| Tong video da upload | > 800 trieu | Petabyte-scale storage |
| Avg watch time/user/day | ~30 phut | Engagement cuc cao |
| So resolution can transcode | 5-8 | 144p, 240p, 360p, 480p, 720p, 1080p, 1440p, 4K |
| CDN cost/nam (uoc tinh) | $1-5 ty | Chi phi lon nhat cua YouTube |
Aha Moment: Video streaming khong phai la “gui file cho user.” No la chia file thanh hang ngan manh nho (segments), chon dung resolution cho tung thoi diem, va gui tung manh mot — giong nhu dut com cho em be, tung muong mot, dung toc do em be nuot duoc.
1. Step 1 — Understand the Problem & Establish Design Scope
1.1 Clarifying Questions (Cau hoi lam ro)
Trong interview, luon hoi truoc khi thiet ke. Duoi day la cac cau hoi quan trong:
| Cau hoi | Tra loi | Ghi chu |
|---|---|---|
| Nhung feature nao can thiet ke? | Upload video + Stream video | Core features |
| Clients nao? | Mobile app, web browser, smart TV | Multi-platform |
| DAU bao nhieu? | 5 trieu | Quy mo trung binh-lon |
| Thoi gian xem trung binh/ngay? | 30 phut | Engagement metric |
| Can ho tro nhieu resolution? | Co — tu 240p den 4K | Adaptive bitrate |
| Can ho tro nhieu ngon ngu? | Co — subtitle, audio tracks | i18n |
| Video dai toi da bao lau? | 12 gio (nhu livestream rerun) | Anh huong storage |
| Can DRM (bao ve ban quyen)? | Co | Dac biet cho premium content |
| Upload file toi da bao lon? | 256 GB | Cho video dai, 4K |
| Co phan biet user mien phi va tra tien? | Co — priority transcoding | Business logic |
1.2 Functional Requirements (Yeu cau chuc nang)
- FR1: Upload video — user upload video tu client, he thong xu ly va luu tru
- FR2: Stream video — user xem video muot ma, khong giat lag
- FR3: Multi-resolution — tu dong chuyen doi video thanh nhieu resolution (240p → 4K)
- FR4: Adaptive bitrate — tu dong dieu chinh chat luong theo toc do mang cua user
- FR5: Search — tim kiem video theo title, description, tags
- FR6: Thumbnail generation — tu dong tao thumbnail cho moi video
- FR7: Video metadata — title, description, view count, like/dislike, comments
- FR8: Resumable upload — neu upload bi gian doan, tiep tuc tu cho dang do
1.3 Non-functional Requirements (Yeu cau phi chuc nang)
| Requirement | Target | Giai thich |
|---|---|---|
| Availability | 99.99% | Video platform la entertainment — downtime = mat user |
| Latency (playback start) | < 2 giay | User nhan play → video phai bat dau trong 2s |
| Buffering ratio | < 1% | Ty le thoi gian user phai doi buffer |
| Upload reliability | 99.9% | Upload khong duoc mat file |
| Scalability | 5M DAU, scale to 50M | Thiet ke cho tuong lai |
| Durability | 99.999999999% (11 nines) | Video upload len roi → khong bao gio mat |
| Global reach | Serve tu CDN gan nhat | User o VN xem video phai nhanh nhu user o My |
1.4 Pham vi thiet ke (Out of scope)
De tap trung, chung ta khong thiet ke:
- Recommendation engine (ML-based) — chi thiet ke data pipeline cho no
- Comment system — da co trong case study khac
- Live streaming — chi focus vao Video-on-Demand (VOD)
- Monetization / Ads system
- Content moderation chi tiet (chi overview)
2. Step 2 — Propose High-Level Design
2.1 Hai luong chinh (Two Main Flows)
Hieu, toan bo YouTube co the chia thanh hai luong chinh hoan toan khac nhau:
| Luong | Mo ta | Dac diem |
|---|---|---|
| Video Upload Pipeline | User upload video → he thong xu ly → luu tru → phan phoi | Write-heavy, CPU-intensive, async |
| Video Streaming | User nhan play → he thong tra ve video segments | Read-heavy, bandwidth-intensive, real-time |
Key Insight: Hai luong nay co yeu cau hoan toan khac nhau. Upload can processing power (transcoding). Streaming can bandwidth (CDN). Thiet ke rieng biet cho tung luong.
2.2 High-Level Architecture Overview
graph TB subgraph Clients A[Web Browser] B[Mobile App] C[Smart TV] end subgraph "Upload Flow" D[API Gateway / Load Balancer] E[Upload Service] F[Original Video Storage<br/>Blob Store] G[Transcoding Service<br/>DAG Scheduler] H[Transcoded Video Storage<br/>Blob Store] I[Thumbnail Generator] J[Metadata Service] K[Message Queue<br/>Kafka/RabbitMQ] end subgraph "Streaming Flow" L[CDN Edge Server] M[CDN Regional Server] N[CDN Origin Server] O[Metadata Cache<br/>Redis] end subgraph "Data Stores" P[(Video Metadata DB<br/>MySQL/Vitess)] Q[(User Activity DB<br/>Cassandra)] R[Search Index<br/>Elasticsearch] end A & B & C -->|Upload| D D --> E E --> F E -->|Trigger| K K --> G G --> H G --> I H --> N E --> J J --> P J --> R A & B & C -->|Stream| L L -->|Cache miss| M M -->|Cache miss| N N -->|Fetch| H J --> O O --> L
2.3 Component Overview
| Component | Chuc nang | Tech stack (vi du) |
|---|---|---|
| API Gateway | Authentication, rate limiting, routing | Kong, AWS API Gateway |
| Upload Service | Nhan file, chia chunk, luu vao blob store | Custom service + pre-signed URL |
| Original Storage | Luu video goc truoc khi transcode | AWS S3, Google Cloud Storage |
| Transcoding Service | Chuyen doi video thanh nhieu resolution/codec | FFmpeg workers, AWS Elastic Transcoder |
| Transcoded Storage | Luu video da transcode o moi resolution | S3, GCS (rieng bucket) |
| CDN | Phan phoi video den user | CloudFront, Akamai, Google CDN |
| Metadata Service | Quan ly thong tin video | REST API + MySQL/Vitess |
| Message Queue | Decouple upload va transcoding | Kafka, RabbitMQ → Tuan-08-Message-Queue |
| Cache | Cache metadata, CDN config | Redis → Tuan-06-Cache-Strategy |
3. Step 3 — Design Deep Dive
3.1 Video Upload Pipeline — Chi tiet
3.1.1 Upload Flow chi tiet
Khi user upload video, day la toan bo quy trinh dien ra:
Buoc 1 — Pre-signed URL
- Client goi API: “Toi muon upload video, file size = 2GB”
- API Server kiem tra authentication, quota, file type
- Neu hop le → tao pre-signed upload URL tro thang toi Blob Storage (S3)
- Client upload truc tiep len S3, khong di qua API server → giam tai server
Tai sao pre-signed URL? Neu 1000 user upload cung luc, moi file 2GB, API server phai xu ly 2TB bandwidth. Voi pre-signed URL, traffic di thang vao S3 (designed for massive throughput). API server chi xu ly metadata (nhe).
Buoc 2 — Chunked Upload (Resumable)
- File 2GB duoc chia thanh nhieu chunks (thuong 5-10MB/chunk)
- Client upload tung chunk mot
- Neu bi mat mang → chi can upload lai chunk bi loi, khong phai upload lai tu dau
- Server theo doi progress: chunk 1/400 ✓, chunk 2/400 ✓, … chunk 235/400 ✗ (retry)
Buoc 3 — Original Storage
- Khi tat ca chunks da upload xong → S3 ghep lai thanh file hoan chinh
- File goc duoc luu vinh vien (hoac theo retention policy)
- Mot event duoc push vao Message Queue → trigger transcoding pipeline
Buoc 4 — Transcoding (xem phan 3.1.2)
Buoc 5 — CDN Distribution
- Video da transcode duoc push len CDN origin
- CDN tu dong replicate ra edge servers khi co request
Buoc 6 — Metadata Update
- Sau khi transcode xong, Metadata DB duoc update:
- Video status: “processing” → “ready”
- Available resolutions: [240p, 360p, 480p, 720p, 1080p]
- Duration, thumbnail URL, manifest file URL
- User nhan notification: “Video da san sang!“
3.1.2 Video Transcoding — DAG Architecture
Day la phan phuc tap nhat cua toan bo he thong. Transcoding khong phai chi la “chuyen 1080p thanh 720p.” No la mot DAG (Directed Acyclic Graph) gom nhieu buoc:
graph TD A[Original Video File] --> B[Video Splitting<br/>Chia thanh segments] B --> C1[Segment 1] B --> C2[Segment 2] B --> C3[Segment 3] B --> C4[Segment N...] C1 --> D1[Encode H.264 1080p] C1 --> D2[Encode H.264 720p] C1 --> D3[Encode H.264 480p] C1 --> D4[Encode H.264 360p] C1 --> D5[Encode H.264 240p] C1 --> D6[Encode VP9 1080p] C1 --> D7[Encode VP9 720p] C1 --> D8[Encode AV1 1080p] C2 --> E1[Encode H.264 1080p] C2 --> E2[Encode H.264 720p] C2 --> E3[...] D1 & D2 & D3 & D4 & D5 & D6 & D7 & D8 --> F[Merge Segments<br/>Per resolution/codec] A --> G[Audio Extraction] G --> G1[AAC 128kbps] G --> G2[AAC 256kbps] G --> G3[Opus 128kbps] A --> H[Thumbnail Generation<br/>Extract key frames] H --> H1[Thumbnail 1] H --> H2[Thumbnail 2] H --> H3[Thumbnail 3] A --> I[Watermark Overlay<br/>Optional - for copyright] F --> J[Generate Manifest Files<br/>HLS .m3u8 / DASH .mpd] G1 & G2 & G3 --> J J --> K[Upload to Transcoded Storage] K --> L[Push to CDN Origin] L --> M[Update Metadata DB<br/>Status = Ready]
3.1.3 Tai sao dung DAG?
| Ly do | Giai thich |
|---|---|
| Parallelism | Moi segment duoc encode doc lap → chay song song tren nhieu worker |
| Flexibility | De dang them/bot resolution, codec, buoc xu ly moi |
| Fault tolerance | Neu encode segment 5 loi → chi retry segment 5, khong phai lam lai tu dau |
| Priority | Encode 720p truoc (pho bien nhat) → user xem duoc som hon |
| Resource efficiency | Moi task co resource requirement khac nhau → schedule thong minh |
3.1.4 Video Codecs — H.264 vs VP9 vs AV1
| Codec | Uu diem | Nhuoc diem | Dung khi nao |
|---|---|---|---|
| H.264 (AVC) | Ho tro 99% thiet bi, decode nhanh | File lon hon | Default cho moi video |
| VP9 | Nho hon H.264 ~30%, free (Google) | Encode cham hon 10x | Chrome, Android, YouTube web |
| AV1 | Nho hon VP9 ~20%, free | Encode cuc cham (50-100x H.264) | Content pho bien, tiet kiem bandwidth |
| HEVC (H.265) | Nho hon H.264 ~40% | License fee dat, ho tro thiet bi han che | Apple ecosystem, Smart TV |
Chien luoc cua YouTube thuc te: Encode H.264 cho tat ca video (compatibility). Voi video popular (nhieu view), encode them VP9 va AV1 de tiet kiem bandwidth CDN — vi chi phi encode 1 lan nhung tiet kiem bandwidth cho hang trieu luot xem.
3.1.5 Transcoding Queue va Priority
Khong phai moi video deu duoc transcode nhu nhau:
| Priority | Doi tuong | Xu ly |
|---|---|---|
| P0 — Critical | Paid creators (YouTube Partner) | Encode ngay, dedicated workers |
| P1 — High | Video tu channel lon (>1M subs) | Queue rieng, timeout ngan |
| P2 — Normal | User thuong | Queue chung, best-effort |
| P3 — Low | Re-encode video cu sang codec moi | Chi chay luc off-peak (2AM-6AM) |
Implement bang multiple message queues voi priority khac nhau:
graph LR A[Upload Event] --> B{Priority<br/>Router} B -->|P0| C[Critical Queue<br/>Dedicated Workers] B -->|P1| D[High Queue<br/>Auto-scaling Workers] B -->|P2| E[Normal Queue<br/>Shared Workers] B -->|P3| F[Low Queue<br/>Spot Instances Only] C --> G[Transcoding Worker Pool] D --> G E --> G F --> G G --> H[Transcoded Storage]
Cost optimization: P3 jobs (re-encode video cu) chi chay tren spot instances (re hon 70-90% so voi on-demand). Neu spot bi recall → job tu dong quay lai queue, doi spot instance moi.
3.2 Video Streaming — Adaptive Bitrate
3.2.1 Streaming Protocol: HLS va DASH
Khi user nhan “Play,” dieu gi xay ra?
Buoc 1: Client request manifest file (playlist)
- HLS: file
.m3u8 - DASH: file
.mpd
Buoc 2: Manifest file chua danh sach tat ca resolution co san va URL cua tung segment:
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
360p/segment_001.ts
360p/segment_002.ts
...
#EXT-X-STREAM-INF:BANDWIDTH=2400000,RESOLUTION=1280x720
720p/segment_001.ts
720p/segment_002.ts
...
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080
1080p/segment_001.ts
1080p/segment_002.ts
...
Buoc 3: Client do bandwidth hien tai → chon resolution phu hop
Buoc 4: Client tai tung segment (thuong 2-10 giay/segment)
Buoc 5: Neu bandwidth thay doi (vao thang may, chuyen tu WiFi sang 4G) → client tu dong chuyen resolution cho segment tiep theo
Day la Adaptive Bitrate Streaming (ABR) — Key cua trai nghiem xem video muot. Thay vi buffer va cho, player giam resolution de video tiep tuc chay.
3.2.2 So sanh HLS va DASH
| Tieu chi | HLS (Apple) | DASH (MPEG) |
|---|---|---|
| Developer | Apple | MPEG consortium (open standard) |
| Container | .ts (Transport Stream) | .mp4 fragments |
| Manifest | .m3u8 | .mpd (XML) |
| DRM support | FairPlay | Widevine, PlayReady |
| Browser support | Safari native, others via JS | Chrome, Firefox, Edge |
| Latency | 10-30s (can giam voi Low-Latency HLS) | 3-10s |
| YouTube dung? | Co (cho iOS/Safari) | Co (cho Android/Chrome) |
Thuc te: YouTube dung ca hai — HLS cho Apple ecosystem, DASH cho phan con lai. Player tu dong chon protocol phu hop.
3.2.3 Adaptive Bitrate Flow
sequenceDiagram participant User as User/Player participant CDN as CDN Edge participant Origin as Origin/Storage User->>CDN: GET /video/abc123/manifest.m3u8 CDN-->>User: Manifest (list of resolutions + segment URLs) Note over User: Bandwidth = 5 Mbps → Chon 1080p User->>CDN: GET /video/abc123/1080p/seg_001.ts CDN-->>User: Segment 1 (1080p) User->>CDN: GET /video/abc123/1080p/seg_002.ts CDN-->>User: Segment 2 (1080p) Note over User: Bandwidth giam xuong 1.5 Mbps<br/>(vao thang may) User->>CDN: GET /video/abc123/480p/seg_003.ts CDN-->>User: Segment 3 (480p) ← Tu dong ha resolution User->>CDN: GET /video/abc123/480p/seg_004.ts CDN-->>User: Segment 4 (480p) Note over User: Bandwidth phuc hoi 8 Mbps<br/>(ra khoi thang may) User->>CDN: GET /video/abc123/1080p/seg_005.ts CDN-->>User: Segment 5 (1080p) ← Tu dong tang resolution Note over User: Pre-fetch: Player tai truoc<br/>2-3 segments tiep theo
3.2.4 Pre-fetching Strategy
Player thong minh khong chi tai segment hien tai ma con tai truoc (pre-fetch) cac segment tiep theo:
| Strategy | Mo ta | Khi nao dung |
|---|---|---|
| Eager pre-fetch | Tai truoc 3-5 segments | WiFi/unlimited data, user dang xem lien tuc |
| Conservative pre-fetch | Tai truoc 1-2 segments | Mobile data, user hay skip |
| No pre-fetch | Chi tai segment hien tai | Bandwidth cuc thap, user chua nhan Play |
| Quality pre-fetch | Tai truoc resolution thap, replace bang resolution cao khi ranh | Bandwidth bien dong |
Aha Moment: Pre-fetching la ly do ban thay thanh buffer chay truoc vi tri dang xem tren YouTube. No giup video tiep tuc chay ngay ca khi mang cham dot ngot trong 2-3 giay.
3.3 CDN Architecture — Da tang (Multi-tier)
3.3.1 CDN la gi va tai sao la chi phi #1?
CDN (Content Delivery Network) la mang luoi server phan tan tren toan cau, cache noi dung gan user nhat co the → Tuan-03-Networking-DNS-CDN.
Voi video streaming, CDN bandwidth chinh la chi phi lon nhat:
| Hang muc chi phi | % tong chi phi (uoc tinh) | Ghi chu |
|---|---|---|
| CDN bandwidth | 50-70% | Chi phi #1, khong the tranh |
| Storage (S3/GCS) | 10-15% | Video goc + transcoded |
| Transcoding compute | 10-15% | CPU-intensive |
| Metadata infra (DB, cache) | 5-10% | Tuong doi re |
| Network (non-CDN) | 3-5% | Inter-region transfer |
| Other (monitoring, etc.) | 2-5% | — |
3.3.2 CDN Multi-tier Architecture
graph TD subgraph "Tier 1 — Edge (PoP)" E1[Edge HCM<br/>Cache: Hot content<br/>TTL: 24h] E2[Edge Ha Noi<br/>Cache: Hot content<br/>TTL: 24h] E3[Edge Bangkok<br/>Cache: Hot content<br/>TTL: 24h] E4[Edge Tokyo<br/>Cache: Hot content<br/>TTL: 24h] E5[Edge SF<br/>Cache: Hot content<br/>TTL: 24h] end subgraph "Tier 2 — Regional" R1[Regional Singapore<br/>Cache: Warm content<br/>TTL: 7 days] R2[Regional US-West<br/>Cache: Warm content<br/>TTL: 7 days] R3[Regional EU-West<br/>Cache: Warm content<br/>TTL: 7 days] end subgraph "Tier 3 — Origin" O1[Origin Storage<br/>US-East<br/>All content] O2[Origin Storage<br/>EU-Central<br/>All content] end E1 & E2 & E3 -->|Cache miss| R1 E4 -->|Cache miss| R1 E5 -->|Cache miss| R2 R1 -->|Cache miss| O1 R2 -->|Cache miss| O1 R3 -->|Cache miss| O2 O1 <-->|Cross-region<br/>replication| O2
3.3.3 Push vs Pull CDN
| Kieu | Cach hoat dong | Uu diem | Nhuoc diem | Dung cho |
|---|---|---|---|---|
| Push | Server chu dong day content len CDN truoc khi co request | Luot xem dau tien cung nhanh (da co cache) | Ton storage, day noi dung khong ai xem | Video trending, popular creators |
| Pull | CDN chi fetch content khi co request dau tien (cache miss) | Tiet kiem storage, chi cache content thuc su can | Request dau tien cham (phai fetch tu origin) | Long-tail content (video it view) |
Chien luoc ket hop: Video moi tu popular creators (>100K subs) → Push len edge ngay. Video tu user thuong → Pull khi co nguoi xem.
3.3.4 Long-tail Content Strategy
Thuc te, phan bo view cua YouTube tuan theo Power Law (quy luat luy thua):
- Top 1% video chiem ~80% tong view → day len CDN edge, cache lau
- Top 10% video chiem ~95% tong view → cache o regional CDN
- Bottom 90% video (“long-tail”) chiem ~5% tong view → serve tu origin, khong cache
| Tier | Content type | Cache policy | Chi phi/view |
|---|---|---|---|
| Edge (expensive) | Top 1% — trending, viral | Cache 24h, push proactively | Thap nhat (da cache) |
| Regional (moderate) | Top 10% — popular, recent | Cache 7 ngay, pull-based | Trung binh |
| Origin (cheapest storage) | Bottom 90% — long-tail | Khong cache tren CDN | Cao nhat (fetch tu origin) |
Aha Moment: Neu ban cache tat ca video tren CDN edge → chi phi CDN se gap 10-20 lan. Bi quyet la chi cache nhung gi thuc su duoc xem nhieu. Day la ly do YouTube chi tieu hang ty USD/nam cho CDN ma van phai toi uu lien tuc.
3.4 Metadata System
3.4.1 Video Metadata DB
| Truong | Type | Giai thich |
|---|---|---|
| video_id | UUID / Snowflake ID | Primary key, globally unique |
| title | VARCHAR(500) | Tieu de video |
| description | TEXT | Mo ta, co the dai |
| uploader_id | BIGINT (FK) | Nguoi upload |
| status | ENUM | uploading, processing, ready, failed, deleted |
| duration_seconds | INT | Thoi luong |
| file_size_bytes | BIGINT | Kich thuoc file goc |
| upload_time | TIMESTAMP | Thoi gian upload |
| available_resolutions | JSON | [“240p”, “360p”, “480p”, “720p”, “1080p”] |
| manifest_url | VARCHAR(1000) | URL toi HLS/DASH manifest |
| thumbnail_urls | JSON | Mang URL thumbnails |
| view_count | BIGINT | So luot xem (eventual consistent) |
| like_count | BIGINT | So luot thich |
| category | VARCHAR(100) | The loai |
| tags | JSON | Tags cho search |
| is_public | BOOLEAN | Video cong khai hay rieng tu |
| drm_protected | BOOLEAN | Co bao ve DRM khong |
Chon DB: MySQL voi Vitess (sharding layer) hoac CockroachDB — vi metadata can strong consistency (khong duoc hien thi video chua transcode xong).
Sharding key: video_id — phan bo deu, query chinh la theo video_id.
3.4.2 User Activity Data
| Data type | Volume | Storage | Giai thich |
|---|---|---|---|
| Watch history | Cuc lon | Cassandra / BigTable | Moi user xem ~20 video/ngay |
| Search history | Lon | Elasticsearch | Full-text search |
| Like/dislike | Lon | MySQL (counter) + Kafka | Event-driven update |
| Watch progress | Cuc lon | Redis (TTL) → Cassandra | ”Tiep tuc xem tu 15:32” |
| Recommendation data | Cuc lon | BigQuery / Data warehouse | ML pipeline input |
Luu y: View count la eventually consistent. YouTube khong dam bao view count chinh xac tuc thoi — no duoc aggregate theo batch (moi 5-10 phut) de giam write load.
3.4.3 Search Index
Video metadata duoc index vao Elasticsearch de ho tro full-text search:
| Truong duoc index | Boost weight | Giai thich |
|---|---|---|
| title | 10x | Quan trong nhat |
| description | 3x | Phu, nhung co nhieu keyword |
| tags | 5x | Creator tu gan tag |
| channel_name | 7x | Search theo kenh |
| captions/subtitles | 1x | Tim trong phu de (cuc manh) |
3.5 Error Handling & Reliability
3.5.1 Resumable Upload
| Van de | Giai phap | Chi tiet |
|---|---|---|
| Mat mang giua upload | Chunked upload + checksum | Client ghi nho chunk cuoi thanh cong, retry tu do |
| File corrupt | MD5/SHA256 checksum per chunk | Server verify tung chunk, reject neu sai |
| Upload timeout | Pre-signed URL co TTL (thuong 24h) | Qua han → client request URL moi |
| Duplicate upload | Idempotency key (hash cua file) | Neu cung file → khong upload lai |
| Server crash | Upload state luu trong durable storage | Khoi dong lai → doc state, tiep tuc |
3.5.2 Transcoding Error Handling
| Loi | Xu ly | Retry policy |
|---|---|---|
| Segment encode fail | Retry segment do (khong phai ca video) | 3 lan, exponential backoff (1s → 2s → 4s) |
| Worker crash | Task quay lai queue, worker khac pick up | Heartbeat timeout = 30s |
| Out of memory | Retry tren worker co nhieu RAM hon | Scale up worker class |
| Corrupt input | Mark video = “failed,” thong bao user | Khong retry — user can upload lai |
| Timeout (encode qua lau) | Chia segment nho hon, retry | Dynamic segment sizing |
3.5.3 Streaming Error Handling
| Van de | Giai phap |
|---|---|
| CDN edge down | DNS failover sang edge khac (automatic) |
| Segment 404 (chua cache) | CDN pull tu regional → origin |
| Bandwidth drop | ABR tu dong ha resolution |
| Video bị xoa giua luc xem | Graceful error message, goi y video khac |
| Region-specific outage | Cross-region CDN failover |
3.5.4 Pre-signed URL Security
| Buoc | Chi tiet |
|---|---|
| 1. Client request upload | Gui authentication token + file metadata |
| 2. Server validate | Kiem tra token, quota, file type allowed |
| 3. Generate pre-signed URL | URL chua: bucket, key, expiration, signature |
| 4. Client upload truc tiep | Upload thang len S3 voi pre-signed URL |
| 5. URL het han | Sau 1-24h, URL khong con su dung duoc |
| 6. Playback URL | Cung dung pre-signed URL voi TTL ngan (vd 6h) → chong share link trai phep |
3.6 Cost Optimization — Chien luoc song con
3.6.1 CDN Cost Optimization
Day la phan quan trong nhat ve mat business — vi CDN chiem 50-70% chi phi:
| Chien luoc | Tiet kiem | Chi tiet |
|---|---|---|
| Long-tail tu origin | 30-40% CDN cost | 90% video it view → serve tu origin (S3 bandwidth re hon CDN) |
| Regional CDN selection | 10-20% | Dung CDN re hon o region it traffic (VD: dung ISP CDN cho VN thay vi Akamai) |
| Codec optimization | 20-30% bandwidth | VP9/AV1 cho popular video → giam bandwidth 20-30% per view |
| Off-peak transcoding | 40-60% compute | Transcode video khong gap vao luc 2AM-6AM, dung spot instances |
| Intelligent caching | 15-25% CDN cost | Cache TTL based on popularity — hot video cache lau, cold video cache ngan |
| Peer-to-peer (P2P) | 10-30% CDN cost | Cho phep viewers share segments voi nhau (WebRTC) |
| Multi-CDN | 10-15% | Dung nhieu CDN provider, route traffic theo gia + performance |
3.6.2 Storage Cost Optimization
| Chien luoc | Chi tiet |
|---|---|
| Tiered storage | Video moi → S3 Standard. Sau 30 ngay → S3 Infrequent Access. Sau 1 nam → S3 Glacier |
| Delete old resolutions | Video >2 nam, khong ai xem → xoa resolution thap (240p, 360p), giu 720p + 1080p |
| Lazy transcoding | Video moi: chi encode 720p + 1080p truoc. 240p, 360p, 4K → chi encode khi co request |
| Deduplication | Detect video trung lap (fingerprinting) → khong luu 2 ban |
3.6.3 Transcoding Cost Optimization
| Chien luoc | Chi tiet |
|---|---|
| Spot instances | Dung AWS Spot / GCP Preemptible cho P2, P3 jobs → re hon 70-90% |
| Reserved capacity | P0, P1 jobs chay tren reserved instances → dam bao availability |
| Off-peak scheduling | P3 jobs (re-encode) chi chay 2AM-6AM khi gia compute re nhat |
| Right-sizing | Video ngan (<1 phut) → worker nho. Video dai (>1h) → worker lon voi nhieu CPU |
| Hardware acceleration | Dung GPU (NVENC) hoac dedicated hardware (AWS MediaConvert) cho encode nhanh hon 5-10x |
4. Capacity Estimation — Uoc luong chi tiet
4.1 Assumptions
| Thong so | Gia tri | Giai thich |
|---|---|---|
| DAU | 5,000,000 | 5M daily active users |
| Avg watch time/day | 30 phut | Engagement trung binh |
| Avg video bitrate (streaming) | 2.5 Mbps | Trung binh giua 480p va 1080p |
| Upload users/day | 0.1% DAU | 5,000 creators upload/day |
| Avg upload video duration | 10 phut | Trung binh |
| Avg upload video size (original) | 1.5 GB | 1080p, 10 phut |
| Transcoding expansion factor | 3x | 1 video goc → ~3x storage (nhieu resolution) |
| Video segment size | 4 giay | Cho HLS/DASH |
4.2 Storage Estimation
Upload storage moi ngay (video goc):
Transcoded storage moi ngay (nhieu resolution):
Tong storage moi ngay:
Storage 1 nam:
Chi phi storage (AWS S3 Standard ~ $0.023/GB/month):
Luu y: Con so nay chua tinh tiered storage (chuyen video cu sang S3 IA/Glacier se giam 50-70%).
4.3 Bandwidth Estimation — Streaming
Concurrent viewers (peak):
Bandwidth peak:
Aha Moment: 781 Gbps la con so khong lo. Day la ly do YouTube can mang luoi CDN toan cau voi hang ngan edge server — khong mot data center don le nao co the serve luong bandwidth nay.
Bandwidth trung binh:
Data transfer moi ngay:
Chi phi CDN bandwidth (AWS CloudFront ~ $0.02/GB o scale lon):
Day la chi phi CDN cho 5M DAU. YouTube voi 2 ty DAU → nhan len ~400x (nhung duoc discount volume). Day la ly do CDN cost la #1 expense.
4.4 Transcoding Compute Estimation
So video can transcode moi ngay:
Trung binh moi video can encode ra 6 resolution × 2 codec = 12 variants:
Thoi gian encode trung binh 1 variant (10 phut video):
Tong compute time (gia dinh 70% H.264, 30% VP9):
So workers can (moi worker 1 vCPU, chay 20h/day):
Chi phi (AWS c5.xlarge spot ~ $0.05/h):
4.5 Tong hop chi phi
| Hang muc | Chi phi/thang | % |
|---|---|---|
| CDN bandwidth | $1,700,000 | 72% |
| Storage (S3) | $253,000 | 11% |
| Transcoding compute | $42,750 | 2% |
| Metadata infra (DB, cache, search) | $150,000 | 6% |
| Network (non-CDN) | $120,000 | 5% |
| Monitoring, ops, other | $100,000 | 4% |
| Tong | ~$2,365,750 | 100% |
Validation: CDN chiem 72% — dung voi industry benchmark (50-70%). Con so nay xac nhan rang CDN cost optimization la uu tien #1.
5. Security — Bao mat Video Platform
5.1 DRM (Digital Rights Management)
DRM bao ve noi dung co ban quyen (phim, show, music video) khoi bi sao chep trai phep.
| DRM System | Platform | Su dung boi |
|---|---|---|
| Widevine (Google) | Chrome, Android, Chromecast | YouTube, Netflix, Disney+ |
| FairPlay (Apple) | Safari, iOS, Apple TV | YouTube (tren iOS), Apple TV+ |
| PlayReady (Microsoft) | Edge, Xbox, Windows | YouTube (tren Edge), Netflix |
Cach DRM hoat dong:
| Buoc | Chi tiet |
|---|---|
| 1. Encrypt video | Moi segment duoc encrypt bang AES-128 key |
| 2. License server | Key duoc luu tren License Server (khong gui cung video) |
| 3. Client request license | Player gui device info + authentication → License Server |
| 4. License Server validate | Kiem tra: user co quyen xem? Device hop le? Region cho phep? |
| 5. Tra ve license | License chua decryption key, co TTL (vd 24h) |
| 6. Client decrypt & play | Player decrypt segments in-memory, khong luu file giai ma |
Luu y: DRM khong phai bulletproof — luon co cach bypass (screen recording). Nhung no tang chi phi cho piracy, du de bao ve cho da so use case.
5.2 Content Copyright Detection (Content ID)
YouTube dung he thong Content ID de phat hien vi pham ban quyen:
| Buoc | Chi tiet |
|---|---|
| 1. Fingerprinting | Moi video duoc tao “fingerprint” (audio + visual) khi upload |
| 2. Database matching | Fingerprint duoc so sanh voi database cua copyright holders |
| 3. Match found | Neu match → tu dong: block video, cho chay nhung chia doanh thu, hoac chi track |
| 4. Appeal process | Uploader co the khieu nai (dispute) neu la fair use |
| Ky thuat | Giai thich |
|---|---|
| Audio fingerprinting | Chromaprint / Shazam-like — trích xuat melody, rhythm, tao hash |
| Visual fingerprinting | Perceptual hashing — so sanh frame-by-frame, chiu duoc crop/resize |
| Temporal matching | Khong chi match toan bo video ma con match 1 doan (vd: 30 giay nhac nen) |
5.3 Abuse Prevention
| Loai abuse | Giai phap |
|---|---|
| Illegal content | ML-based scanning (CSAM detection, violence detection) truoc khi publish |
| Spam upload | Rate limiting per user, CAPTCHA cho upload, file type validation |
| View botting | Anomaly detection: IP clustering, behavioral analysis, khong dem view tu bot |
| DDoS | CDN-level protection (Cloudflare, AWS Shield), rate limiting → Tuan-09-Rate-Limiter |
| Account takeover | 2FA, suspicious login detection, session management |
5.4 Pre-signed URL cho Upload va Playback
| Use case | TTL | Giai thich |
|---|---|---|
| Upload URL | 1-24h | Du thoi gian upload file lon, nhung khong qua lau de bi leak |
| Playback manifest URL | 6-12h | Du cho 1 session xem, het han → client request moi |
| Playback segment URL | 1-6h | Ngan hon manifest, vi segment URL duoc refresh lien tuc |
| Thumbnail URL | 30 ngay | Thumbnail cong khai, khong can bao mat cao |
5.5 Watermarking cho Leak Tracking
| Loai watermark | Chi tiet | Dung khi |
|---|---|---|
| Visible watermark | Logo/text hien thi tren video | Ngan sao chep re-upload |
| Invisible watermark | Embed thong tin user vao pixel data (khong nhin thay) | Tracking leak source |
| Forensic watermark | Moi user nhan version video hoi khac nhau | Xac dinh chinh xac ai leak |
Forensic watermarking: Netflix dung ky thuat nay — moi subscriber nhan video voi watermark khac nhau (invisible). Neu video bi leak, phân tich watermark → biet chinh xac account nao leak.
6. DevOps & Monitoring — Van hanh Video Platform
6.1 Transcoding Pipeline Monitoring
| Metric | Mo ta | Alert threshold |
|---|---|---|
| Queue depth | So job dang cho trong transcoding queue | > 10,000 jobs → scale up workers |
| Processing time p99 | Thoi gian encode 1 video (p99) | > 2h cho video 10 phut → investigate |
| Failure rate | % job bi fail | > 2% → alert, > 5% → page on-call |
| Worker utilization | CPU usage cua transcoding workers | < 30% → scale down, > 80% → scale up |
| Queue wait time | Thoi gian job cho trong queue truoc khi duoc pick up | P0 > 1 phut → alert ngay |
6.2 CDN Performance Monitoring
| Metric | Mo ta | Target |
|---|---|---|
| Cache hit ratio | % request duoc serve tu cache (khong can fetch origin) | > 95% cho edge, > 85% cho regional |
| Origin offload | % traffic KHONG phai di ve origin | > 90% |
| Edge latency (TTFB) | Time to First Byte tu CDN edge | < 50ms |
| Cache fill rate | Toc do content duoc cache lan dau | Monitor de detect cache stampede |
| Bandwidth per PoP | Bandwidth tai moi CDN edge | Dung de capacity planning |
| Error rate per PoP | % request loi tai moi edge | > 0.1% → investigate edge health |
6.3 Streaming Quality Monitoring
| Metric | Mo ta | Target | Anh huong UX |
|---|---|---|---|
| Buffering ratio | % thoi gian user phai doi buffer | < 1% | Cao nhat — user roi di neu buffer nhieu |
| Video Start Time (VST) | Thoi gian tu nhan Play → frame dau tien | < 2s | User mat kien nhan sau 3s |
| Rebuffer frequency | So lan buffer/gio xem | < 0.5 lan/gio | Moi lan buffer = trai nghiem xau |
| Resolution distribution | % user xem o moi resolution | Trend tracking | Giam 1080p → mang co van de |
| Bitrate switches | So lan player doi resolution/phut | < 2 lan/phut | Nhieu lan doi = mang khong on dinh |
| Playback failure rate | % video khong play duoc | < 0.1% | User khong xem duoc = worst case |
6.4 Error Rate per Region
| Region | Metric can theo doi | Hanh dong |
|---|---|---|
| Vietnam | Buffering ratio, CDN latency | Neu cao → them edge o VN hoac dung ISP peering |
| SEA | Cache hit ratio | Neu thap → tang cache capacity o Singapore |
| US/EU | Playback failure rate | Neu cao → check CDN provider health |
| Emerging markets | Resolution distribution | Neu da so 240p/360p → toi uu cho low bandwidth |
6.5 Cost Dashboard
| Dashboard | Metric | Update frequency |
|---|---|---|
| CDN Cost per View | Tong CDN cost / tong views | Hourly |
| CDN Cost per Region | CDN cost breakdown theo region | Daily |
| Transcoding Cost per Video | Avg cost de transcode 1 video | Daily |
| Storage Growth Rate | TB added/day, projected cost in 30 days | Daily |
| Cost per DAU | Tong infra cost / DAU | Weekly |
| CDN Provider Comparison | Cost + performance cua moi CDN provider | Weekly |
6.6 Alerting Tiers
| Tier | Severity | Response time | Vi du |
|---|---|---|---|
| P0 — Critical | Toan bo streaming down | 5 phut | CDN origin unreachable, DB master down |
| P1 — Major | 1 region bi anh huong | 15 phut | Edge HCM down, transcoding queue stuck |
| P2 — Minor | Performance degrade | 1 gio | Cache hit ratio < 90%, encode time tang 2x |
| P3 — Warning | Anomaly detected | Next business day | Cost spike, unusual traffic pattern |
7. Mermaid Diagrams — Tong hop
7.1 Upload Pipeline — End to End
graph TD A[Client] -->|1. Request pre-signed URL| B[API Server] B -->|2. Validate auth + quota| B B -->|3. Return pre-signed URL| A A -->|4. Upload chunks truc tiep| C[Blob Storage<br/>S3/GCS] C -->|5. All chunks received<br/>Trigger event| D[Message Queue<br/>Kafka] D -->|6. Dispatch| E{Priority Router} E -->|P0| F[Critical Queue] E -->|P1| G[High Queue] E -->|P2| H[Normal Queue] E -->|P3| I[Low Queue] F & G & H & I --> J[DAG Scheduler] J --> K[Video Splitting] K --> L[Parallel Encoding<br/>H.264 / VP9 / AV1<br/>Multiple resolutions] K --> M[Audio Extraction<br/>AAC / Opus] K --> N[Thumbnail Generation] K --> O[Watermark Overlay] L --> P[Segment Merging<br/>Per resolution/codec] P --> Q[Manifest Generation<br/>HLS .m3u8 / DASH .mpd] M --> Q Q --> R[Transcoded Storage<br/>S3/GCS] R --> S[CDN Origin Push] S --> T[CDN Edge Distribution] Q --> U[Metadata DB Update<br/>status = ready] U --> V[Notification Service<br/>Video san sang!] V --> A
7.2 Streaming Architecture — Full Flow
sequenceDiagram participant Client as Client (Player) participant DNS as DNS / GSLB participant Edge as CDN Edge participant Regional as CDN Regional participant Origin as CDN Origin participant Storage as Blob Storage participant Meta as Metadata Service participant Cache as Redis Cache Client->>Meta: GET /api/video/abc123 (video info) Meta->>Cache: Lookup video metadata Cache-->>Meta: Cache hit ✓ Meta-->>Client: Video info + manifest URL Client->>DNS: Resolve CDN hostname DNS-->>Client: Nearest edge IP (GSLB) Client->>Edge: GET manifest.m3u8 Edge-->>Client: Manifest (resolution list) loop Moi segment (2-10s video) Client->>Edge: GET /1080p/seg_N.ts alt Cache HIT Edge-->>Client: Segment (from cache) else Cache MISS Edge->>Regional: Fetch segment alt Regional cache HIT Regional-->>Edge: Segment else Regional cache MISS Regional->>Origin: Fetch segment Origin->>Storage: Read from blob Storage-->>Origin: Raw segment Origin-->>Regional: Segment (cached) end Regional-->>Edge: Segment (cached) Edge-->>Client: Segment (cached for next user) end end Note over Client: ABR: neu bandwidth giam<br/>→ chuyen sang resolution thap<br/>cho segment tiep theo
7.3 Adaptive Bitrate Decision Flow
graph TD A[Player bat dau] --> B[Request manifest file] B --> C[Nhan danh sach resolutions<br/>240p / 360p / 480p / 720p / 1080p / 4K] C --> D[Do bandwidth hien tai] D --> E{Bandwidth level?} E -->|"> 8 Mbps"| F[Chon 4K<br/>~15 Mbps bitrate] E -->|"5-8 Mbps"| G[Chon 1080p<br/>~5 Mbps bitrate] E -->|"2.5-5 Mbps"| H[Chon 720p<br/>~2.5 Mbps bitrate] E -->|"1-2.5 Mbps"| I[Chon 480p<br/>~1 Mbps bitrate] E -->|"0.5-1 Mbps"| J[Chon 360p<br/>~0.5 Mbps bitrate] E -->|"< 0.5 Mbps"| K[Chon 240p<br/>~0.25 Mbps bitrate] F & G & H & I & J & K --> L[Tai segment N voi resolution da chon] L --> M[Play segment N] M --> N{Tai segment N+1?} N --> O[Do lai bandwidth] O --> P{Bandwidth thay doi?} P -->|"Giam > 20%"| Q[Ha resolution<br/>cho segment N+1] P -->|"Tang > 30%"| R[Tang resolution<br/>cho segment N+1] P -->|"On dinh"| S[Giu nguyen resolution] Q & R & S --> T[Pre-fetch 2-3 segments tiep] T --> L
7.4 CDN Multi-tier Decision Flow
graph TD A[User request video segment] --> B[DNS/GSLB<br/>Route to nearest Edge] B --> C{Edge cache<br/>co segment?} C -->|HIT| D[Serve tu Edge<br/>Latency: 5-20ms] C -->|MISS| E{Regional cache<br/>co segment?} E -->|HIT| F[Serve tu Regional<br/>Cache len Edge<br/>Latency: 20-50ms] E -->|MISS| G{Origin cache<br/>co segment?} G -->|HIT| H[Serve tu Origin<br/>Cache len Regional + Edge<br/>Latency: 50-200ms] G -->|MISS| I[Fetch tu Blob Storage<br/>Cache len Origin + Regional + Edge<br/>Latency: 100-500ms] D --> J[User xem video] F --> J H --> J I --> J J --> K{Video popular?} K -->|"Top 1%"| L[Proactive push<br/>to all edges] K -->|"Top 10%"| M[Cache dai han<br/>o regional] K -->|"Bottom 90%"| N[Ngan cache TTL<br/>hoac khong cache]
8. Aha Moments & Pitfalls
8.1 Aha Moments — Nhung dieu bat ngo
| # | Aha Moment | Giai thich |
|---|---|---|
| 1 | CDN cost la #1 expense | Khong phai server, khong phai storage — CDN bandwidth chiem 50-70% tong chi phi. Moi quyet dinh thiet ke phai xoay quanh viec giam CDN cost. |
| 2 | Video khong duoc “gui” — no duoc “cat thanh lat mong va dut” | Streaming = gui hang ngan segment nho, moi segment 2-10 giay. Player quyет dinh resolution cho tung segment. Khong phai download toan bo roi play. |
| 3 | Adaptive Bitrate la key cua UX | Khong co ABR, user phai tu chon resolution. Chon sai → buffer. ABR tu dong dieu chinh → video luon chay, chi doi chat luong. Day la ly do YouTube hiém khi buffer. |
| 4 | Long-tail strategy quyet dinh loi nhuan | 90% video chi co 5% view. Neu cache tat ca tren CDN → pha san. Bi quyet: chi cache 10% popular content, 90% con lai serve tu origin (re hon 10x). |
| 5 | Transcoding la CPU-heavy, phu hop spot instances | Transcoding la batch job, co the retry. Spot instances re hon 70-90% → tiet kiem hang trieu USD/nam. Chap nhan duoc vi transcoding la async, khong anh huong user truc tiep. |
| 6 | Encode 1 lan, serve trieu lan | Chi phi encode VP9/AV1 gap 10-50x H.264, nhung tiet kiem 20-30% bandwidth moi lan xem. Voi video 10 trieu view → tiet kiem khong lo. |
| 7 | DAG architecture cho transcoding | Khong phai “1 job = 1 video.” 1 video = nhieu task doc lap (split, encode, merge, thumbnail). DAG cho phep parallel, retry tung task, va priority scheduling. |
| 8 | Pre-signed URL la must-have | Upload: tranh API server lam bottleneck. Playback: kiem soat ai duoc xem, bao lau, chong share link trai phep. |
8.2 Pitfalls — Nhung loi thuong gap trong interview
| # | Pitfall | Giai thich | Cach tranh |
|---|---|---|---|
| 1 | Khong tach Upload va Streaming | Hai luong nay co yeu cau hoan toan khac nhau. Thiet ke chung → khong toi uu duoc. | Luon ve 2 pipeline rieng biet |
| 2 | Quen CDN cost | Noi “dung CDN” ma khong ban ve cost optimization → interviewer nghi ban khong hieu scale thuc te | Luon de cap long-tail strategy, multi-CDN, codec optimization |
| 3 | Chi noi “transcode video” | Khong giai thich DAG, parallel processing, priority queue → interviewer nghi ban chi biet surface level | Mo ta DAG: split → encode tung segment → merge. Noi ve priority va spot instances |
| 4 | Quen Adaptive Bitrate | Noi “stream video cho user” ma khong noi ve ABR → interviewer se hoi “neu mang cham thi sao?” | Luon noi ve HLS/DASH, ABR, segment-based streaming |
| 5 | Khong noi ve resumable upload | Voi file 2-50GB, upload thuong bi gian doan. Khong co resumable = UX te | Noi ve chunked upload, checksum, retry |
| 6 | Thiet ke monolithic | Gop tat ca vao 1 service → khong scale duoc tung phan | Upload service, transcoding service, streaming (CDN), metadata service — tach rieng |
| 7 | Quen error handling | ”Video upload xong, transcode xong, xem duoc” — qua ly tuong. Thuc te: network fail, disk full, codec error | Noi ve retry, exponential backoff, dead letter queue, alerting |
8.3 Interview Tips — Chien luoc trinh bay
| Buoc | Thoi gian | Noi gi |
|---|---|---|
| 1. Clarify | 3-5 phut | Hoi: upload + streaming? Scale? Resolution? Paid vs free? |
| 2. High-level | 5-7 phut | Ve 2 pipeline: Upload (pre-signed URL → storage → transcode → CDN) + Streaming (CDN → ABR) |
| 3. Deep dive | 15-20 phut | Chon 2-3 topic de deep dive: transcoding DAG, CDN multi-tier, ABR. Khong co lam tat ca |
| 4. Cost & Scale | 5 phut | Back-of-envelope cho storage + bandwidth. Nhan manh CDN cost la #1 |
| 5. Wrap up | 3 phut | Noi ve DRM, error handling, monitoring. Goi y extensions: live streaming, recommendation |
Pro tip: Khi interviewer hoi “Design YouTube,” ho khong mong doi ban thiet ke tat ca. Ho muon thay ban chon dung van de de deep dive va giai thich ro rang tai sao.
9. Wrap Up — Step 4: Extensions & Trade-offs
9.1 Possible Extensions (neu co thoi gian)
| Extension | Mo ta ngán |
|---|---|
| Live Streaming | RTMP ingest → transcode real-time → HLS/DASH out. Khac VOD: latency quan trong, khong co pre-transcode |
| Recommendation Engine | Collaborative filtering + content-based. Input: watch history, like, search. Output: suggested videos |
| Comment System | Threaded comments, real-time update, spam detection. Sharding by video_id |
| Analytics Dashboard | Creator analytics: views, watch time, demographics. Dung data warehouse (BigQuery) |
| Multi-language | Auto-generated captions (Speech-to-Text), translation, subtitle management |
| Monetization | Ad insertion (pre-roll, mid-roll), subscription tiers, super chat |
| Offline Viewing | Download video cho xem offline. DRM van phai hoat dong offline (license pre-fetched) |
9.2 Trade-offs Summary
| Quyet dinh | Option A | Option B | YouTube chon |
|---|---|---|---|
| Transcoding timing | Eager (encode tat ca resolution ngay) | Lazy (encode khi co request) | Eager cho popular codecs, Lazy cho rare resolutions |
| CDN strategy | Push (day len CDN truoc) | Pull (CDN fetch khi can) | Push cho popular, Pull cho long-tail |
| Storage | 1 region | Multi-region | Multi-region voi cross-region replication |
| Codec priority | Chi H.264 (nhanh, compatible) | Multi-codec (H.264 + VP9 + AV1) | Multi-codec — tiet kiem bandwidth cho popular content |
| Upload | Qua API server | Pre-signed URL truc tiep len storage | Pre-signed URL — giam tai API server |
| Metadata consistency | Strong consistency | Eventual consistency | Strong cho video status, Eventual cho view count |
| Queue architecture | 1 queue chung | Multi-queue voi priority | Multi-queue — P0/P1/P2/P3 rieng biet |
9.3 Lien ket noi bo (Internal Links)
| Topic | Link | Lien quan |
|---|---|---|
| CDN va DNS | Tuan-03-Networking-DNS-CDN | CDN multi-tier, DNS-based routing, GSLB |
| Cache Strategy | Tuan-06-Cache-Strategy | CDN cache policy, Redis metadata cache, cache invalidation |
| Message Queue | Tuan-08-Message-Queue | Transcoding queue, priority queue, event-driven architecture |
| Database Sharding | Tuan-07-Database-Sharding-Replication | Metadata DB sharding by video_id, replication cho read |
| Rate Limiter | Tuan-09-Rate-Limiter | Upload rate limiting, API protection |
| Monitoring | Tuan-13-Monitoring-Observability | Streaming quality metrics, alerting tiers |
| Security | Tuan-15-Data-Security-Encryption | DRM, encryption, pre-signed URL |
| Back-of-envelope | Tuan-02-Back-of-the-envelope | Capacity estimation methodology |
“YouTube khong phai la 1 he thong — no la hang chuc he thong lam viec cung nhau. Upload pipeline, transcoding DAG, CDN network, metadata service, recommendation engine… Moi phan co the la 1 bai system design rieng. Bi quyet trong interview la biet focus vao dung phan quan trong nhat: CDN cost optimization va adaptive bitrate streaming.”