Skip to content
Architecturepostgresqlelectric

Optimistic realtime with one source of truth

How PostgreSQL transaction identity, Electric Shapes, and TanStack DB can produce immediate interfaces without split-brain state.

WG

WISEPIM GTM-OS

Architecture

1 min read

Fast interfaces do not need a second authority

Optimistic UI is a presentation technique, not a second database. The browser can show the intended result immediately while the authenticated server remains the only owner of durable writes and PostgreSQL remains the source of truth.

The difficult part is settlement. A client must know which committed transaction corresponds to its optimistic mutation, and it must avoid letting an older realtime row overwrite a newer local or server-confirmed version.

Commit the reconciliation handle with the write

The write service returns the PostgreSQL transaction ID captured inside the same transaction as the domain change. TanStack DB attaches that identity to the pending mutation and waits for the authorized Electric stream to observe the matching commit.

A stable client mutation ID also makes retries safe. If a timeout causes the browser to submit again, the server returns the original receipt instead of applying the command twice.

mutation-result.ts
return {
  entity: committedEntity,
  clientMutationId: input.clientMutationId,
  transactionId: committedTransactionId,
}

Treat realtime as an authorized read path

Electric delivers committed changes through a same-origin proxy with fixed resource definitions. Membership is resolved on the server and the resulting Shape exposes only the columns and tenant rows needed by that feature.

This arrangement keeps responsibilities simple: services authorize and write, PostgreSQL commits, Electric distributes, and TanStack Query or TanStack DB owns browser server-state. Zustand remains available for ephemeral UI state that does not belong in the durable model.

  • Use fixed server-owned Shape definitions.
  • Return transaction identity from the committing transaction.
  • Preserve newer row versions during reconciliation.
  • Invalidate or update only the resource scopes affected by the write.
postgresqlelectrictanstack-db

Turn the ideas into a working foundation

Continue with the architecture and implementation guides, each linked back to its canonical source.

Read the docs