Run the Yjs Sync Protocol with Our Own Handler
- Deciders: Julian Bäume
- Date: 2026-10-12
Status
Accepted
Answers "Open: the sync server" in ADR-0028.
Context
ADR-0028 left two candidates for the sync server: our own handler on y-protocols, or Hocuspocus with its database and Redis extensions. Both were built against the Drizzle persistence. The Hocuspocus spike passed all five checks, so by ADR-0028's rule Hocuspocus would have won. Comparing the two finished implementations changed that.
Decision
Teams runs the y-websocket protocol itself, in src/yjs/sync-server.ts, embedded in Fastify on /ws/:threadId. It replaces @y/websocket-server.
- The upgrade handler checks the JWT or session and inbox access, and buffers messages that arrive meanwhile. A failed check closes with 4401, since
wsrejects 401 and clients saw 1006 and retried forever. - Each open document loads its stored state, then joins the document bus from ADR-0028. Editors don't wait for the join: while Redis is away they work on the stored state. Once joined, the document asks the other replicas for their state with a sync step 1 and tells them who is editing locally. Updates and awareness of local editors go out on the bus; what comes in is applied without being sent back.
- Redis pub/sub delivers at most once, and a lost message causes no error. Every open document repeats the sync step 1 every 30 seconds (
YJS_BUS_RESYNC_MS) and after a Redis reconnect, so replicas that drifted apart catch up. - Cluster mode uses sharded pub/sub. Classic pub/sub would also do at Teams' traffic.
- A clear empties the document in place on every replica that has it open, so no editor writes the old content back.
Consequences
- The wire format stays y-websocket's. The client keeps
WebsocketProvider, and open tabs keep working across the deploy. - No Redlock lock per store: the store's merge in the transaction is enough.
- Errors are logged through pino like everything else.
- We own the replica protocol. Two sync servers on one bus in Vitest, against an in-process bus and CI's Redis, plus a second pod in preview deployments, cover it.
- Carets of editors on a replica that dies stay until y-protocols' 30 second awareness timeout.
- Emptying a document walks Yjs internals (
_start,_map). That is the first thing to check on the move to yjs 14.
Alternatives considered
- Hocuspocus (branch
spike/hocuspocus). It passed the checks, but it changes the wire format, so every open tab needs a reload on deploy. It takes a Redlock lock on every store, logs some errors withconsole, and its Redis extension only uses classic pub/sub. Clearing needed the same in-place emptying as our own handler, so it saved no work there. - yhub: rejected in ADR-0028.

