📌Problem Statement
Design a video streaming platform like YouTube.
📌Requirements
Functional
Scale
📌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
📌Key Components
Asked at Google, Netflix, and Amazon interviews.