Design Dropbox (File Sync)
medium 60 minKeep 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
| Block | Version 1 | Version 2 | Send? |
|---|---|---|---|
| 1 | 9f2c | ||
| 2 | b771 | ||
| 3 | 51aa | ||
| 4 | c3d8 |
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
- 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
criticalAn edit made offline disappears without a message.
Wrongfiles[path] = new_listRightif files[path].version != base: return "outdated" # keep bothfiles[path] = new_listA hash as a ticket
criticalAnyone who learns a name could claim the bytes, and replies reveal what others store.
Wrongif index.exists(h): skip_upload()Rightif owns(person, h): skip_upload()# equal blocks are merged on the serverMoving the cursor too early
criticalA crash between the two lines skips changes for ever.
Wrongcursor = afterapply(entries)Rightapply(entries)cursor = afterCounters that decide deletion
criticalA repeated or lost message leads to zero while the block is needed.
Wrongrefs[h] -= 1if refs[h] == 0: delete(h)Rightmark_if_in_no_live_version(h)# days later, scan againdelete_if_mark_unchanged(h)Bytes through the API servers
major84 Gb/s at peak would pass machines that add nothing.
WrongPOST /files (50 GB body)RightPUT 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.