Repository navigation
Description
Found during the #649 review. Related: #651, #595.
Benchmark
| Field | Value |
|---|---|
| Metric | Per-connection sync bytes per 60 s sliding window (new histogram, bytes). Largest inbound sync message (beforeSync histogram, bytes). Over-budget closes (new counter). Signed-in editor closes in 7 days, each checked against its warn log (count). |
| Before | No per-connection series exists. One connection can add unbounded bytes below 16 MiB per message. Context, production, 2026-11-10 09:45Z, 14 days (read): ydoc_update_bytes p99 7,403 B; about 5 applied changes above 1 MiB. Largest head 12,518,586 B (2026-11-11, #750 read). Largest first sync in the #739 lab: 17,637,972 B. Local bytes accepted from one connection pushing 1 MiB Update frames for 60 s: not measured yet: agent in the repo, before Phase 2. Phase 1 histograms: not measured yet: maintainer, 7 days after the Phase 1 deploy. |
| After | not measured yet |
| Condition | Production at the deployed SHA. Each Phase 1 post states whether #740, #738 and #701 had shipped. e2e: make dev-local, named laptop, Bun 1.4.4, commit SHA recorded. |
| How to run | Phase 1: p50, p99 and the highest filled bucket of both histograms over 7 days, with the window count. Phase 2: scripts/e2e-versions.ts section [8], 5 runs. Production: the over-budget counter over 7 days. |
| Pass rule | e2e [8]: the over-budget connection is closed 1 of 1, and the counter rises by 1. The control has 0 closes. A 15 MiB offline diff syncs once, with 0 closes. The check fails with the budget removed. Production: every over-budget close in 7 days is checked against its warn log, and 0 are signed-in editors. Stop rule: a signed-in editor that trips it means the budget is too low; set WS_SYNC_BUDGET_BYTES=0 as Rollback says. |
| Guard | The content-safety list below. The first SyncStep2 and Update after each connect never trip the budget, so a returning editor cannot loop on close, reconnect and resend. |
| Owner | Agent in the repo: both phases and the e2e checks. Maintainer: the 7-day read, the limit post and the deploy. |
| Claim | Security verification: "One connection can add at most <limit> B per 60 s (was unbounded below 16 MiB per message). Signed-in editor closes: 0 in 7 days." |
Problem
#651 restores a 16 MiB cap on one inbound WebSocket message. It does not bound how much one connection can add to a document over many messages. #651 is live: maxPayload: MAX_WS_MESSAGE_BYTES at apps/hocuspocus.server/src/hocuspocus.server.ts:605 (checked at 0b302b222).
beforeHandleMessage in apps/hocuspocus.server/src/hocuspocus.server.ts checks messages before they apply. It refuses awareness frames over 64 KiB and client BroadcastStateless frames. onStateless drops relayed payloads over 64 KiB. Sync updates have no budget below the message cap.
A loaded room's memory grows with its document size, and every replica that serves the room holds a full copy (apps/hocuspocus.server/CLAUDE.md §Runtime limits). Nothing sets a per-connection bound on that growth today.
Refuse-and-close can loop. A returning editor uploads its whole IndexedDB copy, up to the 16 MiB message cap (#740). If that is over the budget, the connection closes, reconnects, resends and closes again, so its offline edits never land.
Fix
Add a per-connection budget for sync update bytes, checked in beforeHandleMessage, in the same shape as the awareness budget.
Amended, maintainer ruling update 2026-11-10: measure first. Steps 2 and 3 ship first in measure-only mode, and step 1 no longer sets the budget. See the ruling update comment.
Clarified 2026-11-12: only step 2 ships in Phase 1, in measure-only mode. Step 3 refuses and closes, so it ships in Phase 2.
- Size it from real traffic first. Context only, per the ruling update of 2026-11-10. Read
ydoc_update_bytes(observed inonChange,src/hocuspocus.server.ts:218) from production: its p99 and its highest filled bucket. It is a histogram, so it holds no exact maximum. The first sync message after a connect can carry a whole document, and the largest stored head is 12,518,586 B (2026-11-11). It was 5,357,733 B on 2026-09-05. The Compress collab WebSocket frames with permessage-deflate, behind a flag #739 lab measured a 17,637,972 B first sync. The budget must stay well above all of them. - Count the bytes of each
MessageType.Syncmessage per connection, over a sliding window. Keep the count oncontext, like the other per-connection state, never in a module-levelMap. - Above the budget, refuse the message. Count it in a new counter, and seed it at 0 in
seedWsAlertSeries, as §Persistence requires. Log a warn withdocumentName,socketId, the bytes and the limit. Close that document connection, as the awareness budget does.
The two phases, per the rulings of 2026-11-10:
- Phase 1, measure only (Agent in the repo). A per-connection 60 s sliding-window sync-bytes histogram, and a
beforeSyncmessage-size histogram. Seed both at 0 inseedWsAlertSeries. Close nothing. The limit lives inWS_SYNC_BUDGET_BYTES, and 0 means measure-only. Buckets for both histograms: 1 KiB, 16 KiB, 256 KiB, 1 MiB, 4 MiB, 16 MiB, 32 MiB, 64 MiB. The old 1 MiB top bucket hid every large change in+Inf(note of 2026-11-10). The window histogram observes one sum per connection, when its window closes or the connection ends. - Phase 1 read (Maintainer). After 7 days, post the window and the limit here, with the window count. State whether Load the local IndexedDB copy before the collab provider connects #740, Clear y-tiptap position-migration maps on the server, so big pads load faster #738 and Take y-tiptap 3.3.0 with Tiptap 3.33.1 and drop the local y-tiptap patch #701 had shipped. Clear y-tiptap position-migration maps on the server, so big pads load faster #738 and Take y-tiptap 3.3.0 with Tiptap 3.33.1 and drop the local y-tiptap patch #701 shrink first syncs. If one of them ships later, read one more week before tightening.
- Phase 2, refuse and close (Agent in the repo). Switch on step 3, with the counter and the warn log. The first SyncStep2 and Update after each connect are exempt. The limit is also never below
MAX_WS_MESSAGE_BYTES(16 MiB) plus 1 MiB. - Phase 2 timing. If phase 2 cannot finish its 7-day read before the v2.3.7 ship, record here that phase 2 moves to v2.4.0.
Verification
- Add a control and a check to section
[8]ofscripts/e2e-versions.ts:- Control: a connection that stays under the budget keeps syncing.
- A connection that goes over it is closed, and the new counter moves by 1.
- A client with a 15 MiB offline diff syncs once, is not closed, and the counter stays 0.
- Run the check once without the budget. The close assertion must fail.
- After the deploy, the new counter must stay at 0 for a week. A real editor that trips it means the budget is too low.
Content-safety checks (Phase 2)
Run each check locally on a scratch pad that the builder creates and deletes after. Never type into /demo.
- Reload. Type, wait for "synced", reload. The text is there.
- Two browsers. Edit one pad in two browsers. Both end with the same text.
- Offline, then online. Type offline, go online. The server receives the text.
- Fast tab close. Close the tab 50 ms after typing, then reopen. The text is there.
- Replica restart while typing. No text is lost or doubled.
- Production, 7 days after the deploy. Failed store jobs,
document_persist_fallback_totaland the store dead-letter queue stay at 0. - After an over-budget close and reconnect, every edit made before the close is on the server.
Rollback
Phase 1 has no env flag: revert de0e10fc5. Phase 2: set WS_SYNC_BUDGET_BYTES=0 (measure-only) in the source .env and deploy. Never edit only .env.production, because the next deploy overwrites it.
Decisions
All three are ruled. No open decision is left (ruling of 2026-11-10, 11:42Z).
- Per connection or per document? Ruled 2026-11-10 (ruling): per connection. A per-document budget would let one connection lock every other editor out.
- Refuse and close, or switch the connection to read-only? Ruled 2026-11-10 (ruling): refuse and close, the same as the awareness budget. The provider reconnects, and the counter shows it.
- The window and the budget? Ruled 2026-11-10 (ruling, update, window): a 60 s sliding window per connection, plus a
beforeSyncsize histogram. The limit comes from a week of that data. Changed by the maintainer ruling update, 2026-11-10: set both from a week of measure-only data.
Activity
HMarzban commentedon Nov 3, 2026
Status, 2026-11-03. Nothing for this issue is built yet. Sync updates still have no budget below the message cap, and the #651 cap is not pushed either. What is left:
ydoc_update_bytesp99 and its highest bucket from production, and post the window and budget here;[8]checks with a sabotage run;HMarzban commentedon Nov 4, 2026
Maintainer ruling, 2026-11-04
Moved from milestone v2.3.0 to milestone v2.4.0, from the v2.3.0 open-issue review. Nothing is built yet. It needs a production
ydoc_update_bytesread before the build, and it came from the #649 review.The issue stays open. Its remaining items are unchanged.
HMarzban commentedon Nov 10, 2026
Maintainer ruling, 2026-11-10
Answer: "agree on all".
ydoc_update_bytesread: its p99 and its highest filled bucket. Post both values here before the build starts.HMarzban commentedon Nov 10, 2026
Note, 2026-11-10: what
ydoc_update_bytescan and cannot showRead from code. Two independent reviews and a direct check agree.
update.byteLengthinonChange(apps/hocuspocus.server/src/hocuspocus.server.ts:218-220). That is the Yjs change the server applied. It is not the WebSocket message.onChangeruns before the Redis-origin check.apps/hocuspocus.server/src/lib/metrics.ts:376). An applied change above 1 MiB lands only in+Inf, so the read cannot say how far above. The largest stored head was 5,357,733 B.beforeSyncalready sees each inbound sync message one to one (apps/hocuspocus.server/src/hocuspocus.server.ts:222), but it counts messages only, not bytes.Open question: how to size the budget
This reopens Decision 3 of the 2026-11-10 ruling, which sets the window and the budget from this read.
MAX_WS_MESSAGE_BYTES), with a wide margin. The "counter stays at 0 for a week" check then catches a limit that is too low. The risk: until the limit is raised, it closes real editors' connections.The step 1 read
Run in Grafana Explore on the Prometheus data source. Put one query in each row. In each row's Options, set Type to Instant and Format to Table.
How to read them:
lewhose count equals the+Infcount. Sortleas numbers. Prometheus 3 may print it as64.0or1.048576e+06.increase()then loses most of what a new process recorded before its first scrape, up to 30 s. That is the reconnect burst.max_over_timekeeps it. If the third shows a higher filled bucket than the second, trust the third.HMarzban commentedon Nov 10, 2026
Maintainer ruling update, 2026-11-10
Measure first. Answer: "yep". This changes Decision 3 of the 2026-11-10 ruling above.
ydoc_update_bytesread in the 2026-11-10 note still runs, as context only. It records applied changes, not the bytes a connection sends, so it does not set the limit.Open before the build starts (owner: the maintainer):
beforeSync, so the largest real message is known.Nothing is built yet.
HMarzban commentedon Nov 10, 2026
Read, 2026-11-10:
ydoc_update_bytesover 14 daysContext only, under the ruling update of 2026-11-10. This read does not set the budget, because the histogram records applied changes, not the bytes a connection sends.
Applied changes per bucket, 14 days, from
sum by (le) (increase(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d])). Buckets are cumulative.+Inf)+Inf. About 5 applied changes in the 14 days were bigger than 1 MiB. The histogram cannot say how big they were.sum by (le) (max_over_time(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d]))also shows 5 above 1 MiB (98,379 in+Inf, 98,374 at 1 MiB). Its totals are lower, probably because a replica that reuses an old IP continues the same series after a reset.max_over_timethen keeps only the larger part. That cause is inferred, not checked.sum by (le) (max_over_time(document_snapshot_bytes_bucket[14d]))counts 1,327 stored snapshots. 161 of them were between 4 MiB and 16 MiB, and none were above 16 MiB.What this means for the budget, as context:
Raw data:
{"at":"2026-11-10T09:45:21Z","p99_bytes":7402.8,"increase_14d":{"64":108954.8,"256":109594.8,"1024":111067,"4096":112243.1,"16384":113517.2,"65536":113604.2,"262144":113696.2,"1048576":113718.2,"+Inf":113723.2},"max_over_time_14d":{"64":95383,"256":95947,"1024":96990,"4096":97626,"16384":98263,"65536":98317,"262144":98364,"1048576":98374,"+Inf":98379},"days_of_data":15,"ws_processes_14d":11,"snapshot_max_over_time_14d":{"256":0,"1024":31,"4096":126,"16384":463,"65536":749,"262144":899,"1048576":963,"4194304":1166,"16777216":1327,"67108864":1327,"+Inf":1327}}HMarzban commentedon Nov 10, 2026
Maintainer ruling, 2026-11-10
This answers the window question left open by the ruling update above. Answer: "agree your recomondation."
beforeSync, so the largest real message is known.No open decision is left on this issue.
HMarzban commentedon Nov 11, 2026
Maintainer ruling, 2026-11-11: kept in v2.3.6
The ruling of 2026-11-04 moved this issue to v2.4.0. It sits in v2.3.6 again, since the bulk move of 2026-11-09. That move stands: Phase 1 is measure-only, so it gathers the evidence for the limit, and its read gates Phase 2.
Two facts in the 2026-11-03 status are stale:
maxPayload: MAX_WS_MESSAGE_BYTESatapps/hocuspocus.server/src/hocuspocus.server.ts:605(checked at0b302b222).The body now holds the two phases, a Benchmark section and the standard content-safety list. It adds a rule against a close-and-resend loop for a returning editor, and a rollback through
WS_SYNC_BUDGET_BYTES=0.HMarzban commentedon Nov 12, 2026
Moved to v2.3.7, 2026-11-12 (maintainer ruling). v2.3.6 ships on 2026-11-14 with only finished, measured work, and the work and its measurement take longer than the cut allows. The before values and the plan in this issue stay as they are.
feat(hocuspocus): measure sync bytes per connection
HMarzban commentedon Nov 16, 2026
Measured, 2026-11-16: phase 1 built (measure only)
Commit
de0e10fc5. Phase 1 only: nothing refuses or closes.What changed
apps/hocuspocus.server/src/extensions/syncBytes.extension.ts, in the collaboration server's extension list.ws_sync_message_bytesobserves the payload of each inbound sync message inbeforeSync.ws_sync_window_bytesobserves the peak 60 s sliding sum of one connection, once a minute and once at the close. The window is 60 one-second buckets oncontext, never a module-levelMap. A burst across a minute boundary stays one sum. Phase 2 must refuse on this same rolling sum.onDisconnecttwice (Confirm and guard the double onDisconnect in Hocuspocus 3.4.4 #601).seedWsAlertSeriesseeds both at 0.WS_SYNC_BUDGET_BYTESand its warn log are not in phase 1. A warn above 0 was not ruled for phase 1, so phase 2 adds both with the refusal. Rollback for phase 1 is a revert ofde0e10fc5.CLAUDE.md§Runtime limits has one new bullet. A unit test pins the window: a burst across a minute boundary, the 60 s drop-off, bucket reuse, and the doubleonDisconnect. With the rolling sum sabotaged to read only the current second, 2 of 4 cases fail.Before and after
Server, 20,000 no-op Update frames (59 B), before and after alternatingServer, real provider)onDisconnectran twice, and the window counted onceThe cost is per sync message on one laptop. Production cost was not measured.
Gates
bun testsrc/extensions/__tests__/syncBytes.extension.test.ts: 4 pass.src/modules/document-conversionand this test: 73 pass, 0 fail.bun run typecheck(apps/hocuspocus.server): pass.Notes
beforeSync, not the frame header (the document name plus 2–3 bytes per message). Phase 2 inbeforeHandleMessagesees the whole frame, so it must count the same bytes as phase 1, or the budget from this read will not match.The 7-day read
Run the same three for
ws_sync_message_bytes. For p50, use 0.5.Left
(build): back. The 7-day window starts at that deploy.beforeHandleMessagewithWS_SYNC_BUDGET_BYTESand the warn log, the counter seeded at 0, the first-message exemption, the local 1 MiB-for-60 s before, and the[8]checks with a sabotage run.After the deploy
Deployed with
(build): front back(06f61ee44, run 43876524495, 2026-11-16 13:31Z, Build Images and Deploy Production success). The 7-day phase 1 window starts at that deploy. Read-only, production, 2026-11-16 13:48Z:ws_sync_message_bytes_count= 1,122 andws_sync_window_bytes_count= 19 onhocuspocus-server, so both histograms record. The 7-day read is due on or after 2026-11-23 13:31Z.docs(changelog): record the v2.3.7 work of 2026-11-16
HMarzban commentedon Nov 16, 2026
Review, 2026-11-16 (maintainer approved).
de0e10fc5. Phase 2 setsWS_SYNC_BUDGET_BYTES=0.HMarzban commentedon Nov 17, 2026
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).
HMarzban commentedon Nov 24, 2026
Maintainer ruling, 2026-11-24
Split Phase 2 (
WS_SYNC_BUDGET_BYTESrefusal and close) into #836 (v2.4.0).#659 stays in
v2.3.7only to post the matured Phase 1 7-day PromQL read (2026-11-16 13:31Zto2026-11-23 13:31Z), then closes (unblocking #649).Next step: post the Phase 1 7-day read and close #659.
HMarzban commentedon Nov 24, 2026
Measured, 2026-11-24: Phase 1, 7 days in production
Question: What sync-byte distribution did production observe over the first 7 days of Phase 1 telemetry?
Setup. Production WebSocket replicas, read-only Prometheus
increase()over[604800s]ending2026-11-23T13:31:00Z(7 days after the2026-11-16 13:31Zdeploy06f61ee44). Neither #740, #738, nor #701 had shipped during this window. Container restarts occurred during the2026-11-18host move and11-21..23deploys;increase()accounts for counter resets.Result (exact 7-day Prometheus reads):
n)16 MiBws_sync_window_bytes(60 s sliding window)2,617.364,526.97B (4.4KiB)3,951,110.18B (3.77MiB)4 MiB < x <= 16 MiB(23.0windows)0(32 MiBand64 MiBgain0)ws_sync_message_bytes(beforeSyncframe)51,984.92534.72B13,990.12B (13.7KiB)4 MiB < x <= 16 MiB(23.0messages)0ws_sync_window_bytesbucket cumulativeincrease():<= 1 KiB:989.13,<= 16 KiB:2,390.33,<= 256 KiB:2,518.34,<= 1 MiB:2,553.35,<= 4 MiB:2,594.36,<= 16 MiB:2,617.36.Not measured. Client-side retries after an over-budget close (Phase 2 work).
Verdict: Close #659 per the
2026-11-24ruling. Phase 2 (WS_SYNC_BUDGET_BYTESenforcement) continues in #836 (v2.4.0). Recommended starting default for #836:32 MiB(2xheadroom above the4–16 MiBtop filled bucket, clipping0 %of observed 7-day traffic while bounding larger bursts).