Skip to content
clientdb

Search the docs

Find a page or section

Under the hood

Postgres, Neon and serverless

Runs on any Postgres 14+, including Neon with scale-to-zero and branch restores. Pooling, the tables clientdb adds, tenancy, row-level security and the snapshot cache.

Any Postgres, Neon included

The engine needs no logical replication and no extensions: an outbox table, sequences and advisory locks are enough. Writes send a pg_notify on commit that wakes engine.start(), which listens on its own connection; inline sequencing doesn't need it. It works on Neon, including scale-to-zero and branch restores, and on any managed Postgres.

Serverless (Vercel and similar)

  • Use the Node.js runtime (the default for route handlers; pg needs TCP). On serverless hosts the engine keeps its pool small (5 connections, 10 elsewhere).
  • Request-time locks are transaction-scoped, so a transaction-mode pooler (Neon's pooled URL, PgBouncer) works for the route handlers.
  • engine.migrate() takes a session advisory lock, so it needs a direct connection: DIRECT_URL, Prisma's convention, or the directUrl option. A pooler URL is refused with a message saying so. Long-running WebSocket servers should use a direct connection too.
src/sync/engine.ts
import { createEngine } from "@clientdb/server";
import { permissions } from "./permissions";

// DATABASE_URL may be the pooled URL; migrate() uses DIRECT_URL
export const engine = createEngine({ permissions });
Terminal
DIRECT_URL=postgres://…direct… npx clientdb migrate src/sync/engine.ts

The tables clientdb adds

With Prisma, your rows stay in your tables, and npx clientdb-prisma models adds these models to schema.prisma, so your migrations create them. With a TypeScript schema, engine.migrate() creates them, plus one table per model.

TableHolds
clientdb_outbox, clientdb_log, clientdb_headeach write's changes, then the sync log in commit order (see Outbox and sequencer). clientdb_log.created_at lets retention.log prune it, and each entry's random nonce lets a server restored from a backup (or a Neon branch restore) tell positions from before the restore: devices past it load afresh, keeping their unsent writes
clientdb_transactionsthe ids of applied device transactions and the head each was applied at, so a resend applies once; pruned after retention.transactions, which moves clientdb_head.transactions_horizon (see At most once)
clientdb_rowfor Prisma tables: each row's revision, groups, field_readers (who reads its restricted fields) and a snapshot of its last synced values
clientdb_viewer_groupseach user's groups
clientdb_group_touchthe last write to each group, for the snapshot cache
clientdb_changesids of rows changed outside clientdb, filled by the change triggers

The snapshot in clientdb_row is the "before" of the next write made outside clientdb: when Prisma changes a member's role, the engine compares it with the snapshot to work out who gained or lost access. Rows of serverOnly models keep a snapshot too, without ever being sent to a device, because rules read them: a membership in a serverOnly table still moves rows when it changes.

After upgrading clientdb, run npx clientdb-prisma models again and migrate: clientdb migrate stops with an error naming any column the tables are missing.

Tenancy

Engine-owned tables are multi-tenant: every row, log entry and membership carries a tenant. Your session callback sets it, session: async (request) => { return { viewer: user.id, tenant: user.orgId }; }, and bootstraps, pulls, groups and the log are all scoped by it. Server writes name it: engine.systemWrite(operations, { tenant }).

Prisma tables use a single tenant, and you never pass one: the engine refuses a session or write naming another tenant, loudly, rather than serve the one set of rows to both. Teams or workspaces inside your app are separated by your permission rules.

Row-level security

With rowLevelSecurity: true, every engine transaction sets clientdb.tenant, and Postgres policies keep queries inside that tenant, as defense in depth.

TypeScript
const engine = createEngine({ permissions, rowLevelSecurity: true });

// your own queries, with the same guard
await engine.withTenant(orgId, async (client) => {
  await client.query("SELECT …");
});

Watch out

Run the engine as a non-superuser role: superusers bypass row-level security. It applies to engine-owned tables only.

Snapshot cache

Bootstraps can be served from an optional snapshotStore: MemorySnapshotStore, FsSnapshotStore, or your own (for example object storage). It holds the rows per (tenant, model, group), shared by every member of the group, and each entry is valid until any write touches the group.

TypeScript
import { createEngine, FsSnapshotStore } from "@clientdb/server";

const engine = createEngine({ permissions, snapshotStore: new FsSnapshotStore("/var/cache/clientdb") });

In the benchmark, an admin's 25,000-row bootstrap over HTTP took 196 ms cold and 92 ms from a warm cache.

What each write costs

Your rows, plus one outbox row, moved to the log by the sequencer. On Prisma tables there is also one clientdb_row upsert per synced row, about 0.5 to 1 ms per write. Writes made outside clientdb add one small row per changed row to clientdb_changes, inserted by the change triggers once per statement, so bulk updates stay set-based.