Lesson 4 of 9 · 25m
Building a Transcoding Pipeline That Survives Reality
Design ingest, probing, queuing, worker isolation, validation, and retry behavior around the fact that uploads and compute both fail.
Every transcoding pipeline looks elegant in an architecture diagram and demoralizing in production. Uploads truncate, codecs lie about metadata, workers run out of disk, a single malformed file pins a queue, and the largest customer video always arrives during your smallest on-call rotation. Build for those facts from day one.
Start with an explicit state machine
Do not encode status as a pile of booleans. Use visible states:
uploaded -> probe_failed
uploaded -> queued -> leased -> probing -> transcoding -> packaging -> validating -> ready
+-> retryable_failure -> queued / permanent_failureThe distinction between retryable and permanent failure is operationally priceless. A worker disk filling is retryable. A source file with no decodable video track probably is not, no matter how many times the queue optimistically tries it.
Probe before scheduling expensive work
Use ffprobe and container-aware validators before allocating a large worker:
ffprobe -v error -print_format json -show_format -show_streams input.mp4Capture duration, resolution, rotation metadata, color primaries, frame rate, timebase, audio layout, codec profile, and edit lists. Many playback bugs begin as ignored input metadata. Rotation flags are the classic example: ffmpeg may decode frames in one orientation while the player expects display matrix metadata to be honored or burned in consistently.
Workers need isolation and budgets
Transcoding is hostile compute: high CPU, high memory spikes, high disk I/O, and arbitrary user input. Put workers in isolated instances or containers with bounded local scratch space. Set hard timeouts and maximum output sizes. Never let one job write unbounded intermediate files into the same filesystem used by every other job. A worker lifecycle should look like this:
1. Lease the job; download source to local scratch.
2. Validate checksum and probe media.
3. Transcode renditions; validate outputs.
4. Upload immutable artifacts; write structured metadata.
5. Delete scratch and heartbeat the lease.If the worker dies, the lease expires and another worker retries. If it dies while uploading, object versioning or idempotent object names prevent a half-written artifact from becoming canonical.
Validate output, not just process exit
ffmpeg exiting zero does not prove the output is playable. Run a post-check:
ffprobe -v error -show_entries format=duration,size -of json output-720.mp4
ffmpeg -v error -i output-720.mp4 -f null -Validate every expected rendition, manifest reference, duration delta, stream count, and keyframe cadence. If possible, decode a sample at the beginning, middle, and end. The end matters because truncated sources often look fine for the first ninety percent.
Queues are product controls
A single FIFO queue is simple and unfair. Large batch imports can starve interactive uploads for hours. Use priority lanes or weighted scheduling: interactive first, backfill second, re-encode third. Expose queue depth and age, not just job count; ten two-hour videos are not the same workload as ten clips.
Field notes
- Keep the exact input checksum in job metadata. Reproducing a bug requires knowing which object you actually processed.
- Do not retry forever. Poison messages belong in quarantine with diagnostic artifacts.
- Instance storage is a consumable. Alert on free disk before jobs begin failing unpredictably.
- Measure time-in-queue separately from time-in-encode. Users experience the sum, but fixes differ.
Was this lesson helpful?