Skip to content

Stop the Redis extension sending a full SyncStep2 for every edit in a shared room #595

Closed

Description

@HMarzban

Goal

In a room held on both replicas, every edit triggers a full sync handshake across Redis. Cut that cost without losing the self-healing it gives today.

Facts

Measured locally on 2026-10-23, unless marked "read from code".

  • @hocuspocus/[email protected] onChange calls publishFirstSyncStep for every local change (src/Redis.ts:387-390, read from code). Each peer replica answers with a full SyncStep2, which includes the whole delete set.
  • With 2 replicas, one keystroke costs 4 publishes. On a 5.36 MB edited fixture, that was 281,224 bytes published for a 22-byte edit.
  • With 3 replicas, it rises to 9 publishes.
  • The cost grows with deleted history, not with visible text. Only rooms held on both replicas pay it. Production runs 2 replicas.
  • Hocuspocus v4 coalesces SyncStep1 (Feat/redis pending flushes ueberdosis/hocuspocus#1135) and answers only the requester (fix(extension-redis): publish sync replies to the requester only ueberdosis/hocuspocus#1152). This repo stays on v3, because Bun cannot run v4.
  • So the fix is a Bun patch on extension-redis 3.4.4, through patchedDependencies. The repo has no patch yet. A subclass would need casts, because pubKey, encodeMessage and publishFirstSyncStep are TS private.
  • The catalog pins ^3.4.4 (package.json:134). Pin it to exactly 3.4.4 in the same commit as the patch.

Decision

Design: the lossless SyncStep1 throttle (ruling 4 on #575, 2026-10-24). Still post the two measurements below, before and after.

  1. Recommended: a lossless SyncStep1 throttle. A trailing window of about 100–250 ms per document. It saves less, but it keeps the per-edit self-healing that the accepted cross-replica hazard in apps/hocuspocus.server/CLAUDE.md relies on. Upstream #1135 uses the same lossless idea, but it collapses only within one event-loop turn (setImmediate).
  2. Update relay. Publish the update as a SyncStep2 frame, plus a trailing debounced SyncStep1. It saves the most. But Redis pub/sub delivers at most once, so a lost message leaves replicas apart until the next SyncStep1. That widens the hazard accepted on 2026-07-29, so its text must change in the same commit.

With either design, the SyncStep1 must fire on the trailing edge, after edits stop. Otherwise the accepted case "the last edit before a quiet period is lost" gets worse.

What to build

  1. The chosen design, as a Bun patch on extension-redis 3.4.4. All three docker/Dockerfile.bun files (webapp, hocuspocus.server, admin-dashboard) copy only listed files before bun install --frozen-lockfile. Bun fails that install when a patchedDependencies file is missing. So copy the patch file into every stage that runs that install. Prove it with one local docker build of apps/hocuspocus.server/docker/Dockerfile.bun.
  2. A small script under apps/hocuspocus.server/scripts/, using scripts/lib/e2eHarness.ts. It starts two Hocuspocus instances on one Redis, with one writer on each. RedisExtension loads only when NODE_ENV is production and REDIS is true (src/config/hocuspocus.config.ts:126). spawnServers sets development and starts one instance, so it cannot do this as it is. Production mode also turns on Postgres SSL (src/lib/prisma.ts:43) and refuses invalid tokens. Check at startup that RedisExtension loaded, or the publish counts read zero.
  3. With that script, count Redis publishes per edit, by subscribing to the document's channels. Run it before and after.
  4. With the same script, run a convergence check before and after. Kill one instance 30 times mid-edit, restart it, and count the edits that reach the other side. The 2026-10-23 baseline was 21 delivered and 9 lost.

Acceptance criteria

  • Publishes per edit, before and after, are posted here.
  • The convergence check, before and after, is posted here. The after number is not worse.
  • The hazard text in apps/hocuspocus.server/CLAUDE.md stays as it is (ruling 4).

Effort and order

Effort M. Ruling 4 chose the lossless SyncStep1 throttle, so this issue can start now. The production delete-set density on large edited heads is still unknown; #576 reads it.


Parent: #575. Source: the 2026-10-23 backend perf plan, refined on 2026-10-24.

Activity

  1. added this to the v2.3.0 milestone on Oct 24, 2026
  2. added
    Performance
    on Oct 24, 2026
  3. HMarzban commented on Oct 24, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-10-24

    Use the lossless SyncStep1 throttle, not the update relay. The accepted cross-replica hazard text in apps/hocuspocus.server/CLAUDE.md stays as it is.

    The acceptance criteria still hold: post the publishes per edit and the convergence check, before and after. The SyncStep1 must fire on the trailing edge, after edits stop.

    This issue is unblocked.

  4. HMarzban commented on Oct 26, 2026

    @HMarzban
    CollaboratorAuthor

    Built and committed on local main as 6301b369b. Awaits the maintainer's final push.

    Design: the lossless SyncStep1 throttle from ruling 4, as a bun patch on @hocuspocus/extension-redis 3.4.4. It publishes at once, then at most once per 250 ms per document, and always once more after the last change. The catalog pins exactly 3.4.4.

    Measured with the new apps/hocuspocus.server/scripts/redis-sync-probe.ts: two replicas, one Redis, one writer on each, on the 5.36 MB edited fixture. Unpatched and patched ran back to back.

    Publishes per edit

    Typing pace Before After
    one edit every 50 ms 4 publishes, 281,310 B 1 publish, 70,329 B
    one edit every 100 ms 4 publishes, 281,310 B 1.7 publishes, 119,558 B
    edits more than 1 s apart 4 publishes, 281,310 B 4 publishes, unchanged by design

    Convergence (30 server kills, 0 to 29 ms after an edit; delivered / lost)

    Run Before After
    1 22 / 8 18 / 12
    2 20 / 10 21 / 9
    3 20 / 10 16 / 14

    A trace showed that every marker edit in these rounds opens a new window. So it publishes at once, on the same path as before the patch. The spread is run-to-run noise: the handshake closes about 8 to 10 ms after an edit on this laptop, and after-run 3 shared the machine with a Cypress run. With 10 ms steps, both sides lost only the 0 ms and 10 ms rounds.

    The hazard text in apps/hocuspocus.server/CLAUDE.md is unchanged. A clean frozen install applies the patch, and the hocuspocus production image builds with it inside.

  5. HMarzban commented on Nov 3, 2026

    @HMarzban
    CollaboratorAuthor

    Status, 2026-11-03. 6301b369b is pushed and deployed with 8e25e83df. Production now runs the SyncStep1 throttle patch on extension-redis 3.4.4. Left:

    • Convergence after the patch summed to 55/90 delivered, against 62/90 before. Re-run the check on a quiet machine and post it here.
    • Or, maintainer: accept the run-to-run noise reading, then close.
  6. HMarzban commented on Nov 3, 2026

    @HMarzban
    CollaboratorAuthor

    Result, 2026-11-03. This is the #576 read for this issue. It ran on production on 2026-10-25, read-only. The head bytes were streamed out and decoded locally.

    Head bytes Clients Structs Delete-set ranges Deleted share of item length
    5,357,733 1 36,178 0 0
    1,802,652 1 92,341 0 0
    1,337,473 5 9,209 1 0
    1,334,558 1 9,063 0 0
    377,133 62 5,481 770 0.726
    • The three largest heads hold no deleted history. They look like single-author imports.
    • The largest edited head is 377 KB, with 62 clients and 770 delete-set ranges.
    • The local fixture is a 5.36 MB, heavily edited head on an arm64 laptop. It carries far more deleted history than any production head.
    • So the local before-and-after publish numbers are a worst case. Production rooms should send smaller SyncStep2 frames today. This was not measured.
    • This read does not touch the open item. The convergence re-run, or a maintainer ruling on the noise, is still owed.

    Can close now: no. The convergence check is still open.

  7. HMarzban commented on Nov 4, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-04.

    I ran the convergence check again, with the two sides alternating: unpatched, patched, unpatched, and so on. That gives 6 runs per side. Each run used scripts/redis-sync-probe.ts --mode convergence --rounds 30 --step-ms 1 --redis-db 7, on the 5.36 MB fixture, with two replicas and one Redis.

    • Both sides used the same probe file and the same installed packages.
    • On the unpatched side, a Bun preload served a copy of the package with the patch reversed. That copy is byte-identical to the 3.4.4 files from before the patch.
    • Each spawned server logged which file it loaded and how many patch markers it had.
    • The publishes mode gave the posted numbers again. Unpatched sent 4 publishes and 281,310 B per edit. Patched sent 1 publish and 70,329 B per edit. So the swap worked in the real servers.
    • This run used 1 ms steps; the 2026-11-03 runs used 10 ms. Both sides here used the same step, so the pair compares fairly.

    A run is "quiet" when no Cypress, headless browser or build process was seen, and the 1-minute load stayed under 7 on 18 cores. For U1 to P2, only the load before each run was checked.

    Run Side Proof (32 server loads) Load before Quiet or busy Delivered / lost
    U1 unpatched patchMarks=0 ×32 4.90 quiet 17 / 13
    P1 patched patchMarks=4 ×32 4.23 quiet 20 / 10
    U2 unpatched patchMarks=0 ×32 5.10 quiet 19 / 11
    P2 patched patchMarks=4 ×32 4.79 quiet 16 / 14
    U3 unpatched patchMarks=0 ×32 5.37 quiet 17 / 13
    P3 patched patchMarks=4 ×32 5.20 quiet 21 / 9
    U4 unpatched patchMarks=0 ×32 5.57 quiet 19 / 11
    P4 patched patchMarks=4 ×32 6.69 quiet 18 / 12
    U5 unpatched patchMarks=0 ×32 6.10 quiet 17 / 13
    P5 patched patchMarks=4 ×32 5.46 quiet 18 / 12
    U6 unpatched patchMarks=0 ×32 5.17 quiet 19 / 11
    P6 patched patchMarks=4 ×32 4.67 busy (short Cypress run) 21 / 9
    • Unpatched: 108 / 180 delivered. The mean is 18.0 per run, and runs range from 17 to 19.
    • Patched: 114 / 180 delivered. The mean is 19.0 per run, and runs range from 16 to 21. Without the busy run P6, it is 93 / 150, with a mean of 18.6.
    • Both sides lose the same rounds, from 0 to about 9 ms after the edit, and deliver from about 10 to 12 ms. This fits the earlier trace: each marker edit takes the patch's immediate path.

    Verdict: the patched side is not worse than the unpatched side. The ranges overlap, and the patched sum is a little higher. The busy run does not change this. The earlier before figure of 62/90 does not come back on this machine today: no three unpatched runs in a row went above 55/90. So compare the pair above, not the old pair of 62 and 55.

    The open item meets its acceptance, "the after number is not worse". This can close when the maintainer agrees.

  8. HMarzban commented on Nov 6, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-11-06

    Accepted: the after number is not worse. The alternating runs in the 2026-11-04 comment meet the acceptance item. The hazard text in apps/hocuspocus.server/CLAUDE.md stays as it is (ruling 4).

    Closing as completed. The local @hocuspocus/extension-redis patch stays while we run Hocuspocus 3.4.4.

  9. modified the milestones: v2.3.0, v2.3.6 on Nov 9, 2026
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions