System Design CourseSystem Design Course1

System Design: How to Design YouTube



Introduction

YouTube is one of the most popular video-sharing platforms in the world. It allows users to upload, stream, comment on, and search for videos. Designing such a system is a classic system design interview question that tests your understanding of storage, bandwidth, scalability, and user experience.

Functional Requirements

Non-Functional Requirements

Step 1: How do users upload videos?

When a user uploads a video, the frontend sends the video file to a backend server. This server stores the file temporarily and sends it to a storage service (e.g., Amazon S3 or Google Cloud Storage).

Question: Why not directly store the video in the database?

Answer: Video files are large binary files. Storing them in a database (like MySQL or PostgreSQL) would slow it down and increase backup/restore complexity. Instead, we store metadata in the database and the actual video in a distributed object storage service.

Step 2: Video Transcoding

After uploading, the video must be transcoded into different resolutions (144p, 360p, 720p, 1080p). This is important to support various devices and internet speeds.

Transcoding is usually done using a job queue. For example:

Question: What happens if the transcoding fails?

Answer: The system should implement retries and send alerts. Failed jobs are usually logged and stored in a dead-letter queue for manual or automated reprocessing.

Step 3: Storage and Video Metadata

The actual videos are stored in object storage (like Amazon S3). Only a reference (URL or ID) is stored in the database along with metadata:

Step 4: Content Delivery via CDN

To serve videos efficiently across the globe, we use a Content Delivery Network (CDN). When a user requests a video, the system serves it from the nearest CDN edge location.

Question: What if the video is not available at a nearby CDN node?

Answer: The CDN will fetch it from the origin (e.g., main storage bucket) and cache it at the edge for future users.

Step 5: Streaming the Video

We don’t download the full video at once. Instead, we stream it in chunks using HLS (HTTP Live Streaming) or MPEG-DASH protocols. The video is split into tiny segments (e.g., 2–10 seconds long).

Step 6: Search and Recommendations

For search, we need to index the metadata (title, tags, etc.) using a search engine like Elasticsearch. For recommendations, YouTube uses collaborative filtering and machine learning models.

Example: Simple Recommendation Strategy

For beginners, we can implement a rule-based recommendation system:

Step 7: Comments, Likes, Subscriptions

These features involve separate microservices:

Question: Should these services update video metadata instantly?

Answer: No. They should write to their own databases. A separate batch job or event system can aggregate the data for analytics and update the video stats periodically.

Step 8: Scaling the System

Step 9: Handling Abuse and Security

Step 10: Database Schema Example

Let’s look at a simplified video metadata schema:


  Table: Videos
  -------------
  video_id (PK)
  uploader_id (FK)
  title
  description
  tags
  upload_time
  status
  video_url
  

Points to Remember



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M