System Design · 03. Data & Storage · 8 min · Beginner
Files & Media
Store photos and videos in object storage, and keep only a small record in the database.
In short
Keep files in object storage and a 1 KB metadata row in the database. The app signs a short-lived URL, the browser uploads straight to storage, and workers process the file in the background.
A CDN serves viewers, cutting storage reads from 200 to 10 GB a day, and chunked uploads let big files resume. The app servers handle only 200 MB/day instead of 500 GB/day.
Key ideas
- Bytes go to object storage; facts about the bytes go to the database. (step 3)
- At the end the client says "complete", and storage joins the chunks into one object. Unfinished uploads should expire so they do not cost money. (step 8)
Good to know
- Object storage providers design for extreme durability: S3 advertises 11 nines, so losing a file is very rare. (step 2)
- Uploads scale with the storage service, not with your servers. You can run a few small app servers. (step 5)
- Keep the bucket private and let only the CDN read it. For private photos, give each viewer a signed link that expires. (step 7)
Watch out
- A database is built for small rows you search and update. Big files make it slow and expensive. (step 1)
- The app checks that the user is allowed to upload before signing, and sets limits: one key, the file type and a maximum size. (step 4)
- Never trust a file because of its name or type header. Check the bytes before you serve them to other people. (step 6)
Key numbers
- Database growth
- 100 MB/day
- Bytes through app
- 200 MB/day
- Upload time
- 2.0 s
- Storage reads
- 10 GB/day
Terms
- Blob in the database
- Storing a file inside a database column; it makes the database large, slow and costly to back up.
- Object storage
- A service that stores files as objects found by key, such as S3, Google Cloud Storage or Azure Blob.
- Bucket
- A named container of objects in object storage.
- Metadata row
- A small database record about a file: owner, key, size, type and status.
- Pre-signed URL
- A temporary link, signed by the app, that lets one client read or write one object.
- Direct upload
- The browser sends the file straight to object storage instead of through the app server.
- Processing pipeline
- Background workers that scan, resize or convert a file after upload, started by an event.
- Multipart upload
- Splitting a big file into chunks that are sent and confirmed one by one, so a failed upload can resume.
- CDN for media
- Edge servers cache photos and videos near viewers, so storage serves only the misses.