Two offline edits to one note. Why not keep the latest?
Question
A notes app syncs one note between your phone and your laptop. Both go offline. At 10:05 you rename the note on the phone. At 10:09 you add a line on the laptop. When both are back online, the server keeps the edit with the latest timestamp.
- What goes wrong?
- Now a version is a pair of edit counters, (phone, laptop). Both devices start from (2, 4). A device adds 1 to its own counter on each edit. Give both edits a version, then write a rule that tells "came after" from "conflict".
Show hints (3)
- "WHAT EACH SAW"
When the laptop added its line, did it know about the rename? Does 10:09 being later change that?
- "ONE COUNTER EACH"
Compare (3, 4) and (2, 5) one counter at a time. Which is bigger in the phone slot? Which in the laptop slot?
- "THE RULE"
If every counter in X is at least the matching one in Y, X has seen all of Y's edits. What if each side is bigger somewhere?
Show checked explanation
Last-write-wins keeps the laptop's edit and silently drops the rename, which the laptop never saw. Use version vectors: the phone's edit is (3, 4), the laptop's is (2, 5). X came after Y if every counter in X is ≥ Y's and they differ. Here each is bigger somewhere, so they conflict: keep both and merge.
def relation(x, y): # how version vector x relates to y devices = x.keys() | y.keys() x_ge = all(x.get(d, 0) >= y.get(d, 0) for d in devices) y_ge = all(y.get(d, 0) >= x.get(d, 0) for d in devices) if x_ge and y_ge: return "same" if x_ge: return "after" # x has seen every edit y has if y_ge: return "before" return "conflict" # each has an edit the other never saw base = {"phone": 2, "laptop": 4} phone = {"phone": 3, "laptop": 4} # the rename laptop = {"phone": 2, "laptop": 5} # the new line merged = {"phone": 3, "laptop": 6} # bigger of each, then laptop + 1 for x, y in [(phone, base), (phone, laptop), (merged, phone), (merged, laptop)]: print(relation(x, y))
after conflict after after
Code run on CPython 3.12 against the versions above and every pair of small vectors. Checked by an editor other than the author.
Walkthrough
Each device edits its own copy, which always holds its own last save. An edit adds 1 to that device's counter:
| version | edit | clock time |
|---|---|---|
| (2, 4) | last synced note | 09:50 |
| (3, 4) | phone renames it, offline | 10:05 |
| (2, 5) | laptop adds a line, offline | 10:09 |
Compare (3, 4) with (2, 4). Every counter in (3, 4) is at least as big, so it came after (2, 4) and replaces it.
Compare (3, 4) with (2, 5). The phone counter is bigger on the left, the laptop counter on the right. Each holds an edit the other never saw. That is a conflict, also called concurrent edits. Concurrent here means "neither saw the other", not "at the same moment".
The rule:
- X came after Y when every counter in X is ≥ the matching counter in Y, and they are not equal. Drop Y.
- X and Y conflict when each has some counter bigger than the other's. Keep both.
To merge, the laptop shows both versions and saves the result. Its version takes the bigger counter of each, (3, 5), then adds 1 for the laptop: (3, 6). That is ≥ both in every counter, so it replaces both.
Timestamps can't do this: 10:09 is later, but the laptop never saw the rename. A clock tells you when an edit happened, not what the writer knew.
Common mistakes
- Getting the direction backwards. When every counter in Y is ≥ X's, Y came after X and X is the old one. Swap them and you delete the newer edit.
- Saving a merge without adding 1. If both devices merge and save (3, 5) with different text, they look like one version, and one merge is lost. Every save must raise the saver's counter.
- Sharing a device id. Restore a phone backup on a tablet, and both act as "phone". Both turn (3, 6) into (4, 6), so one edit looks like a repeat and is lost.
Follow-up questions
- "When is last-write-wins good enough?" When losing one of two concurrent writes does no harm: a "last seen" time, a cache entry, session data. Amazon's Dynamo paper describes a session store that uses it. For text that people typed, keep both versions and merge.
- "Fifty devices edit the same note. Is the vector a problem?" It gains a counter for every device that ever edited, stored with every version. Dynamo caps its vectors at about 10 entries, dropping the one updated longest ago. Then it can no longer always see that one version came after another. Some updates show up as false conflicts that someone must merge.
What a strong answer covers
Explains why the latest timestamp loses an edit. Gives (3, 4) and (2, 5) and the rule in the right direction. Keeps both on a conflict, and gives the merge a version after both.
Open in interactive practice deck →