Can't the photos just go in the database?
They can, and at first nothing complains. A furniture marketplace did exactly that: each listing's photos sit in PostgreSQL, in bytea columns next to the price and the seller id. Two years and 400,000 listings later, the nightly backup is 300 GB and runs for most of the night, the read replica spends its bandwidth copying photos, and every page that shows one pulls the bytes through the database and the app server. The listings themselves are 9 GB.
Nobody queries inside a JPEG or changes three bytes of it: it is written once, read many times and served whole. That shape has a store of its own.
Keys, whole objects, HTTP
Object storage, S3-style, keeps objects: a blob of bytes plus a little metadata (content type, size, an ETag, any headers you set), stored under a key inside a bucket. You PUT an object to a key, GET it back, DELETE it and list keys by prefix, all over HTTP. There is no capacity to provision, and the provider stores each object redundantly across devices for durability, as full copies or as erasure-coded fragments (Erasure coding: durability without full copies). It is not a disk: a single VM's disk is block storage, a filesystem shared across instances is file storage (NFS), and point-in-time restore is a backup service's job.
The namespace is flat. listings/88412/photo-1.jpg looks like a path, but in a plain bucket it is one string: the slashes matter only because the list call groups keys by prefix, and renaming a "folder" means copying and deleting every object under it (some providers offer real directories as an option).
An object is replaced, not edited. There is no UPDATE that rewrites bytes 200 to 300, so a seller who crops a photo produces a new object. The marketplace writes the cropped photo under a new key, listings/88412/3f9c2a.jpg, and points the listing at it. Naming each object by a hash of its bytes instead, so identical uploads collapse into one, is Content-addressed storage & deduplication. When two writers hit the same key, nothing merges: one write wins whole. Where losing the other write matters, S3, Google Cloud Storage and Azure Blob Storage can make a write conditional on the object being unchanged since you read it.
A read right after a write gets the new bytes on Amazon S3 (since December 2020) and on Google Cloud Storage; S3-compatible stores and caches in front of a bucket make their own promises, so check yours. Nothing is transactional across keys: a photo and its thumbnail are two writes, and either can fail alone.
The database keeps the key
The move splits each photo in two. The bytes go to the bucket. The database keeps a small row that says what they are: listing_photos(listing_id, object_key, content_type, size_bytes, width, height). Search, permissions and "the first photo of each listing" stay SQL over small rows, and the backup shrinks towards its 9 GB.
Two stores can disagree. A crash between writing the object and inserting its row leaves an object that no row mentions; a row can name an object a bug deleted. Write the object first and the row second: the app uploads the photo, then calls the API to complete, and the API checks that the object is in the bucket before inserting the row. The likely failure is then a stray object that costs a little storage until a sweep removes it, not a listing with a broken image.
Private bucket, one signed operation at a time
A bucket is private unless you open it: every request must be authorised with credentials the store trusts (on S3, a signature), and on S3 new buckets block public access by default (since April 2023). Keep it that way; a bucket policy that allows public reads exposes every object it covers to anyone who can find or guess the key. Buyers read photos through a CDN that fetches from the bucket with its own credentials (CDN & static delivery). Sellers get a presigned URL, signed by the API, that allows one operation on one key, reusable by whoever holds it until it expires a few minutes later, so the photo goes straight from the browser to the bucket; that scoped, expiring permission is the Valet Key pattern, and the full upload path is Large file upload.
The API signs the upload and, when the app calls complete, checks that the photo is in the bucket before recording the key; the photo never passes through it. Buyers read through the CDN, which pulls from the private bucket.
The cleanup job that deleted live photos
A job meant to remove photos of listings closed a year ago misreads a date column and deletes 2,000 photos of live listings. In an unversioned bucket they are gone, and the database backup no longer holds photos. Versioning changes what overwrite and delete mean. With S3-style versioning on, an overwrite keeps the previous bytes as an older version, and a delete adds a delete marker that hides the object without destroying it. Restoring the 2,000 photos means removing the markers the job created.
Old versions are stored, and charged for, like any other bytes, so versioning comes with a rule that expires them after, say, 30 days. And they live in the same bucket, in the same account: whoever can delete versions, or the bucket, can still destroy them all. Copies kept elsewhere, and copies nobody can alter, belong to Backups, high availability & disaster recovery.
Three meters on the bill
Storage is billed per gigabyte-month. Requests are billed by count, with writes and lists priced above reads, and the request fee is the same for a 4 KB thumbnail as for a 4 MB original. Egress, the bytes sent out of the provider's network to the internet, is billed per gigabyte, and at the big clouds' list prices sending a gigabyte out once costs more than keeping it stored for a month. For the marketplace, 300 GB of photos is a small line; buyers viewing 20 TB a month is not. A CDN cuts what is read from the bucket, since repeat views come from the edge, though it sends its own bill. Prices differ a lot by provider, egress most of all.
Colder classes, and rules that run themselves
A photo is hot while its listing is up; after a sale it is kept for disputes and almost never opened. Storage classes trade storage price against access cost. An infrequent-access class charges less per gigabyte-month but adds a per-gigabyte retrieval fee and a minimum storage duration (30 days on S3 Standard-IA); the coldest archive classes cost less again, but some must restore an object, which can take hours, before it can be read. A class saves money only when the access pattern matches: photos buyers open often can cost more in infrequent access, not less, once retrieval fees are counted.
Nobody moves 400,000 objects by hand. A lifecycle rule is a policy on the bucket, matched by prefix, tag or age, that the store applies on its own. The marketplace's rules move objects tagged sold to infrequent access once they are 30 days old and to archive once they are a year old (age counts from the upload, not from the sale), expire old versions 30 days after they stop being current, and expire tmp/ objects after a day, which in this versioned bucket only adds a delete marker; the old-version rule removes the bytes 30 days later. Without rules like these, temporary files and old versions pile up, and so does the bill. Rules run in the background rather than at the moment an object becomes due, so they are a cost tool, not a way to make something disappear on a deadline.
Where the bytes live on your Grid
In the Image Hosting Service challenge, the second stage asks where the binary lives versus the metadata row that describes it. Give it the marketplace's answer: originals and thumbnails in object storage behind a CDN, one row per image in PostgreSQL, and no edge that carries image bytes through the API, with a storage class and a lifecycle rule chosen for each kind of object. When the uploads stop being photos and start being files too big or too slow to send in one request, read Large file upload next.