A sync engine that never merges
Phones that work offline, a server that can't read anything, and money that has to add up to the colón. Most offline-first systems answer that with merging. I built one that never merges. Here's why, and what I traded for it.
Lorenzo Wynberg · November 20, 2026 · 8 min read
A shop owner records a payment against a customer's debt at a farmers' market with no signal. Her helper, on another phone and also offline, corrects the same debt. A week later both phones reconnect, to a server that can't read either change, because everything in Kuentas is encrypted on the phone before it leaves. And the books still have to add up to the colón.
That's the problem in one paragraph. Most offline-first systems answer it with merging: CRDTs, last-write-wins, vector clocks. They're good answers for documents and to-do lists. I built a sync engine that never merges. This is why, how it works, and what I traded for it.
The bet: a blended value is a wrong value
Merging is designed to keep both people's work. In a text document, that's exactly right. In a ledger, it's dangerous. If two people change the same debt offline, any automatic merge produces a number one of them didn't enter, or neither did. Last-write-wins is worse in its own way: one person's change disappears, and nobody is told.
So the rule I settled on is blunt:
- There's one log, and its order is the truth.
- The first save in the log wins.
- The save that lost is shown to the person who made it. Never blended, never silently overwritten.
An honest failure instead of a quiet one. Everything below is what it took to make that rule hold with phones that are offline half the time and a server that can't read a thing.
One transaction, so the phone answers instantly
Every write in the app goes through one function, save(). It changes the phone's local database (encrypted SQLite, running in WebAssembly) and, in the same transaction, writes an entry to an outbox.
On the phone
save()
One local transaction
sales
Coffee and empanada
₡2,500
_sync_outbox
Insert into sales
row before → after
Later, batched
Commit
≤ 200 saves · ≤ 1 MB
The comment at the top of the engine says it better than I can: a save is "in the records and on its way out together or not at all". The phone answers instantly, online or not, and there's no window where the books changed but sync doesn't know.
Push: one commit, one expected number
The outbox drains into commits: up to 200 saves each, capped at about 1 MB. Each commit is signed by the device that made it and names the one position in the log it expects to take.
The server accepts it only at that position. Appending is a primary-key insert on the book and the position, so if another phone got there first, the insert fails and the server answers 409. It never retries the commit and never merges it. The phone pulls what it missed, and tries again.
The rebase
That "tries again" is the heart of the engine. When a phone loses the race, it does three things:
- Undoes its own waiting saves.
- Applies the commits it just pulled.
- Redoes each of its saves, but only where every row the save touched is still exactly as the save found it.
Ana's phone
Debt · Doña Rosa
₡12,000
waiting in outbox
Server log
- #40
Luis's phone
Debt · Doña Rosa
₡8,000
waiting in outbox
Doña Rosa owes ₡10,000. Ana and Luis each update the debt on their own phone, with no signal.
A save that can't be redone isn't dropped. It goes on a list in the app, Cambios que no entraron in Spanish, "Changes that didn't make it" in English, with what it was and why, and two buttons: record it again, or remove it from the list.
Cambios que no entraron
Changes that didn't make it
Debt · Doña Rosa → ₡12,000
Someone changed the same thing first, from another device.
Two cases took me longer than the main path:
- A save that depends on a lost save is lost too, with its own reason. If the helper's correction didn't make it, a follow-up she made on top of it can't stand on its own.
- Two devices making the identical change. Two people fixing the same typo the same way shouldn't be told they conflicted. I added this later: if a lost save's rows ended up exactly as the save would have left them anyway, it's dropped silently. Nobody needs to be told they agreed.
Most of the time, none of this happens, because of how the data is shaped. Records get random IDs on the phone that creates them. Balances and stock are never stored; they're derived from the events. Two people recording two sales on two phones touch no shared row at all. Conflicts only happen on edits, and edits are versioned in an append-only history, one row per record and version.
Trusting a server I don't trust
The server orders commits, but nothing else about it is taken on faith.
#39signed ✓
prev: hash(38)
Ana's phone
#40signed ✓
prev: hash(39)
Luis's phone
#41signed ✓
prev: hash(40)
Ana's phone
Every commit names the hash of the one before it, so the log is a hash chain, and every phone checks each commit it pulls against the head it already trusts. A server that shows two phones two different histories, or skips a commit, is refused permanently, as a trust failure.
Bad commits don't stall anyone either. A commit with a record that won't open under the key its header names, or one that writes rows in someone else's name, is skipped whole and quarantined, so the next round moves on instead of tripping over it forever.
How the keys behind all of that work, who holds them and what happens when someone leaves, is its own post: Holding no keys. The customer version is on the Kuentas blog: How we encrypt your books.
The unglamorous parts
The design above fits on a napkin. These are the parts that made it work on real phones.
Backoff. A device syncs every 20 seconds while it's open. When a round fails, it waits 2 seconds, then 4, 8, 16, doubling up to 5 minutes, each wait randomized between half and all of that step, so a hundred phones coming back online don't arrive at once. Bringing the app to the front runs a round immediately.
Two queues. The phone has one connection to its database, and sync rounds are long. If a person opens a screen during a round, they shouldn't wait for it. So the work runs in two lanes: now for what a person is waiting on (a screen's read, a save) and background for sync. Between units of sync work, the engine yields, so a read posted meanwhile goes ahead of the rest of the round. Getting that yield right was its own small investigation: nested setTimeout(0) calls get clamped to 4 ms each, and Chromium runs a scheduler.yield continuation ahead of tasks already queued, so neither did what I needed. A message the worker posts to itself did.
Snapshots. A log that only grows means a new phone would replay everything. So every 500 commits, an owner's device writes an encrypted, signed snapshot of the whole database, and a new phone restores that and replays only what came after. I measured it before building it: in the prototype, five years of a busy shop took 30.6 seconds to replay on an emulated phone, and 1.7 seconds to restore from a snapshot. The shipped engine came in at a median of 922 ms.
Replay the whole log (prototype)30.6 s · 77 MB
Restore a snapshot (prototype)1.7 s · 15.6 MB
Shipped engine, median922 ms
The server going back in time. If the server is restored from a backup, the phones know a longer history than it does. A phone never takes the shorter log as the truth: an owner's device re-sends what the server lost, byte for byte as originally signed, while the operator holds a restore window open.
Dashed: commits the backup lost. The owner's phone re-sends them byte for byte, as originally signed, while the operator holds a restore window open.
Isn't this just a CRDT?
Partly, and on purpose. Sales, payments and expenses are new records with their own IDs, and balances are computed from them, never stored. Two phones adding records offline never conflict: the log simply gets both. That's the simplest CRDT there is, an append-only set, and it covers almost everything a shop does in a day.
What I left out is merging edits. If two people change the same debt offline, one to ₡15,000 and one to ₡12,000, a CRDT has to pick a number by itself. Last-write-wins quietly drops one. Adding the changes together gives a number nobody typed. Neither can know which amount is right, and neither can hold a rule like "stock never goes below zero", because merges happen without coordination. So for edits, the first one in the log wins and the other person decides what to do with theirs.
Encryption didn't force this, either: CRDTs can sync through a server that only relays ciphertext. The money did.
The trade-offs
Every sync design picks what it's good at. This one picks being right about money:
- When two edits collide, a person decides, not an algorithm. It happens rarely, and when it does, the list of changes that didn't make it says exactly what and why, with one tap to record it again.
- Concurrent edits to the same record go one at a time. For a shared document I'd want them merged. For a small business's books, two people rewriting the same debt at the same moment is rare, and getting it right matters more than getting it fast.
- The phones do the heavy lifting. Verification, decryption and the rebase all run on the device, which is what lets the server stay unable to read a thing. It's also why I measured first, and why, in my tests, a new phone had its books open in under a second at the median.
For money, I'll take an honest "this didn't make it" over a quiet wrong number every time.