← Building blocks

Separate metadata from bytes

Storage & data modellingfundamental

One store is asked to be both a transactional index and a petabyte blob store.

Say it like this

Video bytes go to object storage behind a CDN and only the metadata row lives in the database, so the database stays small and transactional while the blobs scale to petabytes independently.

Basic solution

Keep small, queryable metadata in a database and large immutable bytes in object storage, linked by key.

Postgres + S3DynamoDB + S3

Metadata in the database, bytes in object storage

The API writes a row and hands out a signed URL; bytes go straight to the object store. Complete validates the object before the row says ready. Playback reads the row, then fetches bytes by key from the CDN.

How it works

  1. The client asks the API to start an upload. The API inserts a metadata row with status = uploading, derives an object key from the row's new id, and returns a pre-signed URL that lets the client write to exactly that key for a limited time.
  2. The client uploads the bytes directly to object storage using the URL, in parallel parts for a large file. The API servers never receive or forward the bytes.
  3. The client calls the complete endpoint. The API checks that the object now exists and matches the expected size and checksum (a HEAD request on the object returns both), and only then sets status = ready.
  4. A reader queries the database for metadata (title, owner, renditions, visibility) and gets the object key from the row; the bytes are then fetched from the object store, or from the CDN in front of it, by that key.
  5. A delete marks the metadata row first; a background job later removes the objects, and lifecycle rules in the object store delete anything left orphaned.

The contract between the two stores

  • The database is the index and the source of truth for existence and permissions.
  • The object store holds immutable bytes under keys the database knows about; objects are never updated in place, only written under a new key.

Worked example

Video platform

  • 500K uploads per day averaging 1 GB after transcoding into five renditions: 500 TB of new bytes per day, 180 PB a year.
  • Metadata per video: about 2 KB (ids, title, owner, status, rendition list, timestamps). 500K × 2 KB = 1 GB per day, 365 GB a year. A single Postgres primary with replicas handles that for years.
  • If the bytes were in the database, the daily growth alone would be 500,000× the metadata and every backup, replica and vacuum would carry it.
  • Egress: 37 PB per day served from the CDN; the database sees one metadata read per playback start (about 50M per day, 600 per second), and none of the segment traffic.
  • Cost: object storage at roughly $0.02 per GB-month versus database storage at ten to fifty times that, before replicas.
  • Keys: v/{video_id}/{rendition}/seg_{n}.m4s. The id comes from the metadata row, so the key space is unique without coordination.

Variants and when each wins

Database row plus object key (the default)

  • Metadata lives in SQL or a key-value store, bytes live in S3-style storage, and the row holds the key. It wins because each side scales independently; the cost is two writes per upload that must be kept consistent.

Everything in the database (bytea, BLOB columns)

  • Bytes are stored in the row itself, so one transaction covers metadata and content. It wins below a few hundred GB in total; beyond that, every backup, replica and vacuum has to carry the bytes.

Everything in object storage (metadata as JSON objects)

  • There is no database; metadata is stored as small objects next to the data. It wins only for archives or very simple catalogues, because listing and querying become slow scans.

Content-addressed storage

  • The object key is the hash of its content, so identical uploads share one object and keys can never change. It wins when many users upload the same file, and pairs well with the default split.

Use the split for anything over a few MB per record; add content addressing when duplicates are common.

Failure modes

Metadata says ready, object missing

  • Playback returns 404. It happens because the API set status to ready on the client's word without checking the object. HEAD the object and compare its size and checksum before flipping the status, and make the complete call idempotent so a retry is safe.

Orphaned objects

  • The storage bill grows faster than the catalogue. It happens because uploads were abandoned after the pre-signed URL was issued, or deletes removed the row but never the bytes. Add lifecycle rules that abort incomplete multipart uploads after a few days, and run a reconciliation job that lists objects with no matching row.

Object mutated in place

  • The CDN keeps serving the old version, or a viewer receives a half-written file mid-upload. It happens because an existing key was overwritten. Treat objects as immutable: write a new key and update the metadata pointer.

Permissions checked only in the database

  • A private video plays for anyone who has the URL. It happens because the object keys are public and guessable. Keep buckets private and issue short-lived signed URLs or signed cookies only after the database permission check passes.

Two stores, two failure domains

  • Uploads fail while the catalogue works, or the reverse. It happens because the pair was treated as one system with one failure mode. Design status transitions that tolerate either side being down, with retries, and a sweeper that finds uploads stuck in the uploading state.

Interviewer follow-ups

Answer out loud before you open each one.

Why not store small images in the database if they are only a few KB?

A few KB times a billion rows is still terabytes inside every backup and replica, and reads of the row pull the bytes whether the caller wanted them or not. Keep anything that is served to a browser in object storage; the threshold is usually around 1 MB per record, lower at high row counts.

How do you keep the database and the object store consistent?

Make the database the source of truth and the object store write-ahead. Write the object first, then the metadata; on read, missing metadata means the object does not exist yet. A reconciliation job cleans objects without rows and rows whose objects are gone.

How does a client upload a 10 GB file without going through your servers?

The API issues pre-signed URLs for a multipart upload; the client PUTs parts in parallel straight to object storage and calls complete with the part ETags. The API only sees the start and complete calls and validates the assembled object afterwards.

Where do permissions live if the CDN serves the bytes?

In the database, enforced at the moment a signed URL or cookie is issued. The CDN only checks the signature and expiry, so the permission decision is made once per session by your service, not per segment by the edge.

Connections

Pairs with
Keep large blobs out of the database · Upload large files without touching your servers · Serve static bytes from the edge · Process uploads in parallel
Pulls against
Commit across two stores
Used in
Video Streaming Platform (YouTube / Netflix)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingUpload APICompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon DynamoDBMetadata DBFirestore, BigtableCosmos DBScyllaDB, Apache Cassandra
Amazon S3Object storageCloud StorageBlob StorageCloudflare R2, MinIO, Ceph
Amazon CloudFrontCDNCloud CDN, Media CDNAzure Front DoorCloudflare, Fastly, Akamai