Skip to content
clientdb

Search the docs

Find a page or section

Under the hood

Optimistic writes, conflicts and rejections

Pending writes are replayed on top of newer server data; conflicts resolve per field in commit order; refused transactions roll back cleanly.

Under the hood

You don't need this page to use clientdb. It explains how things work inside, for when you're curious or debugging. The short version is How it works, in 5 pictures.

When Bob renames a todo, the new title shows on his screen at once, before the server has seen it. Meanwhile Alice may assign the same todo to someone, and the server may refuse Bob's change. This page explains how his device keeps the screen right through all of that.

Server data plus pending changes

For every row, the device keeps:

  • the server base: the last version the server confirmed;
  • the stack of pending transactions that touch it, in the order they were made;
  • an optional draft of unsaved edits (see Edit, then save or revert).

The row your components read is the base with the pending changes replayed on top (and the draft, if any).

Text
base (revision 7)   { title: "Ship login", assigneeId: null }
pending tx A        set title = "Ship login page"
visible             { title: "Ship login page", assigneeId: null }

server sends revision 8 { title: "Ship login", assigneeId: "alice" }   ← Alice assigned it
visible             { title: "Ship login page", assigneeId: "alice" }  ← A replayed on top

Replaying on newer data

When a newer base arrives, pending changes are replayed on top of it, so a local edit stays visible over other people's changes. When the server confirms a transaction, it leaves the stack; its effect is now part of the base.

A transaction is stored as the operations sent to the server. One step, expand, turns each operation into its effects on rows (deleting cascading children, setNull, restrict) and the steps to undo them. After a reload, the same step runs again on the stored outbox, so a pending delete hides its cascading children again.

Conflicts

Conflicts resolve per field, in the server's commit order: the last committed write wins. Two people editing different fields of the same todo never conflict. Two people editing the same field end up, on every device, with whichever transaction committed last.

At most once

Unconfirmed transactions are kept and sent again after reconnects, and across reloads when storage is on. The server records each (clientId, clientTransactionId) it applied, so a resend of an applied one is confirmed without effect. These records are kept for retention.transactions (90 days by default, at least retention.log; see The engine).

After that the server can't recognize a resend by its id. Every transaction carries the sync position the device had when it first tried to send it, and the server knows the highest position at which it applied a transaction whose id it deleted. A transaction it may have applied (its id is gone, and the device first tried to send it before that position) is refused with Stale and never applied twice. On Framecast: Olga promotes Max to admin on a train, the server applies it but its answer never arrives, and at her desk she makes Max a member again. If the laptop stays offline for longer than retention.transactions, its resend is refused with Stale, and Max stays a member; within that time it is confirmed without effect.

So every transaction is applied at most once, and confirmed or rejected, never dropped silently. A Stale rejection means a write stayed unconfirmed on a device, which kept trying to send it, for longer than retention.transactions.

Rejections

If the server refuses a transaction, the whole transaction is dropped from the pending stack, so its changes disappear from the screen. Then handle.confirmed rejects with a TransactionError, db.$onTransactionError listeners are called, and the transaction leaves the undo history. On the server, the onWriteRejected hook is called.

CodeMeaning
Forbiddena write rule refused it, or an update changed a field no update group allows; the message names the branches that failed, e.g. Forbidden: todo.delete (author: no, admin: no)
ReadOnlythe session is read-only: the route says readOnly: true, the session callback returned readOnly: true, or the session is anonymous
Validationan unknown field or model, a missing required field, a value of the wrong type, a validate() message, a generated field or a changed primary key
Conflicta create with an existing id, or a delete of a row that restrict relations still point at
NotFoundan update or delete of a row that doesn't exist, or that the viewer can't read (refused alike)
Stalea resend the server may have applied already, after it deleted the transaction's id (above); it is not applied again
AccessLostthe device lost read access to a row it had unsent changes on (issued on the device when the row is removed; the whole transaction is refused)
SchemaOutdatedthe device's schema differs from the server's

Write checks on the server

Each transaction runs in one Postgres transaction:

  1. A read-only session is refused before anything is read.
  2. Every operation is checked against the schema. Fields are sent by name, so a device built for another schema version can't write the wrong column.
  3. The write rules run against the row before and after: when must hold for both; each changed field must belong to an update group whose when holds; a changed restricted field must be readable by the viewer; enforce checks that fields hold their required value; validate runs last.
  4. Rows are written, their groups and any membership changes worked out, and one outbox entry appended (see Outbox and sequencer).

Serialization failures and deadlocks are retried automatically.

Server logic: $action

Some changes need server code: charging a card, sending an invite email. Those go through your own server action or API route, which writes with Prisma; the device then waits for the change to arrive with db.$action(…) (see Server logic with $action).

Collaborative text

Character-level merging of text fields is out of scope. Last write wins is a per-field default, not a global assumption, so it can be added later.