Skip to content

Send save confirmations and access frames to local connections only #758

Done

Description

@HMarzban

Summary

Split from #704 ("Same change" paragraph). Every WebSocket replica subscribes to doc:*:saved and doc:*:access in Redis. Each replica that holds the room then calls document.broadcastStateless. The Redis extension also publishes that call to the room's channel, so every other replica sends the frame again. At R replicas, a client gets R copies of each frame. Production runs 2 WebSocket replicas.

This issue sends those frames to local connections only. It adds a subscriber-reconnect counter, because after the change a replica with a dead subscriber has no second path for its clients.

Status, 2026-11-16. Steps 1-7 are in 51773d6b9, deployed 2026-11-16 13:31Z. Left: the step 8 production scratch-pad checks and the 7-day store read, on or after 2026-11-23.

Why

Read from code at HEAD 0b302b222:

  • apps/hocuspocus.server/src/extensions/redis-subscriber.extension.ts:22 subscribes with psubscribe('doc:*:saved', 'doc:*:access'). Its handler calls document.broadcastStateless(...) for document:saved (:38). It calls handleDocumentAccessEvent for access events (:51).
  • apps/hocuspocus.server/src/lib/accessRealtime.ts:61, :76, :90 and :101 call document.broadcastStateless for the deleted, readOnly and private frames.
  • @hocuspocus/extension-redis beforeBroadcastStateless publishes every stateless broadcast to the room's channel (dist/hocuspocus-redis.esm.js:276-279). Each other replica then sends it to its own connections.
  • So at R = 2, each client gets 2 copies of each document:saved and access frame. This comes from code. The local count is not measured yet.
  • The webapp sets providerStatus to saved on each document:saved frame (apps/webapp/src/hooks/useYdocAndProvider.ts:232-235). So each copy is one more workspace store write.
  • After the change, a replica whose subscriber is down sends no saved frame to its clients. Their status stays on its last value until the subscriber reconnects. The subscriber is an ioredis client (apps/hocuspocus.server/src/lib/redis.ts:205-230). It logs ready and error. Nothing counts its reconnects.
  • apps/hocuspocus.server/src/extensions/statelessRelay.ts relays docTitle fleet-wide on purpose. It stays as it is.

What to change

All code steps are for an agent in the repo. Paths are under apps/hocuspocus.server/src.

  1. Local before, no code change. Add a --mode saved to apps/hocuspocus.server/scripts/redis-sync-probe.ts. It starts two production-mode WebSocket processes on one Redis and puts one client on each. The probe starts no worker today (redis-sync-probe.ts:291), and only the store worker publishes doc:<id>:saved. So either start a worker with spawnWorker from scripts/lib/prodModeStack.ts, or publish doc:<id>:saved straight to the probe's Redis 20 times. Record which one ran. It runs 20 saves, then counts document:saved frames per client per save. Add one access event (readOnly true, then false) and count those frames too. Run it at HEAD and post the counts as Measured, <date>.
  2. In extensions/redis-subscriber.extension.ts, replace document.broadcastStateless(payload) with a loop: for (const connection of document.getConnections()) connection.sendStateless(payload).
  3. In lib/accessRealtime.ts, make the same change at all four call sites. Keep the order: send the frame, then close or mark the connections.
  4. In lib/metrics.ts, add a counter ws_redis_subscriber_reconnects_total, seeded at 0 in seedWsAlertSeries. In lib/redis.ts, increment it on each subscriber ready event after the first one. Review the name with Measure stored head size, save and load phases by pad size, and loaded room bytes #750 and Show event-loop stalls on a panel and a non-paging rule, and measure cold-room reloads #729. This issue owns the name.
  5. Check that the Redis reconnecting... warn line in lib/redis.ts:71 also fires for the subscriber. If it does not, add one warn line on subscriber end, so Loki shows the gap.
  6. Rerun step 1. Then run the drop check. ioredis reconnects about 0.2 s after a single CLIENT KILL (lib/redis.ts:69-70), so one kill makes no real gap. Hold the subscriber down for 30 s instead. Put it behind a TCP proxy that the probe stops and restarts, or repeat CLIENT KILL in a loop. Make 3 saves during the gap, then let it reconnect. Record saved frames during the gap (expected 0) and after it (expected 1 per save), and the reconnect count.
  7. Gates: bun run --filter @docs.plus/hocuspocus typecheck and bun run --filter @docs.plus/hocuspocus test. Run scripts/e2e-versions.ts, which reads document:saved. Then stop at a review checkpoint.
  8. Maintainer. Commit through the commit-review skill and deploy with a (build): back tip. Keep the replica-restart check local. On production, run only the scratch-pad checks (reload, two browsers, offline, fast tab close) at a quiet hour, plus the 7-day store read. A production replica restart disconnects every user on it.

