Content-Addressed Photo Service with Cross-User Deduplication
System DesignModel answerLast updated
Design a service where users upload, view, and remove photos, storing each unique content blob once using its hash as the key.
You're building a photo service that lets users upload, retrieve, and remove their photos. To save storage costs, the system should avoid keeping multiple copies of identical content: whenever two photos have matching SHA-256 hashes, only one copy of the actual bytes should be stored.
Requirements
Your design should address:
- How users upload photos and how the system detects duplicates
- How users retrieve their photos
- How deletion works when multiple users might reference the same underlying content
- What happens when one user uploads the same content another user already has
- Data modeling and storage architecture
- Consistency and concurrency challenges
Constraints
- Two photos are considered identical only if their SHA-256 hashes match exactly; any modification produces different content
- You may assume reasonable scale (state your assumptions)
- Focus on correctness, then discuss scalability
Discussion Points
Consider addressing:
- Whether deduplication should happen across all users or be scoped per-user
- Authorization model: who can access which photos
- Garbage collection strategy for unreferenced content
- Race conditions during concurrent operations
- Privacy implications of shared storage
- Client-side optimizations
Hints
Hint 1
Separate the concept of user ownership from physical storage
Hint 2
Reference counting isn't enough when operations are concurrent
Hint 3
Hashing the same content twice reveals information across user boundaries
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
Architecture Overview
The key insight is separating logical photos (what users see) from physical blobs (what's actually stored). This creates two distinct layers:
User Photo Records: Each entry represents one photo in a user's library, containing metadata (upload timestamp, filename, owner) and a pointer to the content hash.
Content Store: Blobs indexed by SHA-256 hash, stored once regardless of how many users reference them.
Data Model
PhotoMetadata table:
- photo_id (UUID, primary key)
- user_id (indexed)
- content_hash (SHA-256, indexed)
- original_filename
- upload_time
- mime_type
ContentBlob table:
- content_hash (SHA-256, primary key)
- size_bytes
- storage_location (S3 key or similar)
- reference_count
- first_stored_time
The reference count tracks how many PhotoMetadata records point to each blob, enabling garbage collection.
Upload Flow
- Client hashes locally: Before uploading, the client computes SHA-256 of the photo
- Check existence: Client sends hash to server; server checks if ContentBlob exists
- Conditional upload:
- If blob exists: skip transfer, increment reference count, create PhotoMetadata record
- If new: client uploads bytes, server verifies hash matches, stores blob, creates both records
- Atomic operation: Creating PhotoMetadata and incrementing reference_count must be transactional
This client-side deduplication saves bandwidth when uploading content that's already stored.
Download Flow
Authorization happens at the PhotoMetadata level, never by content hash:
- User requests photo by photo_id
- Server verifies user owns that photo_id
- Server retrieves content_hash from PhotoMetadata
- Server generates signed URL or streams blob from ContentBlob storage
- Client downloads content
Critical: never authorize by hash alone, or users could access others' photos by guessing hashes.
Delete Flow
Deletion must handle the reference counting race condition:
Naive approach (broken):
DELETE PhotoMetadata WHERE photo_id = X
DECREMENT ContentBlob.reference_count
IF reference_count = 0 THEN DELETE blob
Race condition: Between decrement and delete, another upload of the same content could increment the count back to 1, then we'd delete a referenced blob.
Correct approach with two-phase deletion:
- Soft delete: Mark PhotoMetadata as deleted, decrement reference_count
- Delayed GC: Separate background job periodically finds ContentBlobs with reference_count = 0 for more than N minutes, double-checks no references exist, then permanently deletes
The grace period (say, 5-10 minutes) ensures any concurrent upload completes before physical deletion.
Concurrency Handling
Upload race: Two users upload identical content simultaneously.
Use database constraints to make content_hash insertion idempotent:
INSERT INTO ContentBlob (content_hash, ...)
ON CONFLICT (content_hash) DO UPDATE SET reference_count = reference_count + 1
Or use distributed locking keyed by hash during upload.
Delete-upload race: One user deletes while another uploads the same content.
The two-phase GC approach handles this: the upload increments reference_count before GC runs, so the blob survives.
Storage Technology
- Metadata: Relational database (PostgreSQL, MySQL) for ACID guarantees on reference counting
- Blobs: Object storage (S3, GCS) indexed by hash, immutable once written
- Scale assumptions: Millions of users, billions of photos, petabytes of unique content
For very high scale, shard PhotoMetadata by user_id and use eventually consistent counters with reconciliation.
Privacy and Security Implications
Cross-user deduplication reveals information:
- Timing attack: If Alice uploads a photo and it completes instantly (deduplicated), she knows someone else already uploaded that exact content
- Confirmation attack: Bob can generate a candidate photo (say, a screenshot from a video), upload it, and learn whether anyone else has that exact frame
- Mitigation: Accept the leak, or scope deduplication per-user (wastes storage but eliminates leakage), or add artificial latency to mask dedup
Authorization vulnerabilities:
- Never expose APIs that accept content_hash as input for retrieval
- Always require photo_id owned by authenticated user
- Don't include hashes in URLs or logs that might leak
Common Mistakes
- Using reference count alone without GC delay: Creates delete-upload race
- Authorizing by hash: Lets users access others' content
- Not verifying uploaded hashes: Client could lie about hash, causing wrong deduplication
- Forgetting partial upload cleanup: If upload fails mid-stream, orphaned blobs need cleanup
- Ignoring metadata growth: Even with dedup, PhotoMetadata grows with every upload
Extensions
- Thumbnails: Generate once per content_hash, deduplicate them too
- Encryption at rest: Encrypt with hash-derived key, so identical content produces identical ciphertext (preserves dedup)
- Multi-region: Replicate ContentBlob across regions, but keep PhotoMetadata local for latency
- Sharing: Add permission table mapping photo_id to authorized users, maintaining same access control model
The design balances storage efficiency with correctness, security, and reasonable complexity.