File Sync: Deep Dives and Curveballs
You parked two things by name, where a chunk ends and edit against delete, and minutes thirty to forty-five go back for both. Between them comes the reason chunks and metadata get such different promises. After that, the interviewer has four curveballs.
Where a Chunk Ends
This is the question you left at about a megabyte. An insert at the start of a 2 GB file shifts every boundary of fixed-size chunks, and all 2,000 change. The saving in Scaling It held because that one byte overwrote another in place.
Content-defined chunks cut where a rolling hash of the last few bytes hits a chosen pattern. A rolling hash is one the agent can update a byte at a time as it moves along the file. A cut then depends on the bytes around it and not on its distance from the start, so a boundary follows the content, and an insert disturbs one or two chunks. The box below sets the two side by side.
Fixed-size chunks are cheap to compute and predictable to store. An overwrite in place changes one chunk, and an insert changes all the chunks after it. Use them when files are rewritten in place or only appended to.
Content-defined chunks cost a rolling hash on the device and vary in size. An insert changes one or two, however early it lands. Use them when people edit text, data files and uncompressed archives, where inserts are normal.
The size is a separate trade. At 1 MB, before deduplication, the index holds 400 billion rows and 20 terabytes. At 64 KB, it holds about 6 trillion rows and 310 terabytes, to save under a megabyte on each edit. The choice is about 1 MB, content-defined. The trigger for rethinking it is a workload of mostly small files, where chunking buys nothing. And a compressed file changes all over on any edit, so no chunker helps, and saying so is part of the answer.
Metadata Apart From Chunks
Chunks are 400 petabytes and never change. A hash names one content, so any copy is correct, and a reader can check it by hashing what it got. Metadata is 50 terabytes and is the only part that changes. The head of a file is one value that two devices fight over. So the promise goes only where it is needed: a linearizable conditional write on the head, and plain object storage for the rest, the split Chapter 4's Consistency Models That Come Up asks for.
That promise covers reads too. A device pulling after a signal reads from the partition's leader, because a copy that lags could answer "nothing new" and leave the change to the five-minute ask. The five-minute ask itself can use any copy. If the leader goes silent, the copies elect another, by the method of Chapter 5's Leader Election and Consensus. Commits wait until then, and agents hold their work as they would offline.
The order is the invariant: chunks first, commit second. A crash between them leaves chunks at zero that nothing refers to, which the sweep finds. The other order tells other devices about a file that cannot be read.
The sweep needs patience. A reference count lags its commit, and a chunk at zero may be one that an agent was just told exists. So assume the sweep takes only a chunk that has sat at zero for a day, and a hash check that finds the chunk restarts that day, as does the check a commit makes on its new hashes. An agent that stalls past that day, a laptop shut over a weekend, is told at commit that a chunk has gone, and it sends the chunk again, since it still holds the bytes. The wait holds at most one more day of new bytes, 200 terabytes on 400 petabytes.
Edit Against Delete, and the Rest
Here are the cases you parked. Edit against edit already has its answer, the conflict copy. Edit against delete: the edit wins and the file comes back, because a deletion can be repeated and a lost edit cannot be recovered. Move against edit: both apply, since they touch the path and the content of one stable file ID, which is why Scope kept the ID still. Move against move: the first commit wins, and the second device is told. A file added to a deleted folder: the folder comes back.
The check compares what a commit changes. An edit compares versions, and a move compares paths, so the two never collide. A delete is a version too. If it commits first, an edit based on the older version fails its check, and the agent commits the edit again on top of the delete. If the edit commits first, the delete fails, and that device gets the file back.
The rule in every row is that anything a user typed survives somewhere. Automatic merging is a per-format extra, because it needs a format whose merge is known, the limit Chapter 4's Concurrent Writes and Conflicts sets when it leaves such merges to rules an application knows.
Curveballs
"A shared folder with a thousand members." The partition key moves, because Chapter 3's Picking the Partition Key starts from the top access pattern, and that is now a folder. A folder that belongs to no one person becomes its own namespace, with its own journal and home, and each member's devices follow several journals. A commit signals 1,000 members at 3 devices each, half of them online: 1,500 pulls. Spread over ten seconds with jitter, a small random wait on each device, that is 150 a second. What it costs: permissions checked on every read, and a busy namespace is a hot partition, as Chapter 3's Hot Partitions describes, that needs a commit limit.
"Restore yesterday's version." Almost nothing to build. A version is a list of hashes, so restoring commits a new version whose list is the old one, and no bytes move in the store. The cost is keeping old chunks. Assume 30 days, at up to 200 terabytes of new bytes a day: at most 6 petabytes on 400.
"A buggy client deletes everything, and the sync spreads it." Deletes are versions, so nothing is destroyed for 30 days. A commit limit per device stops the spread: past an assumed 1,000 deletes in a minute, the commit holds and asks the user. The API enforces it, because the agent is what is broken, and it counts from the journal's last minute, so the limit needs no store of its own. It keeps refusing until the user confirms, and the held commits stay on the device. The journal numbers every commit, so recovery is "put this account back to position N": commit, for each file changed since N, the list it had then. What it costs: a real cleanup sometimes has to be confirmed.
"Now make it real-time collaborative editing." Say first that it is another product. The unit becomes a keystroke, the merge is automatic, and it needs a type that merges itself or one server ordering the operations. Chunks and conflict copies do not help. Keep file sync for files, run documents as a separate service that exports a snapshot into the folder, and put the choice back: "That's a different product from file sync. Do you want that design in the time left?"
How Each Level Answers
A mid-level answer gets whole files syncing, knows files should be chunked, and adds change signals when the polling load is raised. It handles the offline conflict with some prompting, often starting from last writer wins, and moves to keeping both once the interviewer asks what happened to the other edit.
A senior answer asks about offline edits in Scope and puts numbers on both prices: 27 minutes against under a second, 3 million polls against 100,000. It states the commit order and walks the failed commit. It raises what a hash alone must not unlock, and says when one version number is enough and what would change that.
A staff answer weighs a promise of "we cannot read your files" against the 120 petabytes of saving it removes, and leaves that choice to the product. It raises the spreading delete unasked, with a limit and a restore path. It counts conflict copies as a cost users feel, and asks how often they happen before designing further.
- Working the rolling hash's mathematics in the deep dive, when the interviewer wanted what a boundary does on an insert and how size prices the index.
- Answering the shared folder with more replicas. The problem is who owns the journal and who gets told.
- Starting to design operational transforms in the last five minutes, without having said that collaborative editing is a different product.
- A rule for every conflict case, and the rule is that what the user typed survives.
- The invariant named, chunks then commit, with what a crash on either side leaves behind.
- Which promise each store gives, and why the bytes need so much less than the row.
Knowledge Check
"A shared folder with a thousand members." What is the best first move?
- Add replicas to the owner's partition, to serve every member's reads
- Copy the folder into every member's account, and sync copies on commit
- Make the folder a namespace of its own, with its own journal and home
- Send the changed chunks down all the connections as soon as it commits
Why is restoring yesterday's version almost free to build?
- The object store keeps a daily snapshot of each account for a month
- A device still holds yesterday's copy, and it can upload that again
- Old versions are stored whole, so one object is copied back into place
- A version is a list of hashes, so a restore commits the old list as new
A buggy client starts deleting everything, and the sync is spreading it. What is your first move?
- Hold that device's commits past a limit, and ask the user to confirm
- Restore every account from last night's backup of the metadata
- Stop the signal tier, so other devices stop hearing about deletes
- Ignore all deletes from all clients, so that nothing can be lost
The interviewer says: now make it real-time collaborative editing. What does that change?
- Chunks shrink to about one keystroke each, and the rest of the design holds
- Conflict copies are made sooner, so that people can see each other's edits
- The unit is a keystroke and the merge is automatic, so it is another product
- The signal tier carries the whole document, so all editors see it at once
You got correct