Content safety

This changes the sync path. The frames carry no document content, but the save status depends on them. Run every check on a scratch pad you create, in desktop Chrome and phone Safari. Delete the pad after. Never type into /demo.

  • Reload: every typed word is there.
  • Two browsers on the same pad, on different replicas if you can: each sees the other's edits, and each shows saved after a save.
  • Offline, type, then online: the text syncs.
  • Close a tab 50 ms after typing: the text is there after reopen.
  • Restart one WebSocket replica while typing: no text is lost. Local only.
  • Failed store jobs, document_persist_fallback_total and the store dead-letter queue stay at 0 for 7 days after the deploy. The lost-lock line Caught error during storeDocumentHooks stays at 0 in Loki.

Benchmark

Field Value
Metric (a) document:saved frames per client per save at R = 2 WebSocket replicas (exact). (b) Access frames per client per access event at R = 2 (exact). (c) ws_redis_subscriber_reconnects_total change after a 30 s subscriber drop. (d) saved frames per client during the drop and after it
Before (a) 2, (b) 2, from code at HEAD 0b302b222. Local count not measured yet: agent in the repo, step 1, before any code change. (c) no counter
After not measured yet
Condition Named laptop, Bun 1.4.4, two production-mode WebSocket processes on one Redis, Redis extension on (redis-sync-probe.ts). A worker from spawnWorker, or direct doc:<id>:saved publishes; record which. Both SHAs recorded
How to run bun --env-file=../../.env.local scripts/redis-sync-probe.ts --mode saved from apps/hocuspocus.server, 20 saves, one client per replica
Pass rule (a) 1 for every client on every save. (b) 1. (c) moves by exactly 1 after a proxy drop, or by the number of kills in a CLIENT KILL loop. Clients on that replica get saved and access frames for every save after it reconnects. (d) 0 during the 30 s gap, 1 per save after it. A run that counts 0 frames with the subscriber up is a broken probe, not a result. Stop rule: if any client gets 0 saved frames for a save while its subscriber is up, revert the commit
Guard The content-safety list above. stateless_relay_dropped_total{reason="broadcast-frame"} does not move
Owner Steps 1-7: agent in the repo. Step 8 and production reads: maintainer
Claim Internal: "Each editor now gets 1 save confirmation per save, down from 2 at 2 replicas."

Related

#704 (split from; the empty-save skip stays there). #736 (the status chip reads saved). #705 (directed Redis sync replies, same Redis extension). #363 (parent of #704).

