Design File Storage & Sync (Dropbox / Google Drive)
Store files for hundreds of millions of users and keep every device in sync — chunking with content-defined boundaries, content-addressed dedup, block storage vs metadata service, the sync protocol with a per-user change journal, notifications via long polling, conflict handling, versioning, sharing and permissions, and bandwidth optimisations.
1. Requirements
Functional
- Upload/download files of any size (up to e.g. 50 GB) from desktop, mobile, web.
- Automatic sync across a user's devices; offline edits sync later.
- File version history and restore; deleted file recovery (30 days).
- Share files/folders with users or via links with permissions.
Non-functional
- 500M users, 100M DAU; ~1 EB stored.
- Durability 11 nines; never lose a committed file.
- Sync latency of seconds for small changes; efficient bandwidth (delta + dedup).
- Strong consistency of metadata per namespace.
2. Estimation
| Quantity | Value |
|---|---|
| Files | 500M users × 2,000 files = 1T file entries (metadata ~1 KB → ~1 PB metadata incl. versions) |
| Writes | 100M DAU × 50 changed files/day ≈ 60k commits/s avg |
| Block uploads | only changed chunks; dedup ratio across users can exceed 2× |
| Notify connections | ~100M long-poll connections |
3. High-level design
4. Data model
namespaces ns_id, owner_id, type (user_root | shared_folder)
mounts user_id, ns_id, path
files ns_id, path, latest_version, is_deleted PK (ns_id, path)
file_versions ns_id, path, version, size, chunk_hashes[], parent_version, modified_by, created_at
journal ns_id, cursor (monotonic per ns), path, version, op PK (ns_id, cursor)
chunks hash → location, size, refcount (block store index)
acl ns_id, principal, role
5. Deep dives
Upload and commit
Content-defined chunking
def cdc_chunks(data: bytes, avg_bits=22, min_size=1 << 20, max_size=8 << 20):
"""Gear-hash content-defined chunking: boundaries depend on content, not offsets."""
mask = (1 << avg_bits) - 1 # ~4 MiB average chunk
chunks, start, h = [], 0, 0
for i, b in enumerate(data):
h = ((h << 1) + GEAR[b]) & 0xFFFFFFFFFFFFFFFF
size = i - start + 1
if (size >= min_size and (h & mask) == 0) or size >= max_size:
chunks.append(data[start:i + 1])
start, h = i + 1, 0
if start < len(data):
chunks.append(data[start:])
return chunks # store each by sha256(chunk)
GEAR is a table of 256 random 64-bit values. Production implementations (FastCDC) add normalised chunking and are written in native code.
Notifications
Clients hold a long poll: GET /notify?ns=…&cursor=… (timeout ~60 s). The notify service subscribes to change events per namespace and completes waiting requests when the namespace cursor passes the client's. Tens of millions of mostly idle connections → event-loop servers (epoll), like the chat gateways.
Versioning and deletion
Every commit creates a version; old versions retained per plan (30–180 days). Chunks are reference-counted (or garbage-collected by mark-and-sweep over live versions); deletion is soft first. Garbage collection must be careful never to delete a chunk a concurrent commit is about to reference (grace periods).
Bandwidth optimisations
Dedup; delta sync of only changed chunks; compression; LAN sync (devices on the same network fetch chunks from each other); throttling user-configurable; streaming downloads for large files; thumbnails/previews generated server-side for web/mobile.
6. Trade-offs
| Decision | Choice |
|---|---|
| Chunking | content-defined (~4 MB) for dedup stability |
| Metadata | sharded SQL by namespace (transactions + journal) |
| Blocks | content-addressed object store, erasure coded |
| Change detection | journal cursors + long-poll notifications |
| Conflicts | conflicted copies for opaque files |
| Encryption | at rest server-side; client-side E2EE breaks server dedup across users |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.