Skip to content

Bound how much one connection can grow a collaboration document #659

Done

Description

@HMarzban

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.

  1. Size it from real traffic first. Context only, per the ruling update of 2026-11-10. Read ydoc_update_bytes (observed in onChange, 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.
  2. Count the bytes of each MessageType.Sync message per connection, over a sliding window. Keep the count on context, like the other per-connection state, never in a module-level Map.
  3. 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 with documentName, 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:

Verification

  • Add a control and a check to section [8] of scripts/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.

  1. Reload. Type, wait for "synced", reload. The text is there.
  2. Two browsers. Edit one pad in two browsers. Both end with the same text.
  3. Offline, then online. Type offline, go online. The server receives the text.
  4. Fast tab close. Close the tab 50 ms after typing, then reopen. The text is there.
  5. Replica restart while typing. No text is lost or doubled.
  6. Production, 7 days after the deploy. Failed store jobs, document_persist_fallback_total and the store dead-letter queue stay at 0.
  7. 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).

  1. 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.
  2. 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.
  3. The window and the budget? Ruled 2026-11-10 (ruling, update, window): a 60 s sliding window per connection, plus a beforeSync size 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

  1. added this to the v2.3.0 milestone on Oct 31, 2026
  2. added
    Security
    on Oct 31, 2026
  3. HMarzban commented on Nov 3, 2026

    @HMarzban
    CollaboratorAuthor

    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:

    • maintainer: read ydoc_update_bytes p99 and its highest bucket from production, and post the window and budget here;
    • then build the budget, the counter and the [8] checks with a sabotage run;
    • after deploy, the counter stays at 0 for a week.
  4. modified the milestones: v2.3.0, v2.4.0 on Nov 4, 2026
  5. HMarzban commented on Nov 4, 2026

    @HMarzban
    CollaboratorAuthor

    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_bytes read before the build, and it came from the #649 review.

    The issue stays open. Its remaining items are unchanged.

  6. modified the milestones: v2.4.0, v2.3.6 on Nov 9, 2026
  7. HMarzban commented on Nov 10, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-11-10

    Answer: "agree on all".

    1. Per connection, not per document. A per-document budget would let one connection lock every other editor out.
    2. Refuse and close, the same as the awareness budget. The provider reconnects, and the new counter shows it.
    3. The window and the budget come from the production ydoc_update_bytes read: its p99 and its highest filled bucket. Post both values here before the build starts.
  8. HMarzban commented on Nov 10, 2026

    @HMarzban
    CollaboratorAuthor

    Note, 2026-11-10: what ydoc_update_bytes can and cannot show

    Read from code. Two independent reviews and a direct check agree.

    • The histogram observes update.byteLength in onChange (apps/hocuspocus.server/src/hocuspocus.server.ts:218-220). That is the Yjs change the server applied. It is not the WebSocket message.
    • So it does not measure what the budget will count, which is the bytes of sync messages per connection:
      • A message that repeats content the server already holds records little or nothing, because Yjs emits no update when nothing changed.
      • It never sees the load from the database, or the server's own SyncStep2 to a client.
      • It also counts changes that no connection sent: edits relayed through Redis, REST content apply, restore and imports. Each replica counts a relayed edit, because onChange runs before the Redis-origin check.
    • Its top finite bucket is 1,048,576 B (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.
    • beforeSync already 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.

    1. Recommended: measure first. Build the per-connection count as planned, but ship it in measure-only mode. Record each connection's sync bytes per window in a new histogram, seeded at 0, and refuse nothing. After a week, set the window and the limit from that histogram, then switch the refusal on. The step 1 read below still runs, as context.
    2. Other: set the limit now from the known bounds (the 5,357,733 B head and the 16 MiB 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.

    histogram_quantile(0.99, sum by (le) (increase(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d])))
    
    sum by (le) (increase(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d]))
    
    sum by (le) (max_over_time(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d]))
    
    (time() - min(min_over_time(timestamp(up{job="hocuspocus-server"})[15d:1h]))) / 86400
    

    How to read them:

    • The first gives the p99 in bytes. A result of exactly 1048576 means the p99 is above 1 MiB, by an unknown amount.
    • The second gives counts per bucket for the window. Buckets are cumulative, so the largest change sits at or below the smallest le whose count equals the +Inf count. Sort le as numbers. Prometheus 3 may print it as 64.0 or 1.048576e+06.
    • The third is a cross-check for the largest bucket. A deploy starts new series, because each replica gets a new IP. 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_time keeps it. If the third shows a higher filled bucket than the second, trust the third.
    • The fourth gives the days of data that Prometheus still holds. The 4GB size cap can cut retention below 15 days. If it reads under 14, the window covers only that many days.
  9. HMarzban commented on Nov 10, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling update, 2026-11-10

    Measure first. Answer: "yep". This changes Decision 3 of the 2026-11-10 ruling above.

    • Build the per-connection count of sync bytes as planned (Fix step 2), but ship it in measure-only mode first. It records each connection's sync bytes per window in a new histogram, and it closes nothing.
    • After a week of production data, post the window and the limit here, read from that histogram. Then switch the refusal on, with the counter and the warn log from Fix step 3.
    • The ydoc_update_bytes read 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.
    • Decisions 1 and 2 stand: the budget is per connection, and an over-budget connection is refused and closed.

    Open before the build starts (owner: the maintainer):

    • Which window does the measure-only build count over? Recommended: one 60 s sliding window per connection. It is long enough to hold a whole-document first sync plus the edits after it. The largest stored head was 5,357,733 B. It is also short enough to catch a connection that keeps growing the document. Also record each inbound sync message's size in beforeSync, so the largest real message is known.

    Nothing is built yet.

  10. HMarzban commented on Nov 10, 2026

    @HMarzban
    CollaboratorAuthor

    Read, 2026-11-10: ydoc_update_bytes over 14 days

    Context 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.

    • Setup: production, read-only, at 2026-11-10 09:44Z, through Grafana's Prometheus data source. Prometheus holds 15 days of data, so the 14-day window is complete. 11 WS processes ran in the window, because each deploy starts new replicas.
    • p99 of an applied change: 7,403 B.

    Applied changes per bucket, 14 days, from sum by (le) (increase(ydoc_update_bytes_bucket{job="hocuspocus-server"}[14d])). Buckets are cumulative.

    At most Changes Share
    64 B 108,955 95.8 %
    256 B 109,595 96.4 %
    1 KiB 111,067 97.7 %
    4 KiB 112,243 98.7 %
    16 KiB 113,517 99.82 %
    64 KiB 113,604 99.90 %
    256 KiB 113,696 99.98 %
    1 MiB 113,718 99.996 %
    any size (+Inf) 113,723 100 %
    • The largest bucket is +Inf. About 5 applied changes in the 14 days were bigger than 1 MiB. The histogram cannot say how big they were.
    • The cross-check agrees. 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_time then keeps only the larger part. That cause is inferred, not checked.
    • Stored documents reach 4 to 16 MiB. 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:

    • Normal changes are small. 97.7 % are 1 KiB or less, and the p99 is 7,403 B.
    • A few changes are bigger than 1 MiB, and whole documents reach 4 to 16 MiB. So the budget must let a whole-document sync through.
    • The measure-only build from the ruling update shows the real bytes per connection, which this histogram cannot.

    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}}
  11. HMarzban commented on Nov 10, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-11-10

    This answers the window question left open by the ruling update above. Answer: "agree your recomondation."

    • The measure-only build counts each connection's sync bytes over one 60 s sliding window.
    • It also records the size of each inbound sync message in beforeSync, so the largest real message is known.
    • The window and the limit for the refusal still come from a week of that data.

    No open decision is left on this issue.

  12. HMarzban commented on Nov 11, 2026

    @HMarzban
    CollaboratorAuthor

    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:

    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.

  13. modified the milestones: v2.3.6, v2.3.7 on Nov 12, 2026
  14. 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 the work and its measurement take longer than the cut allows. The before values and the plan in this issue stay as they are.

  15. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-16: phase 1 built (measure only)

    Commit de0e10fc5. Phase 1 only: nothing refuses or closes.

    What changed

    • New apps/hocuspocus.server/src/extensions/syncBytes.extension.ts, in the collaboration server's extension list.
    • ws_sync_message_bytes observes the payload of each inbound sync message in beforeSync.
    • ws_sync_window_bytes observes the peak 60 s sliding sum of one connection, once a minute and once at the close. The window is 60 one-second buckets on context, never a module-level Map. A burst across a minute boundary stays one sum. Phase 2 must refuse on this same rolling sum.
    • The close takes the peak once, because Hocuspocus 3.4.4 runs onDisconnect twice (Confirm and guard the double onDisconnect in Hocuspocus 3.4.4 #601).
    • Both histograms use the ruled buckets: 1 KiB, 16 KiB, 256 KiB, 1 MiB, 4 MiB, 16 MiB, 32 MiB, 64 MiB. seedWsAlertSeries seeds both at 0.
    • WS_SYNC_BUDGET_BYTES and 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 of de0e10fc5.
    • 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 double onDisconnect. With the rolling sum sabotaged to read only the current second, 2 of 4 cases fail.

    Before and after

    Metric Before After Change n, range, condition, source
    Time per sync message, in-process Hocuspocus Server, 20,000 no-op Update frames (59 B), before and after alternating 4,319.5 ns median 4,439.5 ns median +120 ns, 2 % more time (rounded down from 2.7 %) n = 8 each. Before 4,226–5,350 ns, after 4,315–4,602 ns; the ranges overlap. One shared laptop, Bun 1.4.4. A local scratch script (one raw WebSocket client, then N acks); the before variant loads no extension
    Histograms record (real Server, real provider) both counts 0 (seeded) 23 messages: 22 at or under 1 KiB, 1 in the 4 MiB bucket; 1 window at the close — n = 1 probe, on the first build of this change. onDisconnect ran twice, and the window counted once

    The cost is per sync message on one laptop. Production cost was not measured.

    Gates

    • bun test src/extensions/__tests__/syncBytes.extension.test.ts: 4 pass. src/modules/document-conversion and this test: 73 pass, 0 fail.
    • bun run typecheck (apps/hocuspocus.server): pass.
    • ESLint on the touched sources, and Prettier: pass.

    Notes

    • The window counts the y-sync payload from beforeSync, not the frame header (the document name plus 2–3 bytes per message). Phase 2 in beforeHandleMessage sees 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

    histogram_quantile(0.99, sum by (le) (increase(ws_sync_window_bytes_bucket{job="hocuspocus-server"}[7d])))
    
    sum by (le) (increase(ws_sync_window_bytes_bucket{job="hocuspocus-server"}[7d]))
    
    sum(increase(ws_sync_window_bytes_count{job="hocuspocus-server"}[7d]))
    

    Run the same three for ws_sync_message_bytes. For p50, use 0.5.

    Left

    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 and ws_sync_window_bytes_count = 19 on hocuspocus-server, so both histograms record. The 7-day read is due on or after 2026-11-23 13:31Z.

  16. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

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

    • The production pass rule no longer needs the "landed an edit in 7 days" join. No step builds that join. Now each over-budget close in 7 days is checked against its warn log, and 0 may be signed-in editors.
    • Rollback now matches the phases. Phase 1 has no env flag, so it reverts de0e10fc5. Phase 2 sets WS_SYNC_BUDGET_BYTES=0.
    • Added a timing line. If phase 2 cannot finish its 7-day read before the v2.3.7 ship, phase 2 moves to v2.4.0.
  17. 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).

  18. HMarzban commented on Nov 24, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-11-24

    Split Phase 2 (WS_SYNC_BUDGET_BYTES refusal and close) into #836 (v2.4.0).

    #659 stays in v2.3.7 only to post the matured Phase 1 7-day PromQL read (2026-11-16 13:31Z to 2026-11-23 13:31Z), then closes (unblocking #649).

    Next step: post the Phase 1 7-day read and close #659.

  19. HMarzban commented on Nov 24, 2026

    @HMarzban
    CollaboratorAuthor

    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] ending 2026-11-23T13:31:00Z (7 days after the 2026-11-16 13:31Z deploy 06f61ee44). Neither #740, #738, nor #701 had shipped during this window. Container restarts occurred during the 2026-11-18 host move and 11-21..23 deploys; increase() accounts for counter resets.

    Result (exact 7-day Prometheus reads):

    Series Count (n) p50 p99 Highest filled bucket Samples above 16 MiB
    ws_sync_window_bytes (60 s sliding window) 2,617.36 4,526.97 B (4.4 KiB) 3,951,110.18 B (3.77 MiB) 4 MiB < x <= 16 MiB (23.0 windows) 0 (32 MiB and 64 MiB gain 0)
    ws_sync_message_bytes (beforeSync frame) 51,984.92 534.72 B 13,990.12 B (13.7 KiB) 4 MiB < x <= 16 MiB (23.0 messages) 0

    ws_sync_window_bytes bucket cumulative increase(): <= 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-24 ruling. Phase 2 (WS_SYNC_BUDGET_BYTES enforcement) continues in #836 (v2.4.0). Recommended starting default for #836: 32 MiB (2x headroom above the 4–16 MiB top filled bucket, clipping 0 % of observed 7-day traffic while bounding larger bursts).

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

    DevOpsSecurity

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions