Three hours of office work, gone at tunnel exit
To simplify document saving, Julie merged a PR that replaced the incremental patch sync with a clean PUT /api/docs/:id sending the full document JSON whenever window fired an online event. We tested it at the office by toggling Wi-Fi off and on for three seconds, typing a sentence, and watching the network request succeed. Everything synced cleanly in 40 milliseconds.
Then Thomas boarded a four-hour TGV with the product spec open in two browser tabs. He edited section three on and off between stations. Meanwhile, back at the office, three people spent the afternoon reviewing and merging 24 revisions (rev: 95 through rev: 118). When Thomas's train cleared the final tunnel and his laptop found 4G, both tabs fired online at the exact same instant, sent PUT /api/docs/42 from rev: 94 with a fresh updatedAt timestamp, and silently overwrote three hours of the team's work. The gateway returned 200 OK to both requests.
How 412 inverted the data loss
Marc added an If-Match ETag check on the backend so stale PUT requests returned 412 Precondition Failed instead of overwriting newer revisions. Unit tests proved the server rejected outdated revisions cleanly, and we shipped it before Thomas's return trip.
Two days later, Thomas got back on the train. When his laptop reconnected outside Lyon, the server dutifully returned 412. Unfortunately, the client's catch block did what it had always done for 4xx responses: it cleared the local IndexedDB outbox and reloaded the server document so the UI would not stay stuck in an error state.
This time, the office kept its 24 revisions and Thomas lost four hours of train edits, including three tables he had rewritten from scratch. We had not fixed data loss; we had just flipped who got to be angry.
Five ways two tabs can race
First, replacing CRDT delta merging with a full-document PUT turns every reconnect into Last-Write-Wins, where whichever tab gets 4G last decides reality for everyone else. Second, without cross-tab leader election, two open tabs wake up on online and race to drain the same IndexedDB outbox, reading the same pending queue before either tab deletes it. Third, flaky 4G tunnels retry those requests without idempotency keys, duplicating or reordering mutations on the wire when a packet drops after reaching the server.
When Marc pulled the gateway logs from Thomas's first train trip, he could see both tabs' PUT requests landing 14 milliseconds apart, followed by three automatic cellular retries from tab two. Neither tab knew the other existed, and the server had no way to tell whether rev: 94 was an intentional revert or a stale snapshot arriving four hours late.
Electing one leader across tabs
The IndexedDB transaction trap
First try in the leader loop: we opened one readwrite IndexedDB transaction, read the pending ops, called await fetch('/api/docs/42/ops', ...), and then tried to delete the acked ops inside the same transaction.
// First try: auto-commits during await fetch(), throws TransactionInactiveError
const tx = db.transaction('outbox', 'readwrite');
const ops = await reqToPromise(tx.objectStore('outbox').getAll());
await fetch('/api/docs/42/ops', { method: 'POST', body: JSON.stringify(ops) });
tx.objectStore('outbox').clear(); // TransactionInactiveError
Under the IndexedDB specification, a transaction auto-commits the moment the event loop finishes a microtask with no pending IDB requests. By the time fetch() resolved 200 ms later, tx was already dead and clear() threw TransactionInactiveError, leaving the same ops in the outbox to be replayed forever. Reading the outbox in a short readonly transaction, running fetch() outside any transaction, and acknowledging the synced IDs in a second readwrite transaction solved it cleanly.
What stayed in the codebase
"Whoever gets network last wins" is not a synchronization strategy.
Thomas still rides the train with two tabs open, except now only one of them is allowed to talk to the server. When he arrived in Paris on Friday, all three tables he had rewritten on the TGV merged cleanly into rev: 119 without touching a single line from the office.

Top comments (0)