Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

Two people edit the same spreadsheet in a shared folder while one of them is on a plane. When the laptop reconnects, its sync client uploads its version, the other person's changes from the last three hours vanish, and support tickets about lost work grow every month.

How should the sync protocol detect and handle conflicting edits so no work is lost, and how can it avoid re-uploading a whole 2 GB file when only a few bytes changed?

The client committed blindly, so the last writer won and three hours of the other person's work vanished. Every commit must name the base version it was edited from, and the server accepts it only if that base is still the latest, a compare-and-set; otherwise it keeps both by saving the local edit as a conflicted copy and telling the person. To avoid re-uploading a 2 GB file, use content-defined chunking so an edit changes only nearby blocks, add deltas and compression for in-place edits, and keep version history as the safety net.

Background to this question

Lost work is the one failure a sync product cannot afford: people forgive a slow upload, but not a spreadsheet that quietly lost an afternoon of edits. This scenario is a favourite because it combines a concurrency bug, optimistic concurrency control, with an efficiency problem, how block boundaries are chosen. In this answer, we will reproduce the lost update, fix it with a base version check, decide what to do on conflict, see why fixed-size blocks force a full re-upload and how content-defined chunking avoids it, add deltas for in-place edits, and finish with version history.

1Reproducing the Lost Update

Both people start from version 12 of budget.xlsx. Ana, on the plane, edits her copy offline. Ben, on the ground, edits and saves three times, producing versions 13, 14 and 15. When Ana lands, her client uploads its blocks and commits her file as the new latest version. The server accepts it, because the commit only said 'here is the file', and Ben's three hours are buried as history that nobody knows to look at.

Step throughLast writer wins
Ana's laptop (offline)Sync serverBen's laptop
edit from v12 offline
commit edits
v13, v14, v15
reconnect: commit(file) with no base
store as v16, latest
notify: v16 replaces v15
Ben's changes disappear

7 messages in order. Play them, or step through them one at a time.

The server never asked what Ana's edit was based on.

2Fix: Commit Against a Base Version

The client always knows which version it last synced; that is the version it started editing from. So every commit names that base version, and the server accepts the commit only if the base is still the file's latest version. This is a compare-and-set, an update that succeeds only if the current value is still the one the caller saw, done inside the metadata transaction.

Java
-- commit(ns, path, new_blocks, base_version = 12)
UPDATE file_entries
SET latest_version = latest_version + 1
WHERE ns_id = 7 AND path = '/team/budget.xlsx'
AND latest_version = 12; -- 0 rows: latest is now 15, so this is a conflict
-- 1 row -> insert version 13 and a journal entry, return OK
-- 0 rows -> return CONFLICT {latest: 15}

Notice that the check and the update happen in one statement, so two commits racing on the same base cannot both win. Ben's commits always named the version he had, so they succeed one after another; Ana's commit names 12 while the latest is 15, so it is rejected instead of silently replacing his work.

3What to Do When a Conflict Happens

The service cannot safely merge two edited spreadsheets, images or design files; it does not understand their formats, and a byte-level merge would produce a corrupt file. So it keeps both. Ana's client renames her local file to something like budget (Ana's MacBook conflicted copy 2026-11-13).xlsx, uploads it as a new file next to the original, downloads Ben's version 15 into the original name, and shows Ana a notice.

That is not elegant, but no work is lost and a human, who understands the content, decides how to combine them. Applications that do understand their format, such as online document editors, can merge at a finer level, but a general sync service should never guess.

4Why the Whole 2 GB File Uploads Again

Now the second problem. With fixed-size blocks of 4 MB, block boundaries are at fixed byte offsets: 0, 4 MB, 8 MB and so on. If a user inserts 10 bytes near the start of a 2 GB file, every byte after the insert moves 10 positions later. Every block from that point on now contains slightly different bytes, so every hash changes, and the client uploads nearly all 512 blocks again for a 10-byte edit.

