Skip to content

Persist Yjs Documents in PostgreSQL and Sync Replicas over Redis

  • Deciders: Julian Bäume
  • Date: 2026-10-12

Status

Accepted

Refines ADR-0008. The dedicated Yjs WebSocket stays. This ADR replaces the persistence library and decides how replicas share open documents. Which server runs the sync protocol was decided after a spike, in ADR-0029.

Context

Shared drafts are Yjs documents, one per thread. Behind /ws/:threadId the backend uses @y/websocket-server for the sync protocol and y-postgresql to store updates in yjs-writings.

  • y-postgresql is unmaintained. It opens its own pg pool, and its table has no relation to threads, so documents outlive their thread. Clearing a document commits outside the caller's transaction.
  • @y/websocket-server is pinned at 0.1.1, because later releases need yjs 14. Teams moves to yjs 14 when Tiptap does.
  • Open documents live in one process. Two replicas would each hold their own copy and never exchange updates. Running several replicas is expected.

PostgreSQL NOTIFY (ADR-0023) can't carry Yjs updates: payloads are capped at 8000 bytes, and every keystroke would go through the notification queue that all writes share.

Decision

Persistence is Teams code on Drizzle and the regular pool. One row per document in a new table, with a foreign key to threads and ON DELETE CASCADE. The row is a snapshot, stored debounced, when the last editor disconnects, and on shutdown. Each store merges with the stored state inside the transaction, so replicas can't overwrite each other. Clearing runs in the caller's transaction.

Replicas sync open documents over Redis. Each replica with a document open subscribes to its channel and publishes the updates of its own editors. A replica that opens a document loads it from PostgreSQL and catches up over the channel. PostgreSQL stays the only durable store. Thread events and clients stay as ADR-0023 describes them.

A small document bus hides Redis, with an in-process implementation for a single replica and a Redis one, like the ThreadEventSource interface in ADR-0023.

Redis is configured like in other services. The chart takes Switchboard's redis values (hosts, prefix, mode, sentinelMasterId, db, tls, auth, secret override). Replica sync is on when redis.hosts is set. In cluster mode the bus may use sharded pub/sub; classic pub/sub is fine at Teams' traffic.

Tests: two server instances in one Vitest run against the CI Redis service, with persistence faked in memory, plus a second replica in preview deployments.

Migration

After the Drizzle migrations, migrate.ts folds every document from yjs-writings into the new table (in TypeScript, since SQL can't merge Yjs updates), skips documents of deleted threads, and drops the old table. Edits the old replica writes during the rolling update are lost for drafts nobody has open. Teams is not widely used in production yet, so we accept that. Scaling Teams to zero before the upgrade avoids it.

Open: the sync server

Our own handler on y-protocols, or Hocuspocus with its database and Redis extensions. A spike of at most three days decides. Hocuspocus wins if:

  1. it runs inside Fastify on /ws, without a second port;
  2. our auth and inbox access check fit into onAuthenticate, with the same close codes;
  3. the Drizzle persistence plugs into extension-database;
  4. two processes stay in sync through its Redis extension;
  5. Tiptap collaboration and carets work on @hocuspocus/provider.

The persistence work starts now and the spike runs against it. Outcome: our own handler, see ADR-0029.

Consequences

  • Removes an abandoned dependency and its second connection pool. Documents go away with their thread, and clearing is atomic.
  • Several replicas can serve the same document, and losing one only costs its editors a reconnect.
  • Several replicas without Redis split documents, and the backend can't tell. The chart has to say so.
  • A crash can lose the last seconds of edits since the last snapshot; editors still hold them and sync them back on reconnect.
  • The Redis path only runs with several replicas, hence the dedicated tests.

Alternatives considered

  • Keep both packages. Leaves an abandoned dependency and still rules out several replicas.
  • One row per update, as y-postgresql does. A write on every change, plus compaction.
  • Sticky routing instead of Redis. Istio can't hash on the path, and the hash ring moves documents whenever replicas change, splitting them until editors reconnect.
  • Yjs updates over NOTIFY. Payload limit and load on the shared notification queue.
  • Two-release migration with dual-write. Loses nothing, but costs an extra release and code that keeps writing y-postgresql's format. Right once Teams is widely used in production.
  • yhub. Needs Redis even for one replica, supports yjs 14 only, is beta and runs on its own port. Its AGPL-or-proprietary license also loses to Hocuspocus's MIT where both fit.