{}DSA Atlas

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.

Advanced5 interview questions

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

QuantityValue
Files500M users × 2,000 files = 1T file entries (metadata ~1 KB → ~1 PB metadata incl. versions)
Writes100M DAU × 50 changed files/day ≈ 60k commits/s avg
Block uploadsonly changed chunks; dedup ratio across users can exceed 2×
Notify connections~100M long-poll connections

3. High-level design

desktop clientwatcher · chunkerlocal index DB phone / web block serviceput/get chunk by hash metadata servicecommit · list_changes notify servicelong poll block storage(content-addressed, EC) metadata DBsharded by namespacefiles · versions · journal change events (Kafka) cache(hot) bytes and metadata take separate paths; the journal cursor drives every device's sync
Block service for bytes, metadata service for truth, notify service for "something changed".

4. Data model

Text
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

clientmetadatablock service commit(path, [h1,h2,h3], parent=v6) need [h3] put h3 (only the changed chunk) commit again v7 + journal cursor 9182 ok v7 (or conflict if parent ≠ v6)
Dedup by hash means most commits upload little or nothing.

Content-defined chunking

Python
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

DecisionChoice
Chunkingcontent-defined (~4 MB) for dedup stability
Metadatasharded SQL by namespace (transactions + journal)
Blockscontent-addressed object store, erasure coded
Change detectionjournal cursors + long-poll notifications
Conflictsconflicted copies for opaque files
Encryptionat rest server-side; client-side E2EE breaks server dedup across users

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.