Video in progress
Why should you store user-uploaded images and videos in object storage rather than in the database?
Large binary files bloat the database: backups and replication slow down, and memory and disk get expensive. Object storage (S3, MinIO) is built exactly for large files: it's cheap, scales without limit and plugs into a CDN easily. The database keeps only the file key and metadata.
📖 Answer
On one project, images were stored in PostgreSQL in a bytea column. The database grew from 20 GB to 800 GB, backups started taking hours, and adding a new replica became a day's work. Meanwhile the "real" data was only about 15 GB.
The typical flow:
- The client asks for permission to upload a file.
- The server gets a temporary pre-signed URL from S3 and gives it to the client.
- The client uploads the file straight to S3 — it doesn't pass through your server.
- Only a record goes into the database:
CREATE TABLE media (
id BIGSERIAL PRIMARY KEY,
owner_id BIGINT NOT NULL,
storage_key TEXT NOT NULL, -- for example: users/42/7f3c.jpg
size_bytes BIGINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
When displaying, the file is served through the CDN.
Saving to local disk is a bad option too: with two servers, an image uploaded to the first one won't be on the second.
Sign up to mark studied questions and save your progress.