Activity

  1. added this to the v2.3.6 milestone on Nov 11, 2026
  2. added
    Performance
    on Nov 11, 2026
  3. modified the milestones: v2.3.6, v2.3.7 on Nov 12, 2026
  4. HMarzban commented on Nov 12, 2026

    @HMarzban
    CollaboratorAuthor

    Moved to v2.3.7, 2026-11-12 (maintainer ruling). v2.3.6 ships on 2026-11-14 with only finished, measured work, and it waits on an earlier result or a gate read. The before values and the plan in this issue stay as they are.

  5. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-16

    Steps 1 to 7 are committed: the probe mode in bd9c845c5, the code in 51773d6b9. Step 8 is the maintainer's.

    The 2026-11-12 move note exists. It names no result that gates steps 1 to 7, so all of them ran.

    What changed

    • scripts/redis-sync-probe.ts has a new --mode saved. It publishes doc:<id>:saved straight to the probe's Redis 20 times. No worker ran. Then it sends one access event (readOnly true, then false). It counts the frames each client gets.
    • In both the before and the after runs, replica A reaches Redis through a small TCP proxy. For the drop check, the proxy refuses only A's save subscriber for 30 s. That subscriber is the client whose psubscribe names doc:*:saved.
    • src/lib/accessRealtime.ts exports sendToLocalConnections. It loops document.getConnections() and calls connection.sendStateless. The four access call sites and the document:saved send in src/extensions/redis-subscriber.extension.ts use it. The order is kept: send, then close or mark.
    • ws_redis_subscriber_reconnects_total (unlabelled) is in src/lib/metrics.ts. It is seeded at 0 in seedWsAlertSeries. src/lib/redis.ts adds 1 on each subscriber ready after the first one. The name follows the ws_ prefix of Show event-loop stalls on a panel and a non-paging rule, and measure cold-room reloads #729's ws_disconnects_total.
    • Step 5: the Redis reconnecting... warn already fires for the subscriber, through the shared retryStrategy. A logged it 17 times in the 30 s gap, beside 16 Redis subscriber error lines. So no end warn was added. Note: the warn carries redis: "sync", the same label as the main client.

    Before and after

    Condition: Apple M5 Max, Bun 1.4.4, HEAD 3bd5ca1c0. Two production-mode WebSocket processes ran on one local Redis (OrbStack, DB 13), with the Redis extension on. One client was on each replica. The saves were direct doc:<id>:saved publishes, with no worker. Before: 1 recorded run and 1 pilot, with no src change. After: 2 runs. Every run in each set gave the same frame counts. The pilot's room-channel count used the wrong frame type (Stateless, type 5), so only the recorded run's 44 is used. Before = HEAD 3bd5ca1c0 with no src change; after = the working tree on 3bd5ca1c0. The probe file differed between runs (sha256 1aef93bf… before, 2cf21774… after, from #703 edits); the saved mode is the same in both.

    Metric n per run Before After Change
    (a) document:saved frames per client per save 20 saves x 2 clients 2 (range 2 to 2) 1 (range 1 to 1) 50% fewer frames
    (b) readOnly frames per client per access event 2 events x 2 clients 2 1 50% fewer frames
    Broadcast publishes on hocuspocus:<id> 22 events 44 0 100% fewer publishes
    (c) ws_redis_subscriber_reconnects_total after a 30 s drop of A 1 drop no counter A 0 to 1, B 0 to 0 moves by exactly 1
    (d) saved frames during the gap 3 saves A 1, B 1 A 0, B 1 A drops to 0, as expected
    (d) saved frames after A resubscribes 3 saves 2 each 1 each 50% fewer frames
    Guard: stateless_relay_dropped_total{reason="broadcast-frame"} per run 0 to 0 0 to 0 did not move

    A resubscribed 648 to 650 ms after the proxy let it back in. Before the change, B's republish covered A during the gap. After the change, nothing covers A's clients until A's subscriber reconnects. The pass rule expects that.

    How to run, from apps/hocuspocus.server:

    bun --env-file=../../.env.local scripts/redis-sync-probe.ts --mode saved --redis-db 13 --log-dir <dir>
    

    Gates

    • bun run --filter @docs.plus/hocuspocus typecheck: pass.
    • bun run test: pass. 1251 pass, 7 skip, 0 fail.
    • bun --env-file=../../.env.local scripts/e2e-versions.ts: pass.
    • Prettier check on the 5 touched files: pass.
    • ESLint on the touched files, the final probe file included: pass, 0 warnings.

    Left

    After the deploy

    Deployed with (build): front back (06f61ee44, run 43876524495, 2026-11-16 13:31Z, Build Images and Deploy Production success). Read-only, production, 2026-11-16 13:48Z: ws_redis_subscriber_reconnects_total exists on all six processes and reads 0. Step 8's scratch-pad checks and the 7-day store read remain.

  6. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

    Review, 2026-11-16 (maintainer approved).

    • Added a Status line. Steps 1-7 are in 51773d6b9, deployed 2026-11-16 13:31Z. Left: the step 8 production scratch-pad checks and the 7-day store read, on or after 2026-11-23.
    • The CLAUDE.md Left item in the 2026-11-16 Measured comment is already done. apps/hocuspocus.server/CLAUDE.md:60 now names sendToLocalConnections (Send save confirmations and access frames to local connections only #758).
    • The "no alert reads ws_redis_subscriber_reconnects_total" note is dropped. No step asks for that alert.
  7. HMarzban commented on Nov 17, 2026

    @HMarzban
    CollaboratorAuthor

    Content-safety checklist. The shared checklist lives in release-product §Content-safety checklist. Run it for this issue, plus the extra checks in this body. Record each item as pass or fail in the Measured comment, as §Measured comment format says. The list in this body stays as it is (#762, ruling A).

  8. HMarzban commented on Nov 24, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-24: 8-day production guard

    Question: Did production pass the 7-day subscriber-reconnect and store-safety guards since the 2026-11-16 13:31Z deploy?

    Setup. Production Prometheus and live collab container logs, read-only, window ending 2026-11-24 07:00Z ([604800s] Prometheus increase() / max_over_time(), plus current container counters on e4d36ae0f).

    Result:

    Guard Result (2026-11-24) Pass
    ws_redis_subscriber_reconnects_total ([604800s] + live across 6 processes) 0 Pass
    jobs_total{queue="store-documents",status="failed"} ([604800s]) 0 Pass
    document_persist_fallback_total ([604800s]) 0 Pass
    max_over_time(queue_jobs{queue=~".*-dlq"}[604800s]) (all 3 DLQs) 0 Pass
    Live collab logs: Caught error during storeDocumentHooks (6 containers) 0 Pass

    Not measured. Dedicated local browser checks 1–5 were not re-run separately for #758 after #704 verified the local save path on 2026-11-22. Local probe rows (a)–(d) passed in the 2026-11-16 Measured comment (50 % fewer saved/access frames per client at R = 2).

    Verdict: All production and probe guards pass; closing #758.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions