Youtube Service Bug (Karat)
Difficulty: EASYCategory: HLDFrequency: 1Asked On 2025-05-01
Problem Statement
We are working on a service that generates subtitles for users' videos. This process starts a new thread for every video and is processor-intensive. Currently, this service runs as a single process on a machine. We've run into a bug where if the service is processing more than 10 videos at the same time, the service crashes the server, losing all requests currently being processed and affecting other processes on the machine. It may take a long time to find and fix this bug. What workarounds could we implement to continue running the service while we do ?
Solution
1. Limit Concurrent Video Processing
•Use a thread pool with a maximum of 10 threads.
•Enqueue incoming requests and only process up to 10 videos at a time.
•Prevents overload and ensures the service doesn’t exceed the unsafe threshold.
2. Isolate Processing via Worker Processes
•Run video processing in separate worker processes (instead of threads).
•Use a process supervisor like Supervisord or Docker containers to isolate failures.
•Crashes in one worker don’t affect the main process or others.
3. Queue-Based Decoupling
•Offload video processing requests to a message queue (e.g., RabbitMQ, AWS SQS, Kafka).
•A pool of worker machines consumes from the queue with rate limiting.
•Helps spread the load and allows retries for failed jobs.
4. Horizontal Scaling
•Run multiple instances of the service on different machines or containers.
•Each instance has its own processing limit, collectively handling more videos without crashing any one system.
5. Circuit Breaker + Health Checks
•Add a circuit breaker to reject or defer new requests when the system is under pressure.
•Implement health checks to detect unhealthy states and automatically restart affected instances.