KatInterview

Photo Library Backend: Safe Uploads, Dedup and Database Growth

System DesignModel answer
MediumModel answerSystem Designsystem-designobject-storageconsistency

Last updated

Design the storage backend for a photo-sharing service, covering upload consistency, instant visibility, duplicate prevention, and how to justify and scale the metadata database.

Design the backend for a photo library service. Users upload pictures, group them into albums, and browse them from any device. The focus is on infrastructure (storage, consistency, data modeling, scaling), not on UI or features such as editing or face search.

Expect the requirements to change mid-discussion. Revise your design when they do, rather than defending your first version. No traffic numbers are provided, so state your own assumptions and size the system from them.

Questions you may want to ask up front: Can a photo be in several albums? Does deleting an album delete its photos? Which derived sizes are needed and how quickly? Are albums private or shareable? Photos only, or video too? Is deletion permanent or recoverable?

Part 1: Requirements, API, data model. Separate functional from non-functional requirements and prioritize them. Propose an API for upload, album management and paginated browsing. Define a schema for users, photos, albums and the photo-to-album many-to-many link. Sketch the components and the request flow for upload and for viewing an album.

Part 2: The upload path.

  1. Image bytes go to blob storage and a record goes to the metadata database. How do you keep the two consistent, including failures and cleanup?
  2. Right after uploading, the user expects to see the photo immediately, even though thumbnail generation and other processing lag behind. How do you handle that?

Part 3: Duplicates. How do you stop the same user from uploading the same photo more than once? Consider exact-file versus visually similar duplicates, rejecting versus mapping to the existing photo, and two concurrent uploads of the same file.

Part 4: The database. You choose Postgres for metadata. Justify it against this schema's access patterns. At what scale does a relational database become the bottleneck, which signals tell you, and what is your staged scaling path, including a shard key and its costs?

Hints

Hint 1

Never let a metadata row point at bytes that do not exist: give each photo a state (for example pending, uploaded, ready) and only expose it once the bytes are confirmed. Also plan a sweeper for orphaned bytes and stale pending rows.

Hint 2

For instant visibility, serve the uploader from the original image (or a client-side local preview) and route that user's reads to the primary or by a session token until replicas catch up; swap in thumbnails when the async job finishes.

Hint 3

For dedup, combine an idempotency key per upload attempt with a content hash (such as SHA-256) under a unique constraint on (user_id, content_hash), so concurrent duplicates collide in the database rather than in application code.

Hint 4

For scaling, look at the numbers: write rate, working set versus RAM, replica lag and connection counts. Move through indexing and caching, then read replicas, then partitioning, then sharding by user_id, noting that cross-user features become harder.