Skip to content

Sync Topology

Define how transactional data moves and converges between backend PostgreSQL and client-side pgwasm while preserving correctness and user experience.

Primary components:

  • PostgreSQL as durable system of record
  • pgxsinkit’s native sync core for shape-based replication: a Circuits engine that maintains each shape incrementally off Postgres logical replication, and durable-streams as the change transport
  • pgwasm for local/offline query and mutation workflows
  • pgxsinkit integration logic in packages/offline-data

pgxsinkit is treated as an external dependency (npm versioned) that the team can evolve with major-version breaking changes when needed.

  • Authoritative persistence
  • Cross-tenant integrity and policy enforcement
  • Final conflict adjudication rules where server authority is required
  • Local-first UX and offline continuity
  • Pending mutation queue and optimistic projection
  • Fast local reads for active learning workflows
  • Shape subscription and projection
  • Change transport and merge behavior
  • Conflict classification and reconciliation strategies

The read path is two api-mounted surfaces, registered together by apps/api/src/routes/read-path-routes.ts:

  • the control plane — POST /sync/v1/subscribe, POST /sync/v1/refresh, POST /sync/v1/release, GET /sync/v1/barrier — answers per-subject questions (which streams this subject may read) and mints a short-lived stream token. It verifies the caller’s bearer itself, through the same access-token verifier the REST gates use, and every answer is forced cache-control: no-store.
  • the edge — /stream/* — verifies that stream token and proxies durable-streams bytes. It carries no claims resolver and touches no database: the token is the authorization.

The two share the stream-token signing key (STREAM_TOKEN_SECRET) and nothing else. The control plane talks to the Circuits engine (CIRCUITS_ENGINE_URL); the edge talks to durable-streams (DURABLE_STREAMS_URL). Clients are configured with controlPlaneUrl and streamBaseUrl — derived beside the mutations URL in each app’s src/lib/env.ts as …/functions/v1 and …/functions/v1/stream.

Row filters are typed predicates, not SQL text. A registry entry’s shape.rowFilter.customPredicate is a claims-to-predicate function built from the p.* builders in @pgxsinkit/contracts over real Drizzle columns (p.eq/p.and/p.or/p.in(col, p.subquery(...))), with DENY_ALL_PREDICATE as the deny sentinel and null meaning “no filter, all rows”. The control plane compiles the predicate once per (shape, subject) and posts the compiled form to the engine, so no SQL text crosses the read path at all. Subqueries are first-class and uncorrelated — single-column, maintained incrementally by the engine, and shared across every shape that references them — so nesting depth is bounded by the engine’s own contract rather than by a probe of the transport’s grammar.

For syncable domain tables:

  • all writes go through pgxsinkit batch ingress (POST /api/mutations)
  • backend mode is bulk-plpgsql-artifact
  • admin/import/batch/system writes use the same batch mutation contract

Use category-specific sync behavior:

  1. Identity/context records Mostly server-authoritative; low-frequency updates.

  2. Membership and schedule records Server-authoritative with local cache and optimistic reads.

  3. Learner progress and drafts Client-first capture with eventual server merge.

  4. Assessment attempts and submissions Strong integrity requirements; local capture with deterministic commit semantics.

  5. Event logs Append-friendly with idempotency keys.

V1 baseline is deterministic server-apply-order last-write-wins (LWW) for mutable records.

Supporting rules:

  • Every client-submitted mutation is appended to pgxsinkit operation log
  • LWW ordering uses authoritative server apply sequence, not client clocks
  • Every syncable-table mutation (including admin/import/batch) is ordered in the same sequence space
  • Append-oriented tables remain append-oriented with idempotency controls
  • More advanced merge logic is deferred until correctness can be guaranteed without excessive complexity
  • Mutations should carry idempotency keys
  • Local write intent should be auditable
  • Retry logic must be deterministic
  • User-visible state should distinguish local pending vs confirmed

The system must provide:

  • Eventual convergence guarantees for supported mutation classes
  • Replay-safe mutation handling
  • Sync diagnostics for failed merges
  • Metrics for lag, retry counts, and reconciliation outcomes

Sync events should be mappable to standards outputs:

  • Membership changes to roster exchange flows
  • Attempt/finalization changes to xAPI/LRS and grade exchange
  • Qualification grants to CLR/Open Badge projections

See integration docs for event-level mappings.

  • Collection names, shape logic, and local conflict handling: packages/offline-data
  • Durable schema and constraints: packages/db
  • Sync registry and row-filter predicates: packages/db/src/sync-registry.ts, packages/db/src/schema/row-filter-claims.ts
  • Event and payload contracts: packages/domain
  • Mutation orchestration and policy: apps/api
  • Read-path control plane and stream edge: apps/api/src/routes/read-path-routes.ts
  1. Per-table list of LWW exceptions for v1.0 and v1.1
  2. Required offline guarantees for teacher workflows vs learner workflows
  3. Minimum telemetry set for sync health gates in CI/staging