What it is
A photo-sharing platform lets people upload photos from their phones and lets everyone else view them in feeds, on profiles and through shared links. Under the product sit four distinct jobs: accepting large uploads durably, producing derived images (thumbnails and resized versions), storing metadata about every photo, and serving an enormous number of reads quickly all over the world. This case study designs each of them and simulates the parts where load decides the outcome.
Why it exists
As a design exercise it is the classic example of a system whose read path and write path have completely different shapes. Uploads are relatively rare but large and slow; views are tiny per request but outnumber uploads by two or three orders of magnitude; metadata is small but queried constantly. A design that treats all three alike, for example one API that receives bytes, stores them and serves them back, fails on every axis at once.
How it works
Requirements and estimates. Functional: upload a photo, view photos (feed, profile, link), generate thumbnails. Non-functional: never lose an accepted photo, fast views worldwide, a new photo may take a few seconds to appear. Illustrative numbers used in the lesson: 50 M daily users, one in ten uploads one photo a day → 5 M uploads/day ≈ 58/s on average and ~150/s at peak. With 2 MB per original and ~300 KB of thumbnails that is ~11.5 TB of new data per day, ~4 PB per year. At 30 views per user per day, 1.5 B views/day ≈ 17,000/s on average: a read-to-write ratio of ~300:1. Metadata at ~1 KB per photo is ~5 GB/day.
Uploads with presigned URLs. The phone asks the Upload API for permission to upload. The API authenticates the user, picks an object key (for example photos/{user}/{photo_id}) and returns a presigned URL: a URL carrying a signature made with the service’s credentials that permits one operation (a PUT of that key, often with a size and content-type constraint) for a short time. The phone uploads the bytes directly to object storage, then tells the API the upload is complete (or storage emits an event when the object lands). Large files use multipart upload so a dropped connection resumes instead of restarting. The API never touches the bytes, so its workers are busy for milliseconds, not for the seconds a mobile upload takes.
Metadata. On completion the API writes a row: photo id, owner, object key, dimensions, caption, timestamps, visibility. The dominant queries are “this user’s photos, newest first” and “this photo’s details”, so the store is sharded by user id (hash of the id, with consistent hashing or a directory so shards can be added). A profile page then reads one shard. Photo ids should embed the owner’s shard or be looked up through a small mapping, so a direct link can find its row.
Thumbnails asynchronously. The API also enqueues a job (or object storage publishes an “object created” event). Workers fetch the original, produce a few sizes and formats, write them back under keys derived from the photo id and size, and mark the row ready. The upload request does not wait for any of this. Workers must keep up with the average upload rate; the queue absorbs bursts. Jobs are delivered at least once, so they are idempotent: re-running a job overwrites the same keys with the same bytes.
Reads through a CDN. Photos are immutable once uploaded (an edit creates a new key), so they can be cached aggressively with long TTLs. Viewers fetch image URLs from a CDN; the edge serves hits and fetches misses from object storage (the origin), usually through an origin shield, a mid-tier cache that collapses misses from many edges into one origin fetch. Popularity is heavily skewed, roughly Zipf: a small fraction of photos (new posts from popular accounts) gets most views, so an edge cache holding a small fraction of the catalogue answers most requests. The long tail of old photos misses and sets the tail latency. Private photos use signed CDN URLs or cookies with short expiry.
When to use it
This shape fits any service built around user-uploaded media: photos, documents, audio, short videos, attachments in chat or email. Whenever blobs are large, immutable and read far more often than written, separate the bytes (object storage + CDN) from the metadata (database) and do derived work asynchronously.
When not to
- Small internal tools with a handful of users: storing files through the app server is simpler and the problems above never appear.
- Data that changes in place frequently (collaborative documents): immutability and long CDN TTLs no longer hold; you need versioning and invalidation designed in.
- Strictly private content where every read must be authorised against live permissions: CDN caching still works with signed URLs, but revocation is delayed until they expire.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Presigned direct upload | API stays small and fast; storage scales the bytes | Two-step protocol; orphaned objects from abandoned uploads need cleanup (lifecycle rules) |
| Async thumbnails | Fast uploads; processing scales separately; bursts buffered | A photo can be visible before its thumbnails exist; idempotency and monitoring of backlog |
| Shard metadata by user id | Profile reads and writes touch one shard | Cross-user queries need a separate index; very active accounts make hot shards |
| CDN with long TTLs | Most reads never reach the origin; low latency worldwide | Deletes and privacy changes are delayed until purge or expiry; the cache becomes load-bearing |
| Several stored sizes | Small downloads for small screens | More storage and processing per upload; on-the-fly resizing at the edge is the alternative |
Alternatives
- On-the-fly image resizing at the CDN or an image service, caching each requested size, instead of precomputing thumbnails. Less storage and no fixed size list; the first request for a size is slow and the resizer needs protection from arbitrary-size abuse.
- Upload through an edge or upload service that terminates connections close to users and streams to storage, when clients cannot use presigned URLs or content must be scanned before it is stored.
- A wide-column or key-value store for metadata (partition key = user id, clustering by time) instead of sharded relational databases; it gives the same access pattern with built-in partitioning.
Failure modes
- Proxying uploads through application servers: slow clients hold workers for seconds, concurrency runs out and uploads are rejected even though the servers are idle in CPU terms.
- Worker capacity below the average rate: the queue grows without bound and thumbnails arrive later and later; alert on queue age, not just depth.
- Non-idempotent jobs: at-least-once delivery after a worker crash runs the job twice; side effects (counters, notifications) are duplicated.
- Cold or wiped cache: a global purge, a cache fleet restart or a new region sends all views to the origin, which may throttle or fail. Mitigate with an origin shield, request coalescing, targeted purges and origin headroom.
- Hot shard: a very popular account concentrates writes and reads on one metadata shard; cache its profile and spread its reads.
- Orphaned objects: uploads that never call “complete” leave bytes without metadata; lifecycle rules or a reconciliation job clean them up.
In production
Object stores offer very high durability by replicating or erasure-coding data across devices and zones; cross-region replication is extra and has its own cost. Lifecycle policies move old, rarely viewed originals to cheaper storage classes, which matters because storage grows forever. Uploads are scanned (malware, abuse) and stripped of sensitive EXIF data such as GPS location before thumbnails are published. Monitor upload success rate, queue age, CDN hit ratio and origin request rate; the hit ratio dropping is often the first sign of a coming origin overload. Feed generation (which photos a user sees) is its own problem; see the social media feed case study.
Common misconceptions
- “The API should receive the upload so it can validate it.” Validation (size, type, owner) is enforced in the presigned URL’s conditions and after upload by workers; the API does not need the bytes.
- “A queue fixes slow processing.” It fixes bursts. Sustained arrival above processing capacity only makes the backlog longer.
- “A cache holding 10% of the data serves 10% of the requests.” With skewed popularity it serves far more, often most of them.
- “Deleting the object deletes the photo.” Copies remain in CDN caches until they expire or are purged.
Interview angle
Start with requirements and numbers, and say what they imply: “300:1 reads, 11 TB/day of bytes, metadata is small”. Then draw the upload path with presigned URLs, the async pipeline with a queue and idempotent workers, the metadata store with its shard key justified by the main query, and the read path through a CDN. Interviewers often probe: how a new photo appears before thumbnails exist, how you handle deletes and private photos with a CDN, what happens when the cache is cold, how you find a photo’s row from its id, and how storage cost is controlled. Quantify worker counts against the upload rate and origin load against the hit ratio.
Model assumptions
The simulation is a simplified, scaled-down model, not a benchmark: 40 uploads/s and 1,200 views/s instead of the estimate’s peak rates. The Upload API has 64 workers; proxying an upload holds a worker for ~8 s, a control call takes ~15 ms. Object storage is modelled as a store serving 48 requests at once at ~60 ms per read and ~100 ms per write (about 800 reads/s). The direct upload from the phone to storage via a presigned URL is a scripted illustration (packets drawn on the stage, not simulated load); thumbnail workers writing results back to storage are not modelled. Thumbnail workers have 4 slots each at ~300 ms per photo, with a 5 s visibility timeout. Metadata writes go through a consistent-hash router to three shards keyed by a Zipf-distributed user id. Views pick one of 20,000 photos with Zipf(1) popularity; the CDN edge cache is an LRU of 2,000 photos; one-way latencies are 80 ms to the origin, 10 ms to the edge and 60 ms from the edge to the origin. Arrivals are Poisson, service times lognormal. Utilisation is busy-slot fraction, not CPU. All numbers are illustrative orders of magnitude.