Skip to content

How Kovlet sync and conflict resolution work

Kovlet apps save changes on the device first and sync them when they can, so they keep working offline. This page explains how a change travels and what happens when two apps edit the same thing.

6 min readUpdated December 2026Markdown
On this page

At a glance

  • A write shows up in the app at once. Kovlet queues it and sends it to the server when there is a connection.
  • If two apps change different fields of a row, both changes are kept. If they change the same field, the latest change wins.
  • Every device ends up with the same data, whatever order the changes arrived in.
  • A reconnecting app downloads only what it missed. Resending a change never applies it twice.
  • Deleted rows go to the Trash. An owner can undo one app's changes or roll a whole space back.

The path of one write

The app never waits for the network. It changes its own copy of the data, then sends the change in the background.

  1. Local apply. The app calls, for example, expenses.update(row.id, { amount: 4.8 }). The client stamps the write with its HLCHybrid logical clock: a stamp on every write that orders writes to the same field across devices., applies it to the local cache, and adds it to the outboxThe client's queue of local writes not yet acknowledged.. The UI updates without waiting for the network.
  2. Send. When connected, the client sends outbox entries over one WebSocket, in mutate frames. A frame holds up to 500 mutations and 8 MiB. Each mutation has a unique mutation_id.
  3. Check and commit. The server checks the session's role, the schema and any rules. It merges each field by HLC. It writes the change to the space's oplog with the next seq, and records the user, machine and app that made it.
  4. Ack. The server answers with an ack listing each mutation as applied, duplicate or rejected. The client removes acknowledged entries from the outbox. Until then it keeps retrying.
  5. Broadcast. Every other open session on the space gets the change as a broadcast with its seq, usually within a second.

Because each mutation carries a mutation_id, resending it after a dropped connection never applies it twice.

Offline

Offline, steps 2 to 5 wait. The outbox is saved with the local cache, so queued writes survive a reload and are sent on the next connection.

  • In browsers, @kovlet/client keeps both in IndexedDB.
  • In Node the default is memory. Pass a persistent store such as SqliteStore to keep them across restarts (Node quickstart).

Hybrid logical clocks

To decide which change is "latest", Kovlet can't trust each device's clock on its own. Clocks drift. So every write carries a hybrid logical clock (HLC) stamp: the device's time plus a counter that keeps the order right when clocks disagree.

An HLC stamp looks like 1764314700123:00002:mobile-19c2, in the form wall_ms:counter:node_id:

Part and Meaning
PartMeaning
wall_msMilliseconds since the Unix epoch, 13 digits
counterA counter within the same millisecond, 0 to 65,535, 5 digits
node_idThe clock's id, one per client installation

How the stamps behave:

  • Stamps compare by wall time, then counter, then node id. So every replica orders any two writes the same way.
  • Each client's clock only moves forward, and it absorbs every stamp it receives. So an edit made after seeing another app's write always wins over it, even if the two devices' clocks disagree slightly.
  • The server rejects a stamp more than 5 minutes ahead of its own time (clock_drift). So one device with a wrong clock can't win every conflict.

The SDKs manage the clock for you. Clients written from scratch follow section 5 of the protocol reference.

How conflicts resolve

Kovlet merges changes field by field. Each field of a row is its own last-writer-wins register: a write changes a field only if its HLC stamp is newer than the one the field holds. Edits to different fields of the same row never conflict.

Situation and Result
SituationResult
App A edits expenses/42.category offline; App B edits expenses/42.notesBoth changes survive.
App A and App B edit expenses/42.amount concurrentlyThe write with the higher HLC stamp wins on every replica.
A write arrives with an older stamp than the field holdsThat field keeps its current value. Other fields of the same write can still win.
The same mutation arrives twiceThe second is reported as duplicate and changes nothing.
App A deletes a row; App B edits itThe row stays in the Trash with App B's field values merged in. It comes back only with restore.

Deletes are soft: a delete moves a row to the Trash. Editing a trashed row doesn't bring it back; only restore does. A row's Trash state is a separate register with its own stamp, so a stale delete can't hide a row restored later.

A file's metadata (path, content and Trash state) is one register per file.

Note Kovlet doesn't merge text within a field. If two people type into the same field at the same time, one value wins whole. Model data that many people edit at once as separate rows or fields.

Cursors and catch-up: seq and since_seq

The server numbers every accepted change in a Data Space: 1, 2, 3, … with no gaps. This number is the seq. A client keeps a cursor: the highest seq it has applied. That's how it knows what it missed.

  • First connection (since_seq: 0): the server sends a snapshot of the space, then live changes.
  • Reconnect (since_seq: <cursor>): the server sends only the deltas after the cursor, then live changes. The deltas include the client's own earlier writes. Applying an op twice is harmless.
  • The client saves the cursor with its data. So after a restart it catches up on what changed instead of downloading everything again.

Full replica or query subscriptions

An app can keep a copy of the whole space, or only of the rows it asks for. The sync protocol has two modes over the same /v1/sync WebSocket:

  • Protocol 1 keeps a full replica of each Data Space, one socket per space.
  • Protocol 2 carries many query subscriptions on one socket. These are nested queries with where, order, limit and cursors. The client holds only the rows its queries match, so it can work with a space larger than it could copy. It can also commit several writes atomically (all or nothing). End users always sync over protocol 2.

Both modes use the same HLC merge rules and the same outbox.

When the server rejects a write

The server can reject a single mutation without failing the others in the frame. What happens next depends on the reason:

  • quota_exceeded: the write stays queued and visible as blocked. It is retried on reconnect or once usage drops (plans and quotas).
  • internal, rate_limited: retried with backoff.
  • Anything else (schema_violation, forbidden, unknown_column, …): a schema or permission problem. The SDK rolls the local change back to the server's state and reports it through onError.

The errors and limits reference lists every code.

Undo and rollback

Every change in the oplog records its user, machine and app, plus the values before and after. So a space owner can repair damage from the console:

  • restore rows and files from the Trash,
  • undo one app's changes over the last N minutes,
  • roll the whole space back to an earlier seq or time.

A rollback is itself a new set of changes, so it can be undone too. How far back it reaches depends on the plan's history window.

Next

Ready to try it? Start free. No credit card, no API keys.

For coding agents: this page is also Markdown. Add .md to the URL or send Accept: text/markdown. The full reference is at /llms-full.txt.