System design case study: video streaming service
September 18, 2024Youtube is an example of a video streaming service where users upload, stream, search, and review videos. This post presents common designs for processing and hosting uploaded videos across several case studies of video streaming services for system design interviews.
Requirements
Most of the referred case studies include streaming and uploading videos as functional requirements. Because the case studies propose microservice architecture, system design intervews for video streaming services expects high availability, scalability and a loosely consistent model like BASE (Basically Available, Soft state, Eventual consistentcy). Excerpted requrirements from the case studies are below:
-
Functional requirements:
- Stream videos
- Supported clients:
- Browser
- Mobile
- Smart TV
- Supported clients:
- Upload videos
- Ability to change video quality
- Search videos
- Rate videos:
- Like
- Dislike
- Comments
- Stream videos
-
Non-functional requirements:
- High availability
- Scalability
- Reliability
Back of the Envelope Estimation
The estimation computes actual numbers around resources based on given assumptions such as:
- Number of Daily Active Users (DAU)
- Average number of videos watched by a user
- Number of videos uploaded
- Average size of uploaded videos
Architecture
Two patterns are common among these case studies. First, processing uploaded videos is divided into several tasks. Message queues are introduced to make the tasks loosely decoupled. Second, the encoded videos are hosted on a CDN.
Loosely coupled tasks
Case studies process uploaded videos in tasks that are loosely coupled by a message queues. One case study employs Kafka. Workers for a task exchange messages with other workers via queues, enabling asynchronous processing where the number can be scaled independently. Examples of divided tasks include:
- Thumbnail generator
- Content filter
- Content tagger
- Video encoder
The following figure illustrates a design where encoding and tagging videos are asyncrhonous.
The maximum size of the message that can be equeued is limited. For example, the max size of the SQS messages is 256KB, while Kafka’s is 1MB. Generally speaking, the size of a video exceeds 1MB, and videos cannot be enqueued.
The choice of message queuing system is not discussed in the case studies. Kafka is a pull-based system, and RabbitMQ is a push-based system. While Kafka generally offers better performance, Rabbit MQ supports complex routing, like handling priority messages.
If function requirements allows users to upload videos that are not enconded for video streaming, the proposed system must transcode them. FFmpeg and AWS Elemental MediaConvert can convert some video formats to HLS or DASH, which are streaming protocols.
The Content Delivery Network (CDN)
If the videos are served through CDN to reduce latency and save bandwidth, authentication, authorization, WAF should be introduced. On AWS, CloudFront can be protected with WAF. CloudFront serves static files on S3. HTTP requests to the file can be restricted by signed URLs and signed cookies. A developer guide states taht signed URLs should be used to restrict access to individual files, while signed cookies are better suited for controlling access to multiple files.