Repository navigation
perf(platform): serve realtime streams from one shared reader per process - #4590
Merged
Merged
Conversation
yannickmonney
force-pushed
the
perf/realtime-shared-readers
branch
from
October 9, 2026 03:01
56d6ca5 to
c4148bf
Compare
yannickmonney
enabled auto-merge
October 9, 2026 03:24
yannickmonney
force-pushed
the
perf/realtime-shared-readers
branch
from
October 9, 2026 04:29
c4148bf to
52efcfe
Compare
…rocess Each open hint stream ran its own loop: an outbox read every 300 ms and two authorization reads every 15 s, ~3.5 queries a second per browser tab. One loop per process now reads each new outbox row once and fans it out in memory; a ring of recent rows serves resumes without a query; the authorization recheck is one batched pass beside the tail; one timer heartbeats every stream; each stream has an ordered write queue, and one that stops reading is ended so it resumes instead of stalling the rest. A stream opens with a jittered reconnect delay. Same events, same order. 5,000 idle tabs on one api process: 10,961 -> 153 transactions/s, backend CPU -80 %, RSS 1,678 -> 313 MiB, task create p95 3.8 s -> 250 ms.
A deploy refuses every tab's handshake in the same second, and the capped doubling brought them all back in the same second too, attempt after attempt. Each wait is now spread over the upper half of its interval.
Every open chat tab polled its thread's generation row every 250 ms, read the live parts on every tick of a running turn and the whole transcript when it settled. One watcher per process now does a light read per tick for all watched threads, the text and parts only for the threads that moved, and the settled row alone, shared by every tab of the thread.
PERF-P17 checks with the load harness that a crowd of idle tabs costs the database per process, not per tab; its automation row names the unit tests that hold the fan-out half.
A resume the in-memory ring could replay skipped the retention check, so a client whose cursor the database had already reclaimed replayed from the ring and was never told to resync — the contract the per-stream loop kept, and one a reconnect to another process would still keep. The ring path now asks for the oldest retained id, one read shared by every resume within 250 ms and dropped after the hub's own reclaim, and sends resync after the replay when the cursor is below it.
An outbox id is taken at insert but visible only at commit, so a transaction that commits after a later one leaves a hole the tail's id > cursor read has already passed: that hint never reached anyone, as it never did from the per-stream loop. The tail now remembers the ids it read past, looks for them each tick for 30 s, and hands what commits to every stream of its org, framed without an id so a browser's resume position does not move back. A rolled-back id is forgotten after the grace period.
The stream writer's ceiling counted queued frames, and the hub queued a replay or a tail page frame by frame in one synchronous loop: any batch of more than 256 distinct hints overflowed on the fastest client and aborted the stream before it sent anything, and the reconnect replayed the same batch into the same abort. A delivery is now one write, the ceiling also bounds queued characters (1 MiB), and a write onto an empty backlog is always taken.
A late row older than the ring's oldest is no longer put in front of it: the oldest row is what a resume trusts the ring to reach back to. The row still reaches every live stream.
Track the newest ids of a big jump, look only just under the first row when the tail starts on an empty outbox, and give holes among the newest rows at startup the same grace. Late rows are coalesced and framed once per org. A writer refuses only a backlog already past its byte ceiling, and the progress lane gets a 16 MiB ceiling for whole-reply snapshots.
yannickmonney
force-pushed
the
perf/realtime-shared-readers
branch
from
October 9, 2026 04:46
d4771a5 to
22370b5
Compare
The budget was spent in ascending order, so a big older jump in one read used it all and the hole next to the newest rows, the one a transaction still in flight holds, went untracked.
A stream's byte ceiling now leaves its biggest queued write out, so a replay bigger than the ceiling no longer refuses the resync right behind it, which looped a tab through reconnects. A progress snapshot replaces its unsent predecessor in place, carrying its parts, so a tab that stops reading holds one snapshot instead of every tick, and the lane is back on the default ceiling.
yannickmonney
disabled auto-merge
October 9, 2026 05:27
yannickmonney
enabled auto-merge
October 9, 2026 05:35
Holes an earlier read noted held the whole budget for their grace period, so a hole a later read passed, the one a transaction still in flight holds, went untracked. Holes are noted in ascending order and the oldest give way.
A tab that fell behind on text alone was sent the whole parts payload with every replacement. The progress lane gets its larger ceiling back: replacement holds it to a few frames, and an agentic turn's frames can pass a megabyte each, which refused the settled frame of a tab one snapshot behind.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Realtime cost moves from per open browser tab to per API process. Same events, same order, same authorization.
/events(backend/realtime/hint-hub.ts,sse-fanout.ts): one loop per process reads each new outbox row once and fans it out in memory; a ring of recent rows servesLast-Event-IDresumes without a query (an older cursor still reads the database and saysresyncwhen it must); the membership/session recheck is one batched pass beside the tail; one timer heartbeats every stream; each stream has an ordered write queue and is ended when it stops reading, so one slow client cannot stall the rest. Streams open with a jitteredretry:so a deploy's reconnects spread out.backend/domains/chat/generation-watch.ts): one watcher per process does a light read per tick for every watched thread, text and parts only for threads that moved, and reads the settled message alone (not the transcript), shared by every tab of the thread.use-backend-hints.ts): each wait spreads over the upper half of its interval.Why
Each
/eventsstream polled the outbox every 300 ms plus two auth reads per 15 s (~3.5 queries/s per tab); each open chat tab polled its generation row every 250 ms. A million tabs would have been millions of queries a second.Evidence (load harness, #4589)
5,000 idle tabs + 50 task writers, one api process, same machine and workload:
Testing
backend/realtime/*26 tests (8 new: one tail read per poll for a crowd, targeted hints, other orgs, ring resume without a per-stream read, database resume withresync, batched recheck ending only the revoked stream, jittered retry, loop stops after the last stream);generation-watch.test.ts5 new; chat routes and the drain test unchanged;use-backend-hints27.PERF-P17(manual suite) measures the per-process cost with the harness'sconnectionsprofile.Fixes from review and CI (on top of the first push)
resync. The ring path now asks for the oldest retained id — one read shared by every resume within 250 ms (dropped after the hub's own reclaim) — and sendsresyncafter the replay. Proven locally: the lane readsgapped: resync=true.id > cursorread had passed — the hint never reached anyone (main's per-stream loop lost these too, but the shared tail passes other orgs' rows, so more often). The tail now remembers the ids it read past, looks for them each tick for 30 s, and hands late rows to their org's streams framed without anid(a browser's resume position does not move back). A rolled-back id is forgotten after the grace period.sse-fanout.test.tscovers the ceiling; a 400-hint replay test arrives whole.