Sizing and Securing a Deduplicating Document Vault
System DesignModel answerLast updated
Design a long-term document storage service: estimate storage with replicas, add ingestion-time deduplication, and layer encryption, key management, authorization and versioning.
Scenario
Your team is building a document vault. Users upload files (PDFs, DOCX, images), fetch them later, and update them over time. Expected load:
- 10,000 uploads per day
- About 5 MB per file on average
- Every document must be kept for at least 5 years
- Traffic is uneven, with occasional bursts well above the average
What to cover
- Capacity estimate. How much raw storage is needed for one year of uploads? How much if the business also requires two additional copies of all data in geographically separate regions? State your assumptions, including how you read "two backups" and which unit convention you use.
- Deduplication at ingestion. Propose an architecture that detects identical files as they arrive. Describe the upload flow, the data model, and what the extra work does to upload performance.
- Security. Explain how stored files are encrypted at rest and how you make sure only authorized users can reach a given file. Which components handle key management and authorization checks?
Also address
- How the design copes with traffic bursts.
- The trade-offs of computing the content hash on the client versus on the server.
- What happens when a user replaces or edits an existing document (versioning).
Give concrete numbers where you can, and call out the risks and trade-offs of your choices.
Hints
Hint 1
Start with the arithmetic: files per day times size per file, then scale to a year, then multiply by the number of copies. Say explicitly whether 'two backups' means 2 or 3 total copies.
Hint 2
Think content-addressable storage: use a cryptographic hash of the bytes as the blob key, keep logical documents as separate rows that point to blobs, and decide how concurrent uploads of the same new content are resolved.
Hint 3
Deduplication interacts with security. Consider what a client-supplied hash leaks (does a file exist?), whether the server must re-verify it, and how per-user encryption keys conflict with cross-user dedup. Scoping dedup to a tenant is one answer.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach in one line
Upload directly to private object storage, key blobs by content hash, keep metadata and permissions in a database, and wrap every blob's key in a per-tenant key held in a KMS.
1. Capacity
Assumptions: decimal units (1 GB = 1,000 MB), no compression, metadata counted separately.
| Quantity | Calculation | Result |
|---|---|---|
| Daily ingest | 10,000 x 5 MB | 50 GB/day |
| Yearly primary | 50 GB x 365 | 18.25 TB |
| Primary + 2 backups (3 copies) | 18.25 x 3 | about 54.75 TB/year |
| 5-year retention, primary | 18.25 x 5 | about 91 TB |
| 5-year retention, 3 copies | 54.75 x 5 | about 274 TB |
"Two backups" is ambiguous. If it means two copies in total, the yearly figure is about 36.5 TB. I would state the 3-copy reading and mention the alternative.
Metadata is small: about 3.65M documents per year at about 1 KB each is roughly 3.65 GB, which any single relational database handles.
Throughput. The average is about 0.12 uploads/s and 0.6 MB/s. Plan for 10-50x peaks, so about 6 uploads/s and 30 MB/s at the high end. That is modest, but the design should not push file bytes through application servers.
Cost lever. Lifecycle rules move objects to infrequent-access after 30-90 days and to archive after about a year, since old documents are rarely read.
2. Deduplication
Content-addressable storage. Blob key = SHA-256 of the content. Logical documents point to blobs.
blobs(hash PK, size, storage_key, ref_count, state) -- PENDING | COMMITTED
documents(doc_id PK, owner_id, tenant_id, name, current_version)
versions(doc_id, version_no, blob_hash, uploaded_by, created_at)
acl(doc_id, principal_id, permission)
Flow (client-side hashing):
- Client hashes the file and calls
POST /uploads {name, size, sha256}. - Server authorizes the caller and looks up the hash (scoped to the tenant).
- Hit: create the document and version rows and atomically increment
ref_count. No bytes move. - Miss: conditionally insert a
PENDINGblob row (insert-if-not-exists, so concurrent uploads of the same file resolve to one winner) and return a short-lived pre-signed PUT URL.
- Hit: create the document and version rows and atomically increment
- Client uploads straight to object storage.
- A completion event triggers a worker that recomputes the hash from the stored object. If it matches, mark the blob
COMMITTED; otherwise delete the object and fail. - Async workers handle virus scanning, indexing and previews from a queue, which also absorbs bursts.
Performance. SHA-256 runs at roughly 0.5-1 GB/s per core, so hashing 5 MB takes about 5-10 ms. The extra metadata lookup costs a few milliseconds. On a hit the upload gets dramatically faster. On a miss there is one extra round trip, and verification is off the critical path.
Client vs. server hashing
| Client-side | Server-side | |
|---|---|---|
| Bandwidth | Saved on duplicates | Full upload every time |
| Latency on hit | Near zero | Full upload time |
| Trust | Client may lie; server must re-verify | Authoritative |
| Security | Existence side channel; knowing a hash could let someone claim a file they never had | No leakage |
| Client effort | Needs hashing in SDK or browser | Simple |
Mitigations: dedup only within a tenant, require proof of possession (hashes of random byte ranges), or accept every upload and dedup only storage rather than transfer.
Reference counting. Decrement on delete or expiry, and garbage-collect blobs at zero after a grace period. Make increments idempotent and reconcile periodically, because crashes can skew counts.
3. Security
Encryption at rest: envelope encryption.
- Each blob gets a random data key (DEK).
- The DEK is wrapped by a per-tenant key-encryption key (KEK) in a KMS or HSM, and the wrapped DEK is stored with the blob metadata.
- Rotation re-wraps DEKs only; data is not rewritten.
- Deleting a tenant's KEK (crypto-shredding) makes its data, including backups, unreadable.
- Use client-side envelope encryption if the storage provider is not trusted.
Encryption vs. dedup. Per-user keys defeat cross-user dedup. The practical compromise is per-tenant keys and tenant-scoped dedup. Convergent encryption allows global dedup but is vulnerable to confirmation-of-file attacks.
In transit. TLS everywhere, enforced in bucket policy.
Authorization components
- API gateway validating OAuth/OIDC tokens.
- Authorization service evaluating ACLs, groups and share links before every read or write (a relationship-based model suits sharing).
- Pre-signed URLs issued only after an authz check, with a short TTL (about 5 minutes), scoped to one object and method, against a private bucket. Use a download proxy if you need watermarking or per-byte auditing.
- KMS policy allowing only the storage service role to decrypt.
- Immutable audit log of access, key use and permission changes.
A hash must never act as an access credential.
Versioning
An update creates a new versions row and advances current_version. Each version points to its own blob, so re-uploading unchanged content costs no extra storage. Retention rules keep the last N versions or all versions for X days, never violating the 5-year minimum. For large files with small edits, content-defined chunking (rolling-hash boundaries) with chunk-level dedup stores only changed chunks, at the cost of more metadata and more complex reads.
Spikes and failure handling
- Direct-to-storage uploads keep the app tier stateless and cheap to scale.
- Queues decouple scanning and indexing from ingest.
- Replicate across regions asynchronously, and monitor replication lag.
- Rate-limit per tenant.
Common mistakes
- Forgetting to multiply for replicas, or not stating the "two backups" interpretation.
- Trusting the client hash without server verification.
- Ignoring the dedup/encryption conflict and the existence side channel.
- Routing file bytes through application servers.
- Long-lived or unscoped pre-signed URLs.
- Forgetting garbage collection for deduplicated blobs.