Sub topic 4
The end-to-end architecture
Prerequisites and scope
This chapter assumes you know basic web architecture (clients, APIs, databases), how HTTP caching works, and what object storage and message queues do. It builds on the requirements and capacity estimates from earlier chapters. Later chapters cover each stage in detail. Here the goal is to see the whole system, how data moves through it, and where it tends to fail.
Two paths, one catalog
A video platform is two systems joined by storage and metadata:
- The write path (also called the ingest path) takes a source file from a creator and turns it into playable media. It is asynchronous, compute-heavy, and tolerant of delay. A video that takes ten minutes to become available is acceptable.
- The read path (the playback path) delivers media to viewers. It is latency-sensitive, bandwidth-heavy, and must stay up while the write path is down or overloaded.
Keep the two paths separate. They have different scaling curves. The write path scales with uploads and is limited by CPU or GPU time. The read path scales with viewers and is limited by network egress. Uploads are far rarer than views, so the read path usually carries several orders of magnitude more traffic.
The components
Upload service
The upload service accepts the source file and records that it exists. For large files it should not proxy bytes through your API servers. A common pattern is for the API to issue a pre-signed URL (a temporary, scoped URL that lets the client write directly to object storage). The client uploads in parts, so a dropped connection resumes instead of restarting. When the upload completes, the service writes a metadata row with status uploaded and enqueues a job.
Raw object store
Source files go into an object store, separate from the delivery store. The raw store is write-once, rarely read, and worth keeping. If you later add a new codec or rendition, you re-transcode from the original, not from an already compressed copy. Re-encoding a lossy output compounds quality loss.
Job queue
The queue decouples upload from processing. It absorbs bursts, for example thousands of uploads after a popular event, and lets you scale workers on queue depth. It also gives you retries and dead-letter handling for jobs that keep failing.
Transcoding workers
Workers pull a job, read the source, and produce several renditions. Each rendition is a copy of the video at a different resolution and bitrate. A typical worker invokes an encoder such as FFmpeg. Because encoding is deterministic given the same input and settings, a failed job can be rerun safely, as long as its outputs are written idempotently (writing the same output path twice yields the same result).
Long videos are usually split into chunks that are encoded in parallel and then joined. This cuts latency for long inputs but needs care at chunk boundaries, which must fall on keyframes (frames that can be decoded without earlier frames).
Packager
The packager takes encoded streams and cuts them into short segments, typically 2 to 6 seconds, with playlists or manifests that describe them. For HLS, that means .m3u8 playlists. For DASH, it means an MPD manifest. Often the transcoder and packager are one step, as in the FFmpeg command in the Topic Spine's first example, which emits both the encoded video and the HLS playlist. They are shown as two boxes because they are two responsibilities.
Origin store
The origin store holds the final segments and manifests. It is the CDN's source of truth. Object storage works well here because segments are immutable files, which is exactly what object stores are best at.
Metadata database
The metadata database tracks videos, renditions, processing state, ownership, and the location of each manifest. It is a conventional relational or key-value store. It is not in the media path. Players never read segments through it.
Playback API
When a viewer opens a video, the player calls the playback API. The API checks access rights, looks up the manifest location, and returns a manifest URL (signed, if the content is protected). From then on, the player talks to the CDN, not to your servers.
CDN
The CDN (content delivery network) caches segments at edge servers near viewers. Most requests are served from the edge. Only cache misses reach the origin. Without the CDN, the origin would need to carry the full viewing bandwidth.
Following one video through the system
The table traces a single upload and a single view.
| Step | Component | What happens | State after |
|---|---|---|---|
| 1 | Upload service | Client uploads parts to the raw store using a pre-signed URL | `uploaded` |
| 2 | Job queue | A transcode job is enqueued with the video ID | `queued` |
| 3 | Worker | Source is encoded into 360p, 720p and 1080p renditions | `transcoding` |
| 4 | Packager | Each rendition is cut into segments, and playlists are written | `packaging` |
| 5 | Origin store | Segments and the master playlist are stored | files durable |
| 6 | Metadata DB | Manifest location is recorded | `ready` |
| 7 | Playback API | Viewer requests the video and receives the manifest URL | |
| 8 | CDN | Player fetches the master playlist and segments, mostly from cache |
The state column matters. Processing state is a small state machine, and you should store it explicitly instead of inferring it from which files exist.
The data model
Keep the model small. Three entities cover most of the design.
In practice, you often do not store every segment as a row. The rendition's playlist already lists its segments, and a video with a one-hour runtime and 4-second segments has 900 segments per rendition. Storing the playlist file and a pointer to it is cheaper than a row per segment. Store segment rows only if you need per-segment queries, such as for analytics or repair.
Choosing object key layouts
A predictable key layout makes the origin simple and cache-friendly:
videos/{video_id}/master.m3u8
videos/{video_id}/720p/index.m3u8
videos/{video_id}/720p/seg_0001.tsRelative URLs in playlists then resolve without extra lookups. Segments are immutable, so you can give them long cache lifetimes. Playlists for on-demand video also rarely change, but if you re-transcode a video, write to a new versioned prefix such as videos/{video_id}/v2/. Overwriting objects in place risks players mixing old and new segments, and edge caches may keep serving stale files.
Failure modes and design responses
| Failure | Symptom | Design response |
|---|---|---|
| Upload interrupted | Partial file | Multipart upload with resume, and cleanup of abandoned parts |
| Worker crashes mid-job | Job disappears or hangs | Queue visibility timeout, retry, idempotent outputs |
| Poison input | Job fails every time | Retry limit, dead-letter queue, `failed` state with a reason |
| Queue backlog | Videos stay in `queued` | Autoscale workers on queue depth, prioritize short or paid jobs |
| Origin overload | Slow segments, high miss latency | Higher CDN cache hit ratio, an origin shield (a mid-tier cache in front of the origin) |
| Metadata DB down | Playback API errors | Cache manifest locations, replicate the database, keep the CDN path independent |
| Edge cache stale after re-encode | Mixed-quality or broken playback | Versioned key prefixes, never overwrite |
One design rule covers many of these: the read path should depend on as little as possible. A viewer already watching needs only the CDN and the origin. If the transcoding fleet, queue, or even the metadata database is down, ongoing playback should continue.
Synchronous and asynchronous boundaries
Decide deliberately which calls block.
- Upload and "is it ready?" are synchronous API calls that return quickly.
- Everything between
queuedandreadyis asynchronous. Clients learn the result by polling a status endpoint or by receiving a notification. - Playback start is synchronous but cheap: one API call, then manifest and segment requests to the CDN.
Avoid long-running HTTP requests that wait for transcoding. They tie up connections and fail under proxies with timeouts.
Common mistakes
- Streaming playback bytes through the application servers instead of the CDN.
- Deleting the raw source after the first transcode, which blocks new codecs and renditions later.
- Inferring video state from file existence, which breaks when a job half-completes.
- Overwriting segments in place under the same key.
- Letting playback availability depend on the write path.
Exercise
A creator uploads a 2-hour movie. The transcoding fleet is down for 30 minutes. Which parts of the platform should keep working, which should not, and what state does the movie's metadata row show during the outage?
Show answer
Playback of existing videos should keep working, because the read path uses only the playback API, the metadata database, the CDN and the origin store. Uploads can also keep working, because the upload service writes to the raw store and enqueues a job. Transcoding of the new movie cannot proceed, so the movie's row stays in queued (or transcoding if a worker was mid-job, in which case the job times out and returns to the queue). When the fleet recovers, workers drain the queue and the state moves on to ready.
Exercise
Why should a re-transcoded video be written under a new versioned key prefix instead of overwriting the old segments?
Show answer
Segments are cached at CDN edges and in players. Overwriting them means a player could hold some old segments and fetch some new ones, or an edge could serve stale files, which can cause glitches or failures. A new prefix such as v2/ gives new URLs that no cache has seen, so you can switch the master playlist pointer in the metadata atomically and delete the old prefix later.