Skip to main content
07

Design a file storage and sync service (like Dropbox)

HardChunkingSyncDeduplicationConflicts

Design a file storage and sync service like Dropbox or Google Drive: users keep a folder in sync across their laptop, phone and the web, share files with others, and recover old versions.

A typical framing: "50 million daily active users, each with a few thousand files. Files range from tiny text files to 10 GB videos. Edits on one device should show up on the others within seconds, work offline, and never silently lose someone's changes."

TL;DR

Separate file contents from file metadata. Contents are split into chunks (e.g. 4 MB), each identified by its content hash and stored once in object storage (deduplication across versions and users). Metadata (the folder tree, file versions, which chunks make up each version) lives in a strongly consistent database, because it's what users see and what conflicts are decided on.

The client runs a sync engine: it watches the local folder, hashes changed files, uploads only missing chunks, then commits a new version to the metadata service. Other devices learn about changes through a notification channel (long polling or WebSocket) and pull a change journal from their last cursor. Concurrent edits are detected with version numbers (optimistic concurrency); instead of overwriting, the loser becomes a "conflicted copy".

1. Requirements and estimates

Functional

  • Upload, download, delete, move and rename files and folders.
  • Automatic sync across devices, including offline edits.
  • Sharing with permissions; version history and restore.

Non-functional

  • Never lose data; durability of 11 nines for stored content.
  • Sync latency of seconds; efficient for large files and slow networks.
  • Strong consistency for metadata (no two "truths" about a folder).

Estimates

  • Files: 50M users × 2,000 files = 100B files of metadata; at ~500 bytes each, ~50 TB of metadata (needs sharding).
  • Content: average 1 MB per file → 100 PB before dedupe and with version history; dedupe and compression cut this substantially.
  • Change rate: if each user changes 20 files a day, 1B commits / day ≈ 12,000 commits per second.

2. API

http
POST /v1/files/commit
{ "path": "/Docs/plan.md", "parentVersion": 7, "chunks": ["sha256:ab..", "sha256:9f.."], "size": 8_388_608 }
-> 200 { "version": 8 }
-> 409 { "currentVersion": 9 }                     (someone else committed first)
-> 412 { "missingChunks": ["sha256:9f.."] }        (upload these first)

POST /v1/chunks/batch-check   { hashes: [...] }  -> { missing: [...] }
PUT  <pre-signed URL per chunk>
GET  /v1/changes?cursor=c_811                    -> { entries: [...], cursor: "c_840", hasMore }
GET  /v1/changes/longpoll?cursor=c_840           -> returns when something changes (or times out)
POST /v1/shares                                  { path, userId, role: "viewer" | "editor" }

3. Data model

text
namespaces   (id, owner_id)                         -- a user's root or a shared folder
files        (namespace_id, file_id, path, latest_version, is_deleted)
versions     (file_id, version, chunk_hashes[], size, modified_by, created_at)
chunks       (hash PK, size, storage_key, ref_count)
journal      (namespace_id, seq, file_id, version, op)   -- ordered change log per namespace
shares       (namespace_id, user_id, role)

The journal per namespace is the backbone of sync: every commit appends an entry with an increasing seq, and a device's cursor is simply the last seq it has seen.

4. High-level architecture

Loading diagram...

5. Deep dives and scaling

Chunking and deduplication

  • Fixed-size chunks are simple, but inserting one byte at the start of a file shifts every chunk. Content-defined chunking (boundaries chosen by a rolling hash) keeps most chunks identical after small edits, so only nearby chunks are re-uploaded.
  • A chunk is stored once per hash. Uploading a file someone else already has (or a new version with 1 changed chunk out of 2,500) transfers almost nothing.
  • Cross-user dedupe can leak information ("does anyone have this file?"); many services dedupe per user or per namespace only.
  • Chunks are compressed and encrypted at rest. Garbage collection removes chunks no version references anymore (ref counting plus a mark-and-sweep safety check).

The commit protocol

  1. Client hashes the new file's chunks.
  2. Asks which chunks are missing, uploads only those (in parallel, resumable).
  3. Commits the version with parentVersion. The metadata service checks that all chunks exist and that parentVersion is still the latest, in one transaction, and appends to the journal.

Uploading chunks first and committing last means a crash mid-upload never leaves a version that points to missing data.

Conflicts

Two devices edit the same file offline. The first commit wins (parentVersion matched); the second gets 409. The client keeps the user's work by saving it as plan (conflicted copy from Laptop).md and syncing both. Automatic merging only makes sense for specific formats (like collaborative docs, which use a different design: OT or CRDTs).

Notifying devices

Millions of idle devices each keep one long-poll or WebSocket connection to the notification service, subscribed to their namespaces. When a commit lands, the service pings subscribers ("namespace X changed"), and each device then pulls the journal from its cursor. The ping carries no data, so it can be dropped or duplicated safely.

Scaling metadata

Shard by namespace id: one user's tree, or one shared folder, lives on one shard, so commits are single-shard transactions with strong consistency. Very large shared namespaces (a whole company) may need further splitting. Cache hot directory listings.

Downloads and sharing

Downloads use pre-signed URLs straight from object storage (or a CDN for popular public links). Shared folders are separate namespaces mounted into each member's tree; permissions are checked on every metadata call.

Common follow-ups

  • "How do you sync a 10 GB file that changed by a few bytes?" Content-defined chunking finds the one or two changed chunks; only those (a few MB) are uploaded.
  • "How is the local client kept correct?" It keeps a local database of path → version and chunk hashes, so on startup it can compare the folder with what it last synced and detect edits made while it was closed.
  • "What about deletes and restore?" A delete is a new journal entry and a tombstone; versions and chunks are kept for the retention period (e.g. 30 days), so restore is just committing an old version again.

What interviewers look for

  • Separating content (chunks in object storage) from metadata (strongly consistent DB).
  • Chunking with content hashes, dedupe, and uploading only missing chunks.
  • A commit protocol with versions and conflict handling that never loses data.
  • Change journal plus cursors and lightweight notifications for sync.
  • Sharding by namespace, garbage collection and security considerations.

Was this helpful?

Related questions