Design: file sync (Dropbox)

In one sentence

Split files into hashed blocks, upload only blocks the server lacks, commit the file as a hash list with a version check, and tell other devices to pull changes since their cursor.

Metadata and bytes travel separately: the commit names the blocks, only missing blocks are uploaded, and other devices pull from their cursor.

How it works

  • Split files into 4 MB blocks named by SHA-256; identical blocks are stored and uploaded once.
  • A metadata DB holds paths, versions and blocklists; immutable blocks live in object storage.
  • Commit with a base version: the server names the hashes it needs, then records a journal entry.
  • Idle devices long-poll a notification service, then pull changes since their cursor.
  • Concurrent edits to one file can't be merged: keep both, as a conflicted copy.

Check yourself

Answer to mark it learned; your answers aren't scored.

Your sync client uses fixed 4 MB blocks with SHA-256. Users complain that adding a 10 KB header to the start of a 500 MB file re-uploads almost all of it. Best fix?

Go deeper

Do this round in 45 minutes System design round
Full explanation

Requirements

Functional: upload, download and sync files across a user's devices; shared folders; version history; offline edits. Non-functional: never lose or corrupt a file; small edits sync in seconds; big files don't re-upload in full. Assume 50M daily users making ~10 file changes each.

Data model

  • Blocks: files split into 4 MB blocks (Dropbox's size), each named by its SHA-256 hash and stored once in Object storage (S3-style).
  • Metadata DB: files(namespace_id, path, version, blocklist[], size) plus a per-namespace journal with an increasing cursor. Metadata is small and transactional; blocks are big and immutable.

Sync flow

  1. The client sees a change, rechunks the file and hashes the blocks.
  2. It calls commit(path, blocklist, base_version); the server replies with the hashes it needs.
  3. The client uploads only those blocks and commits again; the server appends a journal entry.
  4. Other devices hold a long-poll to a notification service, hear "namespace changed", call list(cursor) and fetch missing blocks.

Dropbox described essentially this flow in 2014.

The hard parts

Dedup and deltas. Hash-named blocks dedup across versions, files and users: appending to a 120 MB file re-uploads only the new tail. Fixed-size blocks break on inserts, because every later boundary shifts; content-defined chunking (boundaries chosen by a rolling hash) keeps unchanged blocks aligned.

Conflicts. Every commit carries its base_version; if the server's version moved, two devices edited concurrently. Binary files can't be merged, so keep both: Dropbox saves the second as a conflicted copy.

Code Client sync: hash blocks, upload only what's missing
pythonClient sync: hash blocks, upload only what's missing
import hashlib

BLOCK = 4 * 1024 * 1024   # 4 MB, Dropbox's block size

def blocklist(path: str) -> list[str]:
    hashes = []
    with open(path, "rb") as f:
        while chunk := f.read(BLOCK):
            hashes.append(hashlib.sha256(chunk).hexdigest())  # content = identity
    return hashes

def upload(ns: str, path: str, base_version: int) -> int:
    hashes = blocklist(path)
    while True:
        r = meta.commit(ns, path, hashes, base_version=base_version)
        if r.status == "need_blocks":            # send only blocks the server lacks
            for h in r.missing:
                block_server.put(h, read_block(path, hashes.index(h)))
        elif r.status == "conflict":             # someone committed since base_version
            return save_conflicted_copy(ns, path, hashes)   # binary can't merge: keep both
        else:
            return r.version                     # journal entry written; others get notified

def pull(ns: str, cursor: str) -> str:
    changes, cursor = meta.list(ns, cursor)      # only journal entries after our cursor
    for c in changes:
        for h in c.blocks:
            if not local_blocks.has(h):          # dedup on the way down, too
                local_blocks.put(h, block_server.get(h))
        write_atomically(c.path, [local_blocks.get(h) for h in c.blocks])
    return cursor
Real example

Lena appends a day of data (2 MB) to a 120 MB CSV on her laptop. The client rechunks it: the first 30 blocks hash exactly as before, plus one new block. The commit answers "need 1 block"; she uploads 2 MB instead of 122 MB, and the server appends journal entry 9,812 for her namespace. Her phone's long-poll wakes; it lists changes after cursor 9,811 and downloads that single block. Meanwhile her desktop edited the same CSV offline. On reconnect its commit fails the version check, and it saves a conflicted copy next to the original.

Trade-offs +3 · −3

+ You gain

  • Bandwidth and storage scale with changed blocks, not with file size.
  • Immutable, hash-named blocks make uploads idempotent and integrity checks free.
  • A cursor-based journal lets any device catch up after days offline with one list call.

− You pay

  • Fixed-size blocks re-upload everything after an insert near the start; content-defined chunking costs CPU.
  • Conflicted copies push merging onto users.
  • Garbage-collecting blocks shared across versions and users is tricky and must stay conservative.

Use it when

Syncing user files across devices and collaborators: desktop sync clients, backup tools, shared drives, distribution of large build or game assets.

Avoid it when

Real-time co-editing of one document (use OT or CRDTs, Design: collaborative editor), or plain upload-and-serve media, where object storage plus a CDN is enough.

Numbers to know 4
  • Commits (assumed): 50M DAU × 10 changes/day = 500M/day ≈ 5.8k/s average, ~12k/s at a 2× peak.
  • Upload (assumed ~200 KB of new data per change after dedup): 500M × 200 KB ≈ 100 TB/day into block storage.
  • Dropbox: 4 MB blocks hashed with SHA-256 (2014); Magic Pocket stored multiple exabytes (2016).
  • Notifications (assumed ~100k idle long-polls per server): 50M connected devices need ~500 servers.
Common pitfalls 4
  • Storing file bytes in the metadata DB: it bloats, backups crawl, and every listing slows down.
  • Answering "already have that block" without checking the user's access: a known hash leaks another account's data.
  • Last-writer-wins on concurrent edits: one device's work disappears without a trace.
  • Deleting a block as soon as one version drops it: older versions and in-flight commits still need it.
Interview questions 3

Answer out loud first, then reveal.

Why split files into blocks at all?Show answer

Three wins. Bandwidth: an edit re-uploads only changed blocks, so appending 2 MB to a 120 MB file sends 2 MB. Dedup: blocks are named by SHA-256, so identical content across versions, files or users is stored once. Resumability: a dropped upload restarts at the last missing block, not at byte zero. The cost is a blocklist per file in metadata and garbage collection for blocks no version references.

Two devices edit the same file offline. What happens when both reconnect?Show answer

Both commit with the version they started from. The first wins and bumps the version; the second fails the compare-and-set. For binary files I don't attempt a merge: I keep both and save the second as a conflicted copy, which is what Dropbox does, then tell the user. Text formats could merge three-way, but a silent auto-merge of someone's file is a worse surprise than a visible copy.

How do other devices hear about a change fast without hammering the metadata DB?Show answer

Each idle client holds a long-poll to a notification service, sharded by namespace. After a commit, the metadata service publishes "namespace changed", and the notifier completes those long-polls with no payload. Clients then call list with their cursor and get only new journal entries. A content-free notification is cheap, idempotent and safe to drop: worst case, the client's next periodic list catches up.

Deep dive 3

Chunking and dedup

Naming a block by its SHA-256 makes it immutable and self-verifying: the client re-hashes on download to detect corruption, and a re-upload of the same block is a harmless no-op. Fixed 4 MB blocks are simple and handle in-place edits and appends well, but an insert near the start shifts every boundary and re-uploads everything after it. Content-defined chunking cuts where a rolling hash of the bytes hits a pattern, so boundaries move with the content and only nearby blocks change, at some CPU cost. Cross-user dedup saves storage, but the server must check the user may access a block before answering "already have it".

Metadata vs block storage

The two stores have opposite shapes. Metadata is small, hot and needs transactions: a commit must atomically check base_version, write the new blocklist and append to the journal. Shard it by namespace_id (a user's root or a shared folder), so one commit touches one shard and the journal gives a per-namespace order. Blocks are huge, immutable and write-once, a perfect fit for object storage; Dropbox built its own, Magic Pocket, which held multiple exabytes by 2016. Garbage-collect blocks that no version references only after a grace period, because an in-flight commit may be about to reference them.

Conflicts and notifications

A commit is a compare-and-set on the file's version. When it fails, the client keeps its local file as a conflicted copy; Dropbox creates one when two people edit at once, when someone edits offline, or when an autosaving app keeps a file open. Notifications carry no data: the notifier completes the long-poll with "namespace changed", and the client calls list(cursor). That keeps the notifier cheap and stateless, and a dropped notification is harmless because the next list catches up. Cursors also make a device returning after a week offline just another list call.