Content-defined chunking fixes this by choosing boundaries from the content itself. The client slides a small window, say 48 bytes, over the file and computes a rolling hash, a hash that can be updated cheaply as the window moves by one byte. Wherever the hash's low bits are all zero, for example the low 22 bits for an average block of 4 MB, it cuts a boundary, with minimum and maximum block sizes as guards. Because boundaries depend only on nearby bytes, inserting 10 bytes changes the block containing the insert, and the following boundaries reappear at the same content, just shifted. Only one or two blocks are new.

Inserting 10 bytes near the start of a 2 GB file
ChunkingBlocks changedUploaded
Fixed 4 MB blocksalmost all 512about 2 GB
Content-defined, average 4 MB1 or 2about 4 to 8 MB
Algorithms such as Rabin fingerprints, Gear or FastCDC provide the rolling hash.

5Deltas and Compression for In-Place Edits

Some applications rewrite a large file in place, changing a few pages of a database or virtual disk image. Content-defined chunking already limits the damage to the touched blocks, but each touched block is still sent whole. For large files we can go further: the client computes a binary delta between the old and new contents of a changed block, in the style of rsync, and sends only the delta, which the server applies to the previous version's block to rebuild the new one.

Each block is also compressed before upload, for example with zstd, which helps a lot for text, spreadsheets and logs and costs little for already-compressed media, where the client simply skips compression when it does not help. Together these turn a 2 GB re-upload into a few megabytes or less.

6Version History as the Safety Net

No conflict logic is perfect. A buggy client, an application that saves through a temporary file and rename, or a user who overwrites a file on purpose and regrets it can still lose work. So the service keeps every version for a set period, such as 30 days, and makes restoring easy: a file's history in the web and desktop apps, a 'restore this folder to yesterday at 9 AM' action, and visible deleted files. Because versions are just lists of block hashes, and unchanged blocks are shared, keeping history costs only the changed blocks.

7Follow-up questions

Couldn't the server merge changes automatically, like Git?

Git merges text line by line and still asks humans to resolve conflicts. A spreadsheet or image is a binary format where a byte-level merge produces garbage. Merging belongs in applications that understand the format; the sync layer's job is to never lose either side.

What if the client's base version is very old after weeks offline?

Nothing changes in principle: the commit names the old base, the server sees a newer latest version, and it is a conflict only if the file actually changed on the server since then. If the file was untouched, the base is still the latest and the commit succeeds.

Why not lock files while someone edits them?

Locks do not work for offline editing, because the person on the plane cannot hold or release a lock, and a forgotten lock blocks everyone. Optimistic concurrency, checking at commit time, suits a sync service; some products offer an advisory 'someone is editing' hint on top.

8Conclusion

In this answer, we found that the lost work came from blind commits where the last writer won. Naming the base version on every commit and accepting it only if the base is still the latest turns the update into a compare-and-set, and on conflict we keep both versions as a conflicted copy and tell the person. We saw that fixed-size blocks force a full re-upload after an insert, while content-defined chunking with a rolling hash changes only nearby blocks, and deltas and compression shrink in-place edits further. Version history for a period such as 30 days is the safety net for anything the logic misses.

What to say in the interviewAbout 80 seconds aloud

Open with

The bug is that the client committed blindly, so the last writer won and Ben's three hours of versions were replaced by Ana's offline edit. I would make every commit carry the base version the client last synced, and the server accepts it only if that base is still the file's latest version, as a compare-and-set in the metadata transaction.

Then, if they want more

Otherwise it returns a conflict. On conflict we keep both: the client saves its edit as a conflicted copy named with the device and date, pulls down the server version, and tells the person, because a sync service cannot safely merge arbitrary binary formats.

For the 2 GB problem, fixed-size blocks are the issue: an insert near the start shifts every later boundary, so every block hash changes and the whole file uploads. Content-defined chunking picks boundaries with a rolling hash over the content, so an insert changes only the nearby block or two.

For files edited in place, I would add binary deltas against the previous version and per-block compression such as zstd. Finally, I would keep version history for something like 30 days with an easy restore, because that is the safety net for any case the conflict logic misses.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How should the sync protocol detect and handle conflicting edits so no work is lost, and...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/32290af28d/