Skip to content
clientdb

Search the docs

Find a page or section

Under the hood

Outbox and sequencer

How writes get a gap-free, commit-ordered sync id without replication slots: a transactional outbox drained by one sequencer.

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.

The problem

If a client has applied sync id N, no action with an id below N may become visible later; otherwise a slow transaction that committed out of order would be skipped forever. A bigserial assigned inside the transaction follows start order, not commit order, so it can't be the sync id. A per-tenant counter row would serialize all commits. Logical replication slots are unsafe on Neon (idle slots are dropped, branch restores delete them).

The design

  1. Write side. Each write transaction appends its actions to clientdb_outbox, tagged with its transaction id. The outbox row takes effect only if the transaction commits.
  2. Per-row revisions. Every synced row gets a revision from one database sequence on each write. Concurrent writers of a row are serialized by the row lock, so a row's revisions strictly increase.
  3. Sequencer. It drains only committed outbox entries, assigns sync ids in commit order, and writes them to clientdb_log, one log row per transaction, in one database transaction. A transaction that commits later is sequenced later, so a hole can never appear behind a reader.
  4. Clients apply a row only if its revision is newer, so the order of actions inside a pass doesn't matter for correctness, and re-deliveries are no-operations.

Two ways to run it

Inline, inside requests (the default for the HTTP and Next.js handlers). Each push sequences right after it commits, and each pull sequences before it reads. Passes from any number of serverless instances are serialized by a transaction-scoped advisory lock, so they are safe to run concurrently and work through a transaction-mode pooler. A quiet deployment needs no background process at all.

In the background with engine.start(), woken by a Postgres notification when a write commits (from any process, and from the change triggers), with a 250 ms poll as the fallback. Use this with the WebSocket server. Other engine instances tail the log. You may then set inlineSequencing: false on HTTP handlers, though leaving it on is safe.

Background sequencerInline (serverless)
Throughput (benchmark)1,965 tx/s, 16 writers1,184 tx/s, 8 instances
Push p501.17 ms2.21 ms incl. sequencing
Process neededyesno

Inline passes are serialized by the advisory lock, which caps throughput below the background sequencer's batching.

Cost

Per write transaction: your rows, plus one small outbox row (sharing the commit, so no extra fsync), moved to the log by the sequencer. On Prisma tables there is also one clientdb_row upsert per synced row. The outbox insert also sends a pg_notify, delivered on commit, which wakes the background sequencer. No logical replication, no extensions.

Writes made outside clientdb take a similar path: the change triggers record the changed ids in a queue in the same transaction, and the engine drains that queue on the next pull or push (or right away with engine.start()), just like the outbox.

Bootstrap splicing

A bootstrap first reads the current head sync id, then opens a REPEATABLE READ snapshot. Everything sequenced at or before the head committed before the snapshot began, so it is in the snapshot; everything after gets a larger id and arrives as a delta. Actions seen twice are no-operations because of revisions.

Snapshot cache validity

With a snapshotStore, the sequencer records in clientdb_group_touch, in the same transaction as the log, which groups each batch touched. A cached per-group chunk is valid exactly until any action touches its group, so a cached snapshot is never stale.

Log retention

The log grows with every write until engine.prune() deletes entries older than retention.log (see The engine). A device that was offline longer than that can't be caught up from the log: the server answers with reset before applying anything the device sent, and the device bootstraps again, keeping its unsent changes, and sends them again (see Wire protocol). The ids of applied transactions, which keep a resend from applying twice, are kept longer: retention.transactions, 90 days by default (see At most once).