Design Youtube

Understand the problem and establish design scope

Propose high-level design and get buy-in

At the high-level, the system comprises three components:

Client: You can watch YouTube on your computer, mobile phone, and smartTV.
CDN: Videos are stored in CDN. When you press play, a video is streamed from the CDN.
API servers: Everything else except video streaming goes through API servers. This includes feed recommendation, generating video upload URL, updating metadata database and cache, user signup, etc.

Video uploading flow

Upload the actual video

  1. Videos are uploaded to the original storage.
  2. Transcoding servers fetch videos from the original storage and start transcoding.
  3. Once transcoding is complete, the following two steps are executed in parallel:
    • 3a. Transcoded videos are sent to transcoded storage.
    • 3b. Transcoding completion events are queued in the completion queue.
    • 3a.1. Transcoded videos are distributed to CDN.
    • 3b.1. Completion handler contains a bunch of workers that continuously pull event data from the queue
    • 3b.1.a. and 3b.1.b. Completion handler updates the metadata database and cache when video transcoding is complete.
  4. API servers inform the client that the video is successfully uploaded and is ready for streaming.

Update the metadata

While a file is being uploaded to the original storage, the client in parallel sends a request to update the video metadata. The request contains video metadata, including file name, size, format, etc. API servers update the metadata cache and database.

Video streaming flow

Before we discuss video streaming flow, let us look at an important concept: streaming protocol. This is a standardized way to control data transfer for video streaming. Popular streaming protocols are:

  • MPEG–DASH. MPEG stands for “Moving Picture Experts Group” and DASH stands for “Dynamic Adaptive Streaming over HTTP”
  • Apple HLS. HLS stands for “HTTP Live Streaming”.
  • Microsoft Smooth Streaming.
  • Adobe HTTP Dynamic Streaming (HDS).
    streaming protocols

Videos are streamed from CDN directly. The edge server closest to you will deliver the video.

Design deep dive

In the high-level design, the entire system is broken down in two parts: video uploading flow and video streaming flow. In this section, we will refine both flows

Video transcoding

Video transcoding is important for the following reasons:

  • Raw video consumes large amounts of storage space. An hour-long high definition video recorded at 60 frames per second can take up a few hundred GB of space.
  • Many devices and browsers only support certain types of video formats. Thus, it is important to encode a video to different formats for compatibility reasons.
  • To ensure users watch high-quality videos while maintaining smooth playback, it is a good idea to deliver higher resolution video to users who have high network bandwidth and lower resolution video to users who have low bandwidth.
  • Network conditions can change, especially on mobile devices. To ensure a video is played continuously, switching video quality automatically or manually based on network conditions is essential for smooth user experience.

Directed acyclic graph (DAG) model

Transcoding a video is computationally expensive and time-consuming. Besides, different content creators may have different video processing requirements. To support different video processing pipelines and maintain high parallelism, it is important to add some level of abstraction and let client programmers define what tasks to execute.

Facebook’s streaming video engine uses a directed acyclic graph (DAG) programming model, which defines tasks in stages so they can be executed sequentially or parallelly
Distributed Video Processing at Facebook Scale.

In our design, we adopt a similar DAG model to achieve flexibility and parallelism:

The original video is split into video, audio, and metadata. Here are some of the tasks that can be applied on a video file:

  • Inspection: Make sure videos have good quality and are not malformed.
  • Video encodings: Videos are converted to support different resolutions, codec, bitrates.
  • Thumbnail. Thumbnails can either be uploaded by a user or automatically generated by the system.
  • Watermark: An image overlay on top of your video contains identifying information about your video.

Video transcoding architecture

Preprocessor

The preprocessor has 4 responsibilities:

  1. Video splitting: Video stream is split or further split into smaller Group of Pictures (GOP) alignment. GOP is a group/chunk of frames arranged in a specific order. Each chunk is an independently playable unit, usually a few seconds in length.
  2. Some old mobile devices or browsers might not support video splitting. Preprocessor split videos by GOP alignment for old clients.
  3. DAG generation: The processor generates DAG based on configuration files client programmers write:
  4. Cache data: The preprocessor is a cache for segmented videos. For better reliability, the preprocessor stores GOPs and metadata in temporary storage. If video encoding fails, the system could use persisted data for retry operations.

DAG scheduler

The DAG scheduler splits a DAG graph into stages of tasks and puts them in the task queue in the resource manager.

Resource manager

The resource manager is responsible for managing the efficiency of resource allocation. It contains 3 queues and a task scheduler:

  • Task queue: It is a priority queue that contains tasks to be executed.
  • Worker queue: It is a priority queue that contains worker utilization info.
  • Running queue: It contains info about the currently running tasks and workers running the tasks.
  • Task scheduler: It picks the optimal task/worker, and instructs the chosen task worker to execute the job.

The resource manager works as follows:

  • The task scheduler gets the highest priority task from the task queue.
  • The task scheduler gets the optimal task worker to run the task from the worker queue.
  • The task scheduler instructs the chosen task worker to run the task.
  • The task scheduler binds the task/worker info and puts it in the running queue.
  • The task scheduler removes the job from the running queue once the job is done.

Task workers

Task workers run the tasks which are defined in the DAG. Different task workers may run different tasks

Temporary storage

Multiple storage systems are used here. The choice of storage system depends on factors like data type, data size, access frequency, data life span, etc. For instance, metadata is frequently accessed by workers, and the data size is usually small. Thus, caching metadata in memory is a good idea. For video or audio data, we put them in blob storage. Data in temporary storage is freed up once the corresponding video processing is complete.

System optimizations

Speed optimization: parallelize video uploading

Uploading a video as a whole unit is inefficient. We can split a video into smaller chunks by GOP alignment:

This allows fast resumable uploads when the previous upload failed. The job of splitting a video file by GOP can be implemented by the client to improve the upload speed.

Speed optimization: place upload centers close to users

Another way to improve the upload speed is by setting up multiple upload centers across the globe. To achieve this, we use CDN as upload centers.

Speed optimization: parallelism everywhere

Another optimization is to build a loosely coupled system and enable high parallelism.
Our design needs some modifications to achieve high parallelism. The flow of how a video is transferred from original storage to the CDN revealing that the output depends on the input of the previous step. This dependency makes parallelism difficult.
To make the system more loosely coupled, we introduced message queues:

  • Before the message queue is introduced, the encoding module must wait for the output of the download module.
  • After the message queue is introduced, the encoding module does not need to wait for the output of the download module anymore. If there are events in the message queue, the encoding module can execute those jobs in parallel.

Safefy optimization: pre-signed upload URL

To ensure only authorized users upload videos to the right location, we introduce pre-signed URLs:

  1. The client makes a HTTP request to API servers to fetch the pre-signed URL, which gives the access permission to the object identified in the URL.
  2. API servers respond with a pre-signed URL.
  3. Once the client receives the response, it uploads the video using the pre-signed URL.

Safety optimization: protect your videos

To protect copyrighted videos, we can adopt one of the following three safety options:

  • Digital rights management (DRM) systems: Three major DRM systems are Apple FairPlay, Google Widevine, and Microsoft PlayReady.
  • AES encryption: You can encrypt a video and configure an authorization policy. The encrypted video will be decrypted upon playback. This ensures that only authorized users can watch an encrypted video.
  • Visual watermarking

Cost saving optimization

Previous research shows that YouTube video streams follow long-tail distribution:
Conference on Scalability: YouTube Scalability
Understanding the characteristics of internet short video sharing: A youtube-based measurement study

  1. Only serve the most popular videos from CDN and other videos from our high capacity storage video servers
  2. Less popular content and short videos can be encoded on-demand rather than instantly storing many encoded video versions.
  3. Some videos are popular only in certain regions. There is no need to distribute these videos to other regions.
  4. Build your own CDN like Netflix and partner with Internet Service Providers (ISPs). Building your CDN is a giant project; however, this could make sense for large streaming companies. ISPs are located all around the world and are close to users. By partnering with ISPs, you can improve the viewing experience and reduce the bandwidth charges.

All those optimizations are based on content popularity, user access pattern, video size, etc. It is important to analyze historical viewing patterns before doing any optimization.
Content Popularity for Open Connect

Error handling

For a large-scale system, system errors are unavoidable. To build a highly fault-tolerant system, we must handle errors gracefully and recover from them fast. Two types of errors exist:

  • Recoverable error. For recoverable errors such as video segment fails to transcode, the general idea is to retry the operation a few times. If the task continues to fail and the system believes it is not recoverable, it returns a proper error code to the client.
  • Non-recoverable error. For non-recoverable errors such as malformed video format, the system stops the running tasks associated with the video and returns the proper error code to the client.

Typical errors for each system component are covered by the following playbook:

  • Upload error: retry a few times.
  • Split video error: if older versions of clients cannot split videos by GOP alignment, the entire video is passed to the server. The job of splitting videos is done on the server-side.
  • Transcoding error: retry.
  • Preprocessor error: regenerate DAG diagram.
  • DAG scheduler error: reschedule a task.
  • Resource manager queue down: use a replica.
  • Task worker down: retry the task on a new worker.
  • API server down: API servers are stateless so requests will be directed to a different API server.
  • Metadata cache server down: data is replicated multiple times. If one node goes down, you can still access other nodes to fetch data. We can bring up a new cache server to replace the dead one.

[id=8l7fpaj8]
[date=2026-08-06T21:37:12]
[anchor=Metadata DB server down]
—> Metadata DB server down:
- Master is down. If the master is down, promote one of the slaves to act as the new master.
- Slave is down. If a slave goes down, you can use another slave for reads and bring up another database server to replace the dead one.

Wrap up

  • Scale the database: You can talk about database replication and sharding.
  • Live streaming: It refers to the process of how a video is recorded and broadcasted in real time. Although our system is not designed specifically for live streaming, live streaming and non-live streaming have some similarities: both require uploading, encoding, and streaming. The notable differences are:
  • Live streaming has a higher latency requirement, so it might need a different streaming protocol.
  • Live streaming has a lower requirement for parallelism because small chunks of data are already processed in real-time.
  • Live streaming requires different sets of error handling. Any error handling that takes too much time is not acceptable.
  • Video takedowns: Videos that violate copyrights, pornography, or other illegal acts shall be removed. Some can be discovered by the system during the upload process, while others might be discovered through user flagging.