Exact-Match File Deduplication at Petabyte Scale
System DesignModel answerLast updated
Design a service that finds byte-identical files across billions of stored objects and lets clients check for existing content before uploading, while keeping network transfer low.
Exact-Match File Deduplication at Petabyte Scale
You are designing a component of a large cloud storage platform that holds billions of files and petabytes of data. Two files are duplicates if their contents are identical byte for byte, regardless of file name, folder, or owner.
The service has to work in two modes:
- Online: before a client uploads a file, it can ask whether identical content is already stored, so the upload can be skipped.
- Offline: a periodic batch scan locates duplicates already sitting in storage and reconciles the online index.
Requirements
Functional
- Detect every group of exact duplicates across the distributed store.
- Expose an API that answers "does this content already exist?" before an upload.
- Handle millions of new files per day.
Non-functional
- The existence check must have low latency.
- The duplicate index must be highly available.
- The design must stay correct even if two different files produce the same hash.
Constraints
- File sizes range from a few KB to hundreds of GB.
- Keep network I/O low. Avoid pulling whole files across the network just to compare them.
What to cover
- The high-level architecture and the data flow when a file is ingested.
- The schema of the metadata index.
- How the design treats small, medium and very large files efficiently.
- How you would detect and handle a hash collision.
State your own scale assumptions up front and use them to justify your choices.
Hints
Hint 1
Size the problem first. Work out how big an index of one digest plus metadata per file would be. If it fits in a sharded key-value store, the hard part lies elsewhere: hashing data without moving it, and staying correct under concurrency.
Hint 2
Order comparisons from cheapest to most expensive: file size from metadata, then a hash of a few sampled byte ranges, then a full-content hash only for files that are still candidates. Run the hashing next to the disks and send back only digests.
Hint 3
Think about what can go wrong beyond collisions: two uploaders racing with the same new content, a client that lies about its hash, someone who learns a hash and claims a file they never had, and deleting a blob that another file still references.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Scope and sizing
Duplicates mean identical bytes, so names, paths and owners are irrelevant. I would build two paths: an online pre-upload check and an offline batch scan for backfill and drift repair.
Assumptions:
| Quantity | Estimate |
|---|---|
| Files | 10 billion |
| Data | about 100 PB (mean about 10 MB, long tail to hundreds of GB) |
| New files | 5M/day, about 60/s average, about 600/s peak |
| Check traffic | a few thousand QPS (retries, sync clients) |
| Check latency | p99 under 20 ms |
| Index availability | 99.99% |
An index entry is roughly a 32 B digest plus about 100 B of metadata, so 10B entries come to about 1.5 TB. That fits comfortably in a sharded KV store with hot data and Bloom filters in memory. The real difficulty is hashing petabytes without shipping them across the network, plus correctness under races, deletes and malicious clients.
2. API
POST /v1/content/check { sha256, size, partial_hash } -> { exists, content_id?, challenge? }
POST /v1/files { owner, path, content_id | upload_session } -> { file_id }
DELETE /v1/files/{id}
Ownership proof. If knowing a hash were enough to attach to a stored file, anyone who learned the hash of a private file could gain access to it. A positive check therefore returns a challenge: random byte ranges the client must hash together with a server nonce. The server checks the answer against its own copy.
3. Data model
content (one row per unique blob), partitioned by digest, which is uniformly distributed and so avoids hot shards.
| Column | Notes |
|---|---|
hash_algo, digest (PK) | multihash style, so algorithms can be migrated |
size_bytes | compared on every match |
partial_hash | hash of first, middle and last 64 KB |
chunk_manifest | ordered chunk hashes for large files |
storage_locations | blob keys and regions |
ref_count | number of logical files pointing here |
state | PENDING, ACTIVE, DELETING |
created_at, last_ref_change | used by garbage collection |
file (logical files), partitioned by owner_id: file_id, owner_id, path, content_digest, size, created_at, deleted_at.
Batch index on (size_bytes, partial_hash) lives in a columnar warehouse, not the online store.
Storage and availability. Use a Dynamo-style store (Cassandra, DynamoDB, Bigtable) with 3x in-region replication and asynchronous cross-region replication. Put a cache and a per-shard Bloom filter in front. Most uploads are new content, so a quick negative answer avoids most disk reads.
4. Ingestion flow
- The client streams the file in chunks (for example 8 MB), computing per-chunk SHA-256, a Merkle root, the size and the partial hash.
- It calls
check. The server consults Bloom filter, then cache, then KV store. - Hit: the client answers the ownership challenge; the server atomically increments
ref_countand writes afilerow. No file bytes move. - Miss: the client uploads chunks. Storage nodes recompute hashes as bytes arrive; client-supplied hashes are never trusted.
- Commit: a conditional
PutIfAbsent(digest)writes PENDING then ACTIVE. If two users upload the same new content concurrently, one wins and the loser re-points to the winner's blob and drops its own copy. - Publish: a
ContentCreatedevent on a log (Kafka) updates Bloom filters and the warehouse.
5. Handling different sizes
Principle: cheap filters first, expensive hashing last, compute next to the data.
- Tiered comparison: size (free, eliminates most files), then partial hash (about 192 KB read), then full hash only for surviving groups.
- Batch scan: a Spark or Flink job over a metadata snapshot does
GROUP BY size HAVING count > 1, then groups by partial hash, then requests full hashes for survivors. A sidecar on each storage node reads local disks and emits only 32-byte digests, so petabytes never cross the network. - Small files (under about 1 MB): hash inline at ingest; the cost is negligible.
- Large files: chunked hashing with a Merkle tree allows parallel, resumable, incrementally verifiable hashing. Chunk hashes also enable block-level dedup, and content-defined chunking (Rabin fingerprints) finds large files that share most blocks. File identity is the Merkle root.
- Write-time vs batch: write-time dedup prevents new duplicates; batch finds historical ones and repairs drift between the index and reality.
6. Hash collisions
By the birthday bound, for n = 10^10 files and SHA-256 the collision probability is about n² / 2^257, roughly 10^-57. Hardware bit flips and software bugs are far more likely than a genuine collision. The real exposure is a weak algorithm (MD5 and SHA-1 have practical collisions).
Defenses:
- Use SHA-256 or BLAKE3 only.
- Store the algorithm with every digest to allow migration.
- Always compare
size_bytestoo; equal digest with different size is an immediate alarm. - Before physically deleting a redundant copy in the batch path, verify bytes: a full compare for small files, sampled ranges for large ones. Losing user data costs far more than extra reads.
- On a confirmed collision, store the second blob under a suffixed key (
digest#1), mark the digest "collided" so later matches always get a full compare, and alert a human.
7. Trade-offs and common mistakes
- Client vs server hashing: client hashing saves bandwidth but cannot be trusted; server re-verification and the ownership challenge are mandatory.
- Global vs per-tenant dedup: global saves the most space but leaks existence information through the "already exists" response. For sensitive tenants, scope dedup per tenant or encrypt with tenant keys (at the cost of cross-tenant savings).
- Reference counting: increments and decrements must be atomic, and physical deletion should be deferred through a GC grace period so an in-flight upload cannot re-reference a blob being removed.
- Common mistakes: hashing every file fully before filtering by size, trusting client hashes, using MD5/SHA-1, skipping the race on concurrent identical uploads, ignoring the ownership-proof problem, and moving data to a central hashing cluster.