AlgoViz

Design Dropbox (File Sync)

medium 60 min

Keep a folder the same on every device of a person and everyone they share it with.

See it run

Cut, send, commit, wake and fetch, one hop at a time. Then the moments when an edit, a block or a change could be lost.

Operations

Cut and compare

The device finds what changed by comparing fingerprints. No bytes have left it yet.

Step 1 of 5

A file is saved

BlockVersion 1Version 2Send?
19f2c
2b771
351aa
4c3d8

report.pdf, 4 blocks

The operating system reports the change. The device waits until the file rests.

Step 1: A file is saved

def names(path):    with open(path, "rb") as f:        while block := f.read(4 * 1024 * 1024):            yield sha256(block).hexdigest()new = list(names("report.pdf"))send = [n for n in new if n not in known_blocks]
Latency
about 0.1 s for 16 MB
Load
32 B a block

A version is a list of names. Everything else follows from that.

In the real world

The same file sync doing real jobs. Pick one and step through it.

10 GB in 2,500 blocks over a poor network.

Step 1 of 4

Four at a time

Our systemLaptopBlock svcblock indexS3blocks by hashMetadata svcPostgreSQLversion, journalKafka
stored
40%

blocks done

1,000

missing

1,500

sent again

0

uploading

Blocks travel side by side.

Step 1: Four at a time

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Last writer wins

    critical

    An edit made offline disappears without a message.

    Wrong
    files[path] = new_list
    Right
    if files[path].version != base:    return "outdated"      # keep bothfiles[path] = new_list
  • A hash as a ticket

    critical

    Anyone who learns a name could claim the bytes, and replies reveal what others store.

    Wrong
    if index.exists(h): skip_upload()
    Right
    if owns(person, h): skip_upload()# equal blocks are merged on the server
  • Moving the cursor too early

    critical

    A crash between the two lines skips changes for ever.

    Wrong
    cursor = afterapply(entries)
    Right
    apply(entries)cursor = after
  • Counters that decide deletion

    critical

    A repeated or lost message leads to zero while the block is needed.

    Wrong
    refs[h] -= 1if refs[h] == 0: delete(h)
    Right
    mark_if_in_no_live_version(h)# days later, scan againdelete_if_mark_unchanged(h)
  • Bytes through the API servers

    major

    84 Gb/s at peak would pass machines that add nothing.

    Wrong
    POST /files   (50 GB body)
    Right
    PUT  signed address of the storePOST /commits (a list of names)

Interview tips

  • Lead with the list

    • "A file is a list of blocks."
    • Contents never change, metadata is ordered.
    • Base version in each commit.
  • Numbers that decide

    • 28 Gb/s up and 56 down at peak.
    • 222,000 polls a second, or open connections.
    • 210 PB with coding, 450 with copies.
  • Anticipate failures

    • Broken upload: ask what is missing.
    • Lost wake-up: read by cursor.
    • Late commit: conditional mark.
  • Protect people

    • Never lose an edit.
    • Never reveal what others store.
    • Deletes wait.