StackMonkey
Home / Templates / File Storage & Sync

Designing a Distributed Block Deduplication & Sync Engine

Architecting a Dropbox or Google Drive clone requires strict file consistency, cross-device sync synchronization, and byte-level deduplication. Learn how to design chunk-based sync engines, content-defined chunking (CDC), and ACID metadata trees.

Active Sync Users
50M users
Desktop & mobile clients
Total Synchronized Data
10 PB
Immutable object chunks
Deduplication Savings
62% storage
SHA-256 block hashing
Sync Invalidation QPS
40,000 req/s
Sub-500ms cross-device push
Step 1 of 4

Chunk Sizing & Deduplication Economics

Splitting files into discrete immutable chunks allows delta sync: only modified blocks are re-uploaded. Sizing the chunk correctly balances metadata overhead against bandwidth conservation.

Chunk Sizing Trade-offs (4 MB Standard)

  • 1. Chunk Size Selection: If chunk size is too small (e.g. 64 KB), a 10 GB file produces 160,000 chunks, overwhelming database metadata indexes. If chunk size is too large (e.g. 64 MB), changing 1 byte requires re-uploading 64 MB. 4 MB chunks represent the optimal sweet spot.
  • 2. Global Deduplication: Each 4 MB chunk is identified by its SHA-256 hash (32 bytes). Before uploading, the client sends a list of hashes to the server. If a hash exists in the global chunk table, upload is skipped entirely, saving bandwidth and storage.
  • 3. Ingress Bandwidth: 50M users modify an average of 4 files/day (16 MB modified data). Total raw daily upload: 800 TB. With 62% deduplication efficiency, real network transfer drops to 304 TB/day (~28 Gbps average).

File Metadata & Version Tree Capacity

  • 1. Metadata Schema: A file is represented as an ordered sequence of chunk IDs in a relational table: file_chunks(file_id, chunk_index, chunk_hash). Directory hierarchy stored as an adjacency list with parent pointers.
  • 2. Total Chunk Metadata: 10 PB storage / 4 MB chunk = 2.5 billion unique chunks. At 128 bytes of indexing data per chunk, the global chunk metadata fits in 320 GB, easily cached in an in-memory index.
  • 3. ACID Isolation: Renames and directory moves require strict transactional consistency. A file rename must update the path atomically across all devices without mutating underlying object storage blocks.
Step 2 of 4

Conflict Resolution & Delta Sync State Flow

What happens when two users edit the same document offline? The synchronization engine must detect parent-version divergence and prevent silent data overwrite.

Client-Side Upload State Machine

1. Chunk File: Client chunks modified file into 4 MB blocks and computes SHA-256 for each.
2. Query Server: Client sends POST /files/check-blocks with chunk hash list.
3. Direct S3 Upload: Server replies with list of missing hashes and presigned S3 PUT URLs. Client uploads only missing blocks directly to S3.
4. Commit Metadata: Once uploads verify, client sends POST /files/commit with expected base_version_id.

Optimistic Concurrency & Conflict Forking

The metadata server executes commit with conditional CAS (Compare-And-Swap):

UPDATE files
SET version = version + 1, updated_at = NOW()
WHERE file_id = :id AND version = :expected_version;

If 0 rows are affected (someone else committed first), the server rejects the commit with a 409 Conflict. The client creates a conflicted copy (e.g. Document (Alice's conflicted copy).docx) rather than destroying the remote file.

Step 3 of 4

Sync Notification Storm & Socket Exhaustion

Why shared folders with 1,000 collaborators cause cascading connection drops.

The Invalidation Storm

Suppose a team pushes 10,000 files into a shared engineering workspace. The notification service must push 10,000 invalidation alerts to all 5,000 member desktop clients = 50,000,000 outbound push notifications.

All 5,000 clients immediately wake up and issue concurrent metadata diff queries: GET /files/delta?cursor=xxx.

If connection pooling limits metadata DB workers to 100 concurrent threads, average response time spikes from 12ms to 4,800ms. Clients time out after 5s and retry with exponential backoff, quadrupling load and freezing client synchronization.

Production Mitigations
  • Notification Coalescing: Buffer invalidation events on the server for 2 seconds. Send a single batch cursor update rather than 10,000 individual notifications.
  • Cursor Delta Pagination: Clients request diffs strictly by monotone cursor sequence. Results are paginated in chunks of 500 items, preventing giant memory allocations in the metadata layer.
  • Client Jitter & Random Backoff: When reconnecting or receiving bulk notifications, clients apply random jitter (100ms - 3,000ms) before hitting the delta API.
Step 4 of 4

Six-Component Distributed Sync Grid

The complete architecture decoupling control-plane metadata operations from data-plane block storage.

01 路 Notification Cluster
Go WebSocket / SSE Gateway
Maintains persistent connections to 50M client devices. Broadcasts lightweight workspace invalidation tokens when file updates are committed.
02 路 Metadata Service (ACID)
CockroachDB / PostgreSQL Shards
Stores file hierarchies, permission ACLs, version vectors, and chunk mapping tables. Guarantees serializable isolation for cross-device file moves.
03 路 Global Block Index Cache
Redis Cluster (SHA-256 Hashes)
High-performance in-memory index mapping 32-byte chunk hashes to reference counts and object storage URIs. Evaluates block deduplication in < 2ms.
04 路 Presigned Upload Token Broker
Stateless Auth Service
Generates cryptographic presigned S3 PUT/GET URLs. Clients upload raw blocks directly to S3, bypassing app servers to eliminate bandwidth bottlenecks.
05 路 Immutable Block Object Store
Amazon S3 / Ceph Multi-Region
Content-addressable storage storing raw 4 MB encrypted blocks under `s3://bucket/{hash}`. Blocks are never overwritten or deleted while reference count > 0.
06 路 Garbage Collection Worker
Distributed Sweeper Fleet
Asynchronously scans orphan chunk references following file deletions. Decrements block reference counters and purges unreferenced S3 blobs after a 30-day grace window.
Interactive Discrete Event Simulation

Simulate the File Storage Topology Live

Test 4 MB vs 64 MB chunking tradeoffs, inject network reconnect notification storms, and verify metadata CAS conflict resolution live in the browser.