Design a cloud file sync service

    A file sync service (Dropbox/Drive) keeps files identical across a user's devices. The key ideas are block-level chunking with content-addressed dedup (don't re-upload what already exists) and an efficient change-notification sync protocol.

    Storage & Data, 9 components, about 40 min

    1. Scope

    In scope

    • • Upload/download files
    • • Sync changes across devices
    • • Deduplicate identical content
    • • Handle conflicts

    Out of scope

    • • Real-time collaborative editing
    • • Rich previews
    • • Sharing permissions deep-dive

    2. Functional requirements

    Must have

    • • Upload a file and have it appear on other devices
    • • Only changed parts re-uploaded
    • • Conflict handling
    • • Durable storage

    Nice to have

    • • File versioning
    • • Selective sync
    • • Shared folders

    3. Non-functional requirements

    Durability11 nines (S3-class)
    Sync latencySeconds after a change
    BandwidthMinimized via dedup + delta sync
    Availability99.99%

    4. Back-of-envelope estimation

    Assumptions

    • • 100M users, 50M daily active
    • • Average file 1 MB; chunk 4 MB
    • • Each active user changes ~50 files/day
    • • High dedup ratio across users

    Derived numbers

    Change events/day~2.5B
    Metadata QPSHigh — every change is a metadata op
    Block storeExabyte-scale, deduped
    Dedup savingsOften >50%— Shared/identical files

    5. API design

    POST/api/blocks

    Upload a chunk if not already present (by hash)

    POST/api/files

    Commit file = ordered list of chunk hashes

    GET/api/changes?cursor=

    Long-poll for namespace changes

    6. High-level design

    What each box in the reference diagram is responsible for:

    Sync Service — Long-poll/WebSocket; tells devices what changed.
    Block Service — Handles chunk upload with dedup check.
    Metadata DB — file → ordered chunk list, versions, namespace.
    Notification Bus — Pub/sub of "namespace dirty" events.
    Block Store (S3) — Content-addressed chunks (key = hash).
    Dedup Index — hash → exists? to skip duplicate uploads.

    7. Data model

    Metadata DB: files(id, path, version, chunk_hashes[]), namespaces(user, cursor)
    S3: sha256(chunk) → bytes— Content-addressed

    8. Deep dives

    Chunking + content-addressed dedup

    Files are split into ~4 MB chunks; each chunk's SHA-256 is its ID. Before uploading, the client asks "do you have these hashes?" and only uploads the missing ones. Because the hash is the identity, identical chunks (across files and users) are stored once — huge bandwidth and storage savings. Editing a 1 GB file changes only the affected chunks.

    Metadata vs block separation

    Two services: metadata (file → chunk list, fast and transactional) and blocks (bulk bytes in S3). This lets metadata updates be quick and consistent while block storage scales independently and cheaply.

    Sync protocol

    Each device holds a cursor. The sync service long-polls / pushes "namespace dirty" notifications via a pub/sub bus. On a change, devices fetch the new metadata, diff against local state, and pull only the missing chunks.

    Conflict resolution

    When two devices edit the same file offline, use per-file versioning (vector clock or version number). On conflict, keep both as "conflicted copies" or apply last-writer-wins, surfacing the conflict to the user rather than silently losing data.

    9. Bottlenecks

    • Metadata hot path → shard by user/namespace
    • Dedup index lookups → Redis/bloom filter
    • Sync fan-out → pub/sub with per-namespace channels

    10. Trade-offs

    Fixed or content-defined chunks

    Choose: Content-defined chunking (rolling hash, for example ~4 MB average) so an insert at the start of a file shifts only one chunk boundary.

    Instead of: Fixed-size chunks: simpler, but one inserted byte changes every following chunk hash.

    Delta sync is the main bandwidth saver; fixed chunks lose it on any insert.

    Cross-user deduplication

    Choose: Dedupe within a user or team by default.

    Instead of: Global dedupe across all users: bigger savings, but the "do you already have this hash" answer leaks whether someone else stored a file.

    The privacy leak (confirmation-of-file attacks) outweighs the extra savings for most products.

    Conflict policy

    Choose: Keep both: the later writer's version becomes a "conflicted copy" next to the original.

    Instead of: Last writer wins: silent data loss.

    Users forgive an extra file; they do not forgive lost work.

    11. Failure modes

    Metadata commit fails after blocks upload

    Orphan blocks consume storage.

    Mitigation: Blocks are reference-counted by committed file versions; a garbage collector deletes unreferenced blocks after a grace period.

    Notification service drops a "changed" event

    A device stays out of date.

    Mitigation: Notifications are hints; devices also poll with their cursor periodically, so a dropped event delays sync but never loses it.

    Client bug deletes files

    Deletes sync everywhere.

    Mitigation: Version history and soft delete for 30+ days; rate-limit mass deletes and ask for confirmation.

    12. What interviewers probe

    Answer each one yourself before opening it.

    What does the metadata schema look like?Show a strong answer

    files(namespace_id, path, file_id), versions(file_id, version, block_list, size, modified_by), journal(namespace_id, seq, change). The journal sequence is the sync cursor.

    How do you shard metadata?Show a strong answer

    By namespace (user or shared folder), so a sync cursor is a single-shard range scan; huge team namespaces may need their own shard.

    How do shared folders sync for many users?Show a strong answer

    A shared folder is its own namespace mounted into each member's tree; members subscribe to its journal.

    How do you get eleven nines of durability?Show a strong answer

    Erasure coding across failure domains (for example 10+4 Reed-Solomon over different racks and zones), checksums on read, and background scrubbing to repair bit rot.

    Sign in to track this guide in your curriculum progress.