Video in progress

How do you build the upload and processing part of a platform where users upload large videos and watch them in many qualities?

The file doesn't pass through the application servers: the client uploads straight to object storage in parts (multipart) with a presigned URL. Then an event goes to a transcoding queue, workers split the video into segments and encode them in parallel, and the result is distributed through a CDN.

📖 Answer

Pushing a 2 GB file through a Spring application ties up threads, memory and network, and if the connection drops you start from zero. So:

Upload:

  1. The client does POST /uploads. The server returns a metadata record (UPLOADING) and presigned URLs for the multipart upload.
  2. The client uploads the file in parallel in 10-100 MB parts. If a part fails, it re-uploads only that one. If the app restarts, it asks which parts exist and continues.
  3. When all parts are uploaded, complete: the storage merges the parts into one object and emits an event.

Processing:

  • The event goes to the video-uploaded queue. The first step is validation: format, duration, malicious content, size limit.
  • Transcoding is the most expensive part. Split the video into segments of a few seconds (on GOP boundaries) and encode each segment into every quality (1080p, 720p, 480p...) in parallel. A 1-hour video takes hours on one machine and minutes on hundreds of workers.
  • The result is HLS/DASH segments and a manifest. Thumbnails and optional subtitles are produced at this stage too.
  • The state of each step is in the DB (PROCESSING → READY / FAILED). If a worker dies, the task returns to the queue; the steps are idempotent, with deterministic output file names.

Distribution: segments sit in object storage with a CDN in front. An origin shield layer reduces the CDN's requests to the origin. For paid content, a signed URL or token, and encrypted segments.

Cost decisions matter: spot/preemptible machines for transcoding (tasks can be restarted, which is why splitting into segments helps again), encoding the higher qualities of rarely watched videos "on demand", and moving old files to a cheaper storage class.