🇮🇳
🇮🇳
Republic Day Special Offer!Get 20% OFF on all courses
Enroll Now
P
Prakalpana
📚Learn
Code Your Future
System Design⏱️ 22 min read📅 Dec 30

HLD: Design YouTube - Video Streaming Platform

AV
Arjun VermaPrincipal Engineer at YouTube
📑 Contents (11 sections)

📌Problem Statement

Design a video streaming platform like YouTube.

📌Requirements

Functional

  • Upload videos
  • Stream videos (multiple qualities)
  • Like, comment, subscribe
  • Search videos
  • Recommendations
  • Scale

  • 2B users, 500M DAU
  • 500 hours of video uploaded per minute
  • Average video: 5 minutes, 50MB
  • 1B video views per day
  • 📌Architecture Overview

    System Architecture
    Live
    Web/Mobile Client
    Load Balancer
    Global
    API Gateway
    Upload Service
    Streaming Service
    Search Service
    Recommendation Service
    Kafka
    Processing Queue
    Transcoding Workers
    FFmpeg
    S3
    Raw Videos
    S3
    Processed Videos
    Cassandra
    Metadata
    CDN
    Global Edge Servers
    Elasticsearch
    Search
    Redis
    Cache
    ML Service
    Recommendations
    ⚡ High Availability🔄 Auto-scaling🛡️ Fault Tolerant

    Video Upload Pipeline

    public class VideoUploadService {
    public UploadResponse uploadVideo(MultipartFile file, VideoMetadata metadata, String userId) {
    String videoId = generateId();
    String rawPath = s3Client.upload(file, "raw-videos/" + videoId);
    Video video = new Video(videoId, userId, metadata, VideoStatus.PROCESSING);
    videoRepository.save(video);
    kafkaTemplate.send("video-processing", new VideoUploadedEvent(videoId, rawPath));
    return new UploadResponse(videoId, "Processing started");
    }
    }
    public class VideoProcessingService {
    private static final int[] RESOLUTIONS = {360, 480, 720, 1080, 1440, 2160};
    public void processVideo(String videoId, String rawPath) {
    File rawFile = downloadFromS3(rawPath);
    for (int resolution : RESOLUTIONS) {
    String outputPath = transcodeToResolution(rawFile, resolution);
    s3Client.upload(outputPath, "processed/" + videoId + "/" + resolution + ".mp4");
    }
    String thumbnailPath = generateThumbnail(rawFile);
    s3Client.upload(thumbnailPath, "thumbnails/" + videoId + ".jpg");
    videoRepository.updateStatus(videoId, VideoStatus.READY);
    }
    }

    Video Streaming

    public class StreamingService {
    public StreamingUrl getStreamUrl(String videoId, String userId, int quality) {
    Video video = videoRepository.findById(videoId);
    int bestQuality = selectQuality(quality, getUserBandwidth(userId));
    String cdnUrl = cdnService.getSignedUrl(
    "processed/" + videoId + "/" + bestQuality + ".mp4",
    Duration.ofHours(4)
    );
    analyticsService.trackView(videoId, userId);
    return new StreamingUrl(cdnUrl, bestQuality);
    }
    }

    Recommendations

    public class RecommendationService {
    public List<Video> getRecommendations(String userId, int limit) {
    List<Video> recommendations = new ArrayList<>();
    List<String> watchHistory = userService.getWatchHistory(userId, 100);
    Map<String, Double> categoryScores = calculateCategoryPreferences(watchHistory);
    List<Video> trending = getTrendingVideos(limit / 3);
    recommendations.addAll(trending);
    List<Video> collaborative = collaborativeFilter(userId, limit / 3);
    recommendations.addAll(collaborative);
    List<Video> contentBased = contentBasedFilter(categoryScores, limit / 3);
    recommendations.addAll(contentBased);
    return rankAndDeduplicate(recommendations, limit);
    }
    }

    📌Database Design

    CREATE TABLE videos (
    video_id VARCHAR(50) PRIMARY KEY,
    user_id VARCHAR(50),
    title VARCHAR(200),
    description TEXT,
    duration_seconds INT,
    status VARCHAR(20),
    view_count BIGINT DEFAULT 0,
    like_count BIGINT DEFAULT 0,
    created_at TIMESTAMP
    );
    CREATE TABLE video_views (
    view_id BIGINT PRIMARY KEY,
    video_id VARCHAR(50),
    user_id VARCHAR(50),
    watched_duration INT,
    quality INT,
    viewed_at TIMESTAMP
    );

    📌CDN Strategy

  • 1Edge Caching: Popular videos at edge locations
  • 2Adaptive Bitrate: Switch quality based on bandwidth
  • 3Chunked Delivery: HLS/DASH protocols
  • 📌Key Components

  • 1S3: Raw and processed video storage
  • 2FFmpeg: Video transcoding
  • 3CloudFront: Global CDN
  • 4Elasticsearch: Video search
  • 5Spark: Recommendation engine
  • Asked at Google, Netflix, and Amazon interviews.

    AV

    Written by

    Arjun Verma

    Principal Engineer at YouTube

    🚀 Master System Design

    Join 500+ developers

    Explore Courses →
    Chat on WhatsApp