Sub topic 9
Storing Media Files and Metadata
This chapter assumes you know basic object storage (buckets, keys, objects), relational or key-value databases, and HTTP caching. It covers where the packaged output of the transcoding pipeline lives and how the system tracks it.
Two kinds of data, two kinds of stores
A streaming platform stores two things with very different shapes.
- Media files: large, immutable, read in whole or by byte range. Examples are source uploads, init segments and media segments.
- Metadata: small, mutable, queried by many fields. Examples are titles, owners, publish state, rendition lists and playback manifests' locations.
Media goes in object storage. Metadata goes in a database. Keeping them apart lets each scale on its own terms. Segments never need row-level updates, and metadata never needs to hold gigabytes.
Laying out media objects
Object storage has no real folders, so the key is the layout. Use a stable prefix per video and per rendition, so one prefix lists or deletes everything for a title.
sources/{video_id}/original.mp4
media/{video_id}/master.m3u8
media/{video_id}/360p/index.m3u8
media/{video_id}/360p/seg_0000.ts
media/{video_id}/720p/index.m3u8
media/{video_id}/720p/seg_0000.tsThis matches the output of the earlier ffmpeg command, which wrote 720p/index.m3u8 and 720p/seg_%04d.ts. Three design points follow from the layout:
- Keep sources and outputs separate. Sources are kept for re-transcoding with new codecs. Outputs can be regenerated, so they can use a cheaper policy.
- Treat segments as immutable. Never overwrite a segment key. If you re-encode, write under a new version prefix such as
media/{video_id}/v2/. This avoids stale copies in CDN caches. - Spread the key space. Many object stores handle high request rates per prefix. A
video_idthat is random (a UUID) distributes load better than a sequential number.
Storage tiers
Access is skewed. A small share of videos gets most of the views, and the rest are rarely played.
| Tier | Holds | Access pattern | Cost profile |
|---|---|---|---|
| Hot (standard) | Segments of recent and popular videos | Frequent reads | Higher storage, no retrieval fee |
| Infrequent access | Older renditions | Occasional reads | Lower storage, retrieval fee |
| Archive | Original sources | Almost never read | Lowest storage, slow, paid retrieval |
Lifecycle rules can move objects between tiers by age. Be careful with archive tiers for anything the player might request, because restore delays cause playback failures.
Designing the metadata model
The metadata database answers the questions the API asks: who owns this video, is it playable, and where are its files.
CREATE TABLE videos (
video_id UUID PRIMARY KEY,
owner_id UUID NOT NULL,
title TEXT NOT NULL,
status TEXT NOT NULL, -- uploaded, transcoding, ready, failed
duration_ms BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE renditions (
video_id UUID REFERENCES videos(video_id),
name TEXT NOT NULL, -- 360p, 720p, 1080p
bandwidth_bps INTEGER NOT NULL,
playlist_key TEXT NOT NULL, -- media/{video_id}/720p/index.m3u8
PRIMARY KEY (video_id, name)
);Notice that the database stores keys, not URLs. The CDN hostname can change without a data migration.
The status column links the pipeline to the catalog. A video becomes visible only when every rendition and the master playlist are written, and then status flips to ready. Writing the files first and updating the status last avoids showing a video that cannot play.
Common errors
- Listing as a database. Calling "list objects" to find videos is slow and costly. Query the metadata store instead.
- Metadata and storage drift. A crash can leave a row with no files, or files with no row. Run a periodic reconciliation job and clean up orphans.
- Overwriting objects in place. CDN caches and players may serve old and new segments mixed together.
- Hot rows. Counters such as view counts updated on every play overload one row. Aggregate them separately.
Exercise
A re-encode of a popular video overwrites media/{video_id}/720p/seg_0004.ts in place. Why is this risky, and what should you do instead?
Show answer
CDN edges may still hold the old segment while a new playlist or other segments come from the new encode, so players can receive mixed output that does not decode cleanly. Write the new encode under a new version prefix such as media/{video_id}/v2/, update the stored playlist key in the metadata database after all files exist, and let the old version expire.