Terabytes of photos: where do the actual files live?
FotoFlow kept every photo as a binary column right next to its caption and owner, one database, one backup, one place to look. At 100,000 users it still works. The problem is what happens at the next two zeros.
Two terabytes a day
A million photos a day at 2 MB each is 2 TB per day, 730 TB per year, and the database, its replicas and its backups all grow by that much. A restore now takes days, photo views push the useful rows out of memory, and caption queries slow down behind the megabytes.
The fix is a split used by almost every product that stores files. Bytes in object storage, metadata in the database, joined by the object's key. Object storage like S3 is a huge key to file store: PUT a file under photos/9f2c.jpg, GET it back, no queries, no joins, no size limit. It copies each object across several data centres and costs far less per terabyte than database disks.
Uploads skip your servers
Uploading through app servers makes them an expensive relay: a 2 GB video from a phone holds a connection and memory open for minutes just to pass bytes along. If your servers only relay bytes they do not look at, they are an expensive middle-man.
Instead the app hands out permission and steps aside. The phone asks to upload, the app checks the user, writes a pending row, and returns a presigned URL: a link signed with the app's key that allows one upload, to one key, for the next 15 minutes. The phone sends bytes straight to storage, storage fires an event, and the row becomes uploaded.
Big files go in parts. A multipart upload splits 2 GB into 10 MB pieces sent in parallel, each with its own presigned URL. A failed part is resent, a dropped connection resumes, and one final call stitches the parts into a single object.
Workers make what people actually download
Each upload event goes onto a queue, and workers build the versions the app needs: thumbnails, two or three sizes, a malware scan, and removal of the GPS location hiding in the photo's metadata. Never trust an upload. Check the real file type, size and content in the workers, not what the client claimed.
Downloads outnumber uploads, so they skip the app too. A CDN pulls the first copy of each version to the city's edge and everyone after gets it from there. Give each version a key that changes with its content, like photos/9f2c_w640.webp, and it can be cached for a year without going stale. Video gets cut into a few-second segments at several qualities with a playlist; the player picks the next segment's quality from how fast the last one arrived, so a train entering a tunnel drops to 360p instead of freezing.
What this does not solve
Two stores can disagree: a phone loses signal after getting its URL and the row stays pending forever; a user deletes a photo and the files keep costing money. Every row needs a status, deletes remove the row first and queue the file deletions, and a nightly job clears orphans. Deletion is also a legal duty, and it must reach every copy: sizes, transcodes, backups and the CDN.
The bigger known limit is the public bucket. Making a bucket readable "to get images showing" has leaked entire photo collections with their location data. Keep buckets private, serve through the CDN with signed URLs, and remember that a random key is not access control: if a URL is all it takes to read a private file, that URL must expire.
Your database backup is growing by terabytes a week. What is actually in it that a search needs?
A photo's bytes and its caption are different kinds of data. Which one is your database built for?
The database now holds only facts, and the files live where they belong. Next in the series: REST, GraphQL or gRPC: how should the app talk to the server?
Sources
Cloudflare: what is a CDN
Netflix Technology Blog
MDN: HTTP caching
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.