Case study: File storage and sync (Dropbox)
Overview
A file sync service keeps a folder identical across a user’s laptop, phone and the web, and shares folders with others. The design challenge is moving as little data as possible, keeping metadata consistent, and resolving edits that happen on two devices at once.
Try the exercise first: 45 minutes, then compare.
1. Requirements
Functional
- Upload, download, update and delete files and folders; sync across devices automatically.
- Version history (restore a file from up to 30 days ago).
- Share folders with other users (view or edit).
Non-functional
- Never lose or corrupt data (durability is paramount).
- Efficient: small edits to large files shouldn’t re-upload the whole file; work on flaky networks.
- Changes appear on other devices within seconds; strong consistency for metadata.
2. Estimates
changes : 50M users × 10 = 500M file changes / day ≈ 6,000 commits / s (peak ~20,000 / s)
storage : say 10 GB per user × 500M total users = 5 EB raw before dedupe — dedupe and
compression matter enormously; versions add more
metadata : billions of files × a few hundred bytes → tens of TB, sharded3. API (used by the client)
POST /v1/blocks/check {"hashes": [...]} → which hashes are missing
PUT <presigned URL> (block bytes) → upload missing blocks
POST /v1/files/commit {"path", "parent_version", "blocks": [...], "size"} → new version or 409 conflict
GET /v1/changes?cursor=… → list of changes since cursor + new cursor
GET /v1/notify?cursor=… → long poll: returns when something changed4. Data model
namespaces: namespace_id (a user's root or a shared folder) | owner | members
files: namespace_id | path / file_id | latest_version | deleted
versions: file_id | version | block_hashes (ordered list) | size | modified_by | created_at
journal: namespace_id | seq | change (file_id, version, op) -- drives sync cursors
blocks: object storage key = SHA-256 hash; reference counts for garbage collectionThe metadata store needs transactions and conditional updates: a relational database sharded by namespace works well.
5. High-level design
Upload flow: the client splits the changed file into blocks and hashes them, asks which hashes the server lacks, uploads only those, then commits a new version listing the block hashes. Download flow: other devices are notified, fetch the changes since their cursor, and download any blocks they don’t have locally.
6. Deep dives
Chunking and deduplication
- Fixed-size blocks (for example 4 MB) are simple; editing the middle of a file changes only the affected blocks.
- Content-defined chunking (rolling hash boundaries) keeps boundaries stable when bytes are inserted, so an insertion doesn’t shift every following block.
- Blocks are identified by a cryptographic hash, so identical blocks (the same file in two folders, or two users’ copies of the same video) are stored once: deduplication.
- Compress blocks before upload; encrypt at rest.
Consistent metadata and commits
Commits are conditional: “create version 8 of this file, based on version 7”. If the current version isn’t 7, the server rejects the commit with a conflict. Each commit also appends to the namespace’s journal with a sequence number, which is what devices use as their sync cursor. All of this happens in one metadata transaction.
Notifications and sync
Clients keep a long-poll request (or WebSocket) open to the notifier. When a namespace changes, waiting clients get a “something changed” response, then call GET /changes?cursor=… to fetch the exact changes. This keeps notifications lightweight and makes sync robust: a device that was offline for a week simply fetches from its old cursor.
Conflicts
If two devices edit the same file offline, the second commit fails its version check. Rather than silently overwrite, keep both: save the second as “report (Asha’s laptop conflicted copy).docx” and let the user resolve it. Real-time collaborative editors use operational transforms or CRDTs instead, a different problem.
Versions and garbage collection
Old versions are just old block lists, so history is cheap thanks to dedupe. A background job removes versions older than the retention period and deletes blocks no longer referenced by any version (reference counting or mark-and-sweep), carefully, since deleting a still-referenced block loses data.
7. Trade-offs and bottlenecks
- Smaller blocks mean better dedupe and delta sync but more metadata and requests.
- Metadata must be strongly consistent; bytes can be eventually replicated.
- Hot shared folders (a company-wide folder) concentrate on one namespace shard.
8. Follow-up questions
- Large shared folders with thousands of members? Notifications fan out per namespace; clients pull changes.
- LAN sync? Devices on the same network can exchange blocks directly.
- Upload on a flaky network? Blocks are independent and retried; commits happen only when all blocks are present.
Recap
File sync services split files into content-hashed blocks so they upload only what changed and store identical data once. Bytes live in object storage; metadata (files, versions, block lists, a change journal) lives in a strongly consistent store, updated with conditional commits. Lightweight notifications prompt devices to pull changes since their cursor, conflicts become conflicted copies, and garbage collection removes unreferenced blocks.
Design it yourself (45 minutes)
Set a 45-minute timer and design a file sync service: users install a client on several devices; files in a folder stay in sync everywhere; files up to 50 GB; version history for 30 days; sharing folders with other users. Assume 50 million daily active users with 10 file changes each per day. Use the framework, then compare.
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Data model:
7. High-level design:
8. Deep dives (chunking, dedupe, notifications, conflicts):
9. Trade-offs and bottlenecks:A strong answer covers:
- Chunking: split files into blocks (e.g. 4 MB, or content-defined chunking), hash each
block (SHA-256); upload only blocks the server doesn't have → delta sync + dedupe.
- Block service + object storage keyed by block hash; presigned URLs for transfer.
- Metadata service + DB: namespaces, files, versions, ordered list of block hashes per
version, journal of changes with a per-namespace cursor; strongly consistent
(relational, sharded by namespace/user).
- Commit protocol: upload blocks → commit new version (conditional on parent version).
- Notifications: long polling or WebSocket; clients then fetch changes since cursor.
- Conflicts: if parent version is stale, keep both ("conflicted copy").
- Versions: keep old block lists 30 days; garbage-collect unreferenced blocks.
- Estimates: 500M changes/day ≈ 6k commits/s; storage in PB; dedupe saves a lot.Summary
- Split files into blocks identified by content hashes, so only changed blocks are uploaded and identical blocks are stored once.
- Keep bytes in object storage and file metadata (versions, block lists, folder tree) in a strongly consistent metadata store.
- Commit a new version only after its blocks are uploaded, conditionally on the version it was based on.
- Notify other devices of changes (long polling or WebSockets) and let them pull changes since their cursor.
- Detect conflicts with version checks and keep both copies rather than silently overwriting.