Skip to content

perf: coalesce queued updates into one flush - #1500

Merged
dunglas merged 1 commit into
mainfrom
perf/coalesce-sse-writes
Dec 1, 2026
Merged

dunglas merged 1 commit into
mainfrom
perf/coalesce-sse-writes

Conversation

@dunglas

@dunglas dunglas commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Each update was flushed on its own: one write syscall per update and subscriber. Profiling a hub broadcasting to 1,000 URL pattern subscribers puts 95% of its CPU in these writes.

The subscriber loop now writes every update already queued when a batch starts, then flushes once. Updates queued later wait for the next batch. A batch also ends early when the request is canceled, the hub shuts down, a drain starts or the disconnection time is reached, so the subscribe loop handles these without delay.

Deadlines:

  • before each update, after serializing it, the hub checks the write deadline and arms the dispatch deadline (prepareWrite, now shared with write): a batch never writes past the token's exp, even behind a wrapper that re-arms the connection deadline
  • every update keeps its own dispatch_timeout, so a long batch is not cut off by a single timeout, and a client that stopped reading is still cut off after one timeout
  • the flush keeps the last update's dispatch deadline instead of getting a new one
  • near the write deadline, the dispatch deadline is capped at it instead of left unset, which would keep a previous update's shorter deadline armed

Measured on Linux (arm64, 10 CPUs), medians of 3 runs, no lost delivery:

Before After
Replaying 900 updates to 1,000 subscribers (Bolt) 0.59 s, 3 CPU s 0.24 s, 1 CPU s
URL pattern broadcast, 200,000 deliveries/s: delivery p99 12.8 to 20 ms 9.3 to 11 ms

Exact-topic fan-out throughput and CPU are unchanged. On macOS, where concurrent socket writes contend in the kernel, the broadcast drops from 37 to 15 CPU seconds over 10 s.

Each update was flushed on its own, costing one write syscall per update and subscriber. Write every update queued when the batch starts, then flush once. Updates queued later wait for the next batch, and a batch ends early on a disconnection, a shutdown, a drain or the disconnection time, so the subscribe loop handles them without delay.

Check the write deadline and arm the dispatch deadline before each update, after serializing it: a long batch must not be cut off by a single dispatch timeout, nor write past exp. The flush keeps the last update's dispatch deadline. Near the write deadline, the dispatch deadline is now capped at it instead of left unset, which would keep a previous update's shorter deadline armed.

Medians of 3 runs on Linux (arm64, 10 CPUs):

- replaying 900 updates to 1,000 subscribers with Bolt: 0.59 s and 3 CPU seconds -> 0.24 s and 1
- broadcasting to 1,000 URL pattern subscribers (200,000 deliveries/s): delivery p99 12.8-20 ms -> 9.3-11 ms

Exact-topic fan-out throughput and CPU are unchanged.
@dunglas
dunglas force-pushed the perf/coalesce-sse-writes branch from b00036d to 2c64fb8 Compare December 1, 2026 17:22
@dunglas
dunglas merged commit 0e7e6c8 into main Dec 1, 2026
48 checks passed
@dunglas
dunglas deleted the perf/coalesce-sse-writes branch December 1, 2026 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant