Video Streaming System Design (HLD & LLD)
A video streaming platform (such as YouTube, Netflix, or Amazon Prime Video) is a common system design interview topic. This document covers the architecture, components, APIs, data model, scalability considerations, and the role of metadata in video transcoding.
Functional Requirements
Section titled “Functional Requirements”Core Features
Section titled “Core Features”- Upload videos
- Process and transcode videos
- Store videos
- Stream videos
- Search videos
- Like, comment, and share
- Personalized recommendations
Non-Functional Requirements
Section titled “Non-Functional Requirements”- High scalability
- Low-latency playback
- Fault tolerance
- Global availability
- Cost-efficient storage
- Adaptive bitrate streaming
High-Level Architecture
Section titled “High-Level Architecture”flowchart TD
C[Client] --> G[API Gateway]
G --> VS[Video Service]
G --> MS[Metadata Service]
G --> US[User Service]
G --> RS[Recommendation Service]
VS --> S3[Object Storage]
VS --> Q[Message Queue]
Q --> ENC[Encoding Service]
ENC --> S3
MS --> DB[(Metadata DB)]
US --> UDB[(User DB)]
RS --> ML[ML Models]
S3 --> CDN[CDN]
CDN --> Viewer[End User]
Core Components
Section titled “Core Components”API Gateway
Section titled “API Gateway”Responsibilities:
- Authentication
- Rate limiting
- Request routing
Video Upload Service
Section titled “Video Upload Service”Flow:
Client -> Upload Service -> Object Storage -> Queue -> EncoderResponsibilities:
- Accept uploads
- Store original video
- Trigger asynchronous encoding
Encoding Service
Section titled “Encoding Service”The uploaded video is transcoded into multiple resolutions.
Example outputs:
- 240p
- 360p
- 480p
- 720p
- 1080p
Typical output files:
video_240p.mp4video_360p.mp4video_720p.mp4Storage
Section titled “Storage”Object Storage:
- Original videos
- Encoded videos
- HLS/DASH segments
Metadata Database:
video_idtitledescriptiondurationowner_idupload_timeviewsBenefits:
- Edge caching
- Reduced latency
- Lower origin load
Streaming Protocols
Section titled “Streaming Protocols”Adaptive Bitrate Streaming using:
- HLS
- MPEG-DASH
Video is split into segments:
segment1.tssegment2.tssegment3.tsThe player automatically switches quality depending on network conditions.
Upload Flow
Section titled “Upload Flow”flowchart LR
U[User Upload] --> S[Object Storage]
S --> M[Metadata Extractor]
M --> Q[Queue]
Q --> E[Encoding Service]
E --> P[HLS/DASH Packager]
P --> O[Store Encoded Variants]
O --> CDN
Steps:
- User uploads video.
- API Gateway authenticates the request.
- Video is stored in object storage.
- Upload event is sent to a queue.
- Encoding service generates multiple resolutions.
- Metadata is stored.
- CDN caches content.
- Users stream the video.
Streaming Flow
Section titled “Streaming Flow”sequenceDiagram
participant User
participant CDN
participant Origin
User->>CDN: Request master.m3u8
alt Cache Hit
CDN-->>User: Playlist
else Cache Miss
CDN->>Origin: Fetch playlist/segments
Origin-->>CDN: Response
CDN-->>User: Playlist/segments
end
Player downloads:
master.m3u8chunk1.tschunk2.tschunk3.tsDatabase Design
Section titled “Database Design”Video Table
Section titled “Video Table”Video------video_id (PK)user_idtitledescriptiondurationupload_timeviewsstatusVideoFiles Table
Section titled “VideoFiles Table”VideoFiles----------file_idvideo_idresolutionurlsizeformatUsers Table
Section titled “Users Table”Users-----user_idnameemailcreated_atComments Table
Section titled “Comments Table”Comments--------comment_idvideo_iduser_idtextcreated_atAPI Design
Section titled “API Design”Upload Video
Section titled “Upload Video”POST /videosRequest:
{ "title": "System Design", "description": "Video streaming architecture"}Get Metadata
Section titled “Get Metadata”GET /videos/{id}Get Streaming URL
Section titled “Get Streaming URL”GET /videos/{id}/streamResponse:
{ "stream_url": "cdn.com/video123/master.m3u8"}Like Video
Section titled “Like Video”POST /videos/{id}/likeLow-Level Design
Section titled “Low-Level Design”Video Entity
Section titled “Video Entity”class Video { String videoId; String title; String description; String userId; long duration; long views;}Video Service
Section titled “Video Service”class VideoService {
VideoRepository repository;
public Video upload(VideoUploadRequest req) { Video video = new Video(); video.setTitle(req.getTitle()); video.setDescription(req.getDescription());
repository.save(video); return video; }}Streaming Service
Section titled “Streaming Service”class StreamingService {
public String getStreamingUrl(String videoId) { return cdnService.getPlaylist(videoId); }}Scalability Considerations
Section titled “Scalability Considerations”| Challenge | Solution |
|---|---|
| Storage explosion | Compression and object storage |
| Upload spikes | Pre-signed uploads |
| Hot videos | CDN edge caching |
| CPU-intensive encoding | Distributed worker queues |
| Metadata lookups | Redis caching |
Additional optimizations:
- Kafka/RabbitMQ for asynchronous processing
- Redis for metadata and counters
- Elasticsearch for search
- Batch updates for view counts
Advanced Topics
Section titled “Advanced Topics”Live Streaming
Section titled “Live Streaming”Pipeline:
RTMP Ingest -> Encoder -> HLS/DASH Packaging -> CDNRecommendation System
Section titled “Recommendation System”Inputs:
- Watch history
- Clicks
- Watch duration
View Counting
Section titled “View Counting”Use Redis counters and periodically flush aggregated counts to the database.
Role of Metadata in Resolution Generation
Section titled “Role of Metadata in Resolution Generation”Video metadata is only partially responsible for preparing different resolutions.
1. Source Video Metadata (Used by the Encoder)
Section titled “1. Source Video Metadata (Used by the Encoder)”Technical metadata extracted from the uploaded file:
{ "width": 3840, "height": 2160, "duration": 3600, "bitrate": 25000000, "codec": "h264", "fps": 30}This metadata determines:
- Supported output resolutions
- Bitrate ladder
- Codec compatibility
- Whether transcoding is required
Example:
| Source Resolution | Generated Variants |
|---|---|
| 4K | 2160p, 1440p, 1080p, 720p, 480p |
| 1080p | 1080p, 720p, 480p, 360p |
| 720p | 720p, 480p, 360p |
| 480p | 480p, 360p |
A 720p source cannot produce a true 1080p stream.
2. Application Metadata (Not Used for Transcoding)
Section titled “2. Application Metadata (Not Used for Transcoding)”Typical application metadata:
{ "videoId": "123", "title": "System Design Tutorial", "description": "Video Streaming Architecture", "uploadedBy": "user1", "views": 1000}Used for:
- Search
- Recommendations
- UI display
- Analytics
- User feeds
Encoding Decision Flow
Section titled “Encoding Decision Flow”flowchart TD
U[Video Upload] --> O[Object Storage]
O --> X[Metadata Extractor]
X -->|Resolution Codec FPS| E[Encoding Service]
E --> R1[1080p]
E --> R2[720p]
E --> R3[480p]
E --> R4[360p]
R1 --> P[HLS/DASH Packager]
R2 --> P
R3 --> P
R4 --> P
Example metadata table:
VideoMetadata-------------video_idsource_widthsource_heightdurationfpscodecbitrateaspect_ratiostatusThe encoding service reads these fields to choose encoding profiles and generate adaptive streaming variants.
Interview Answer
Section titled “Interview Answer”When asked how resolutions are generated:
After upload, the system extracts technical metadata such as resolution, bitrate, codec, and frame rate from the source video. Based on predefined encoding profiles, the transcoding service generates multiple resolution variants (1080p, 720p, 480p, etc.). These variants are segmented and packaged into HLS/DASH manifests to support adaptive bitrate streaming.
Key Takeaways
Section titled “Key Takeaways”- Separate application metadata from technical media metadata.
- Technical metadata drives transcoding decisions.
- Adaptive streaming relies on multiple encoded variants and segmented media.
- Asynchronous encoding, object storage, CDN, and caching are fundamental to scalability.
- HLS/DASH packaging enables seamless quality switching during playback.