Repository navigation
Description
Every open dashboard tab polls GET /api/versions: every 5 s while the reader is present, 10 s while away, and 60 s while the tab is hidden. This proposal sends the same Versions tokens over a push channel, so the server reads them only when they can have changed. It follows #2655, which added the detail_version bump and deferred SSE because push needs the same change signal plus streaming infrastructure.
Recommendation
- Signal: Postgres
NOTIFYfrom triggers on the tables that the tokens read. Each function instance holds oneLISTENsession. - Transport: SSE at
GET /api/versions/stream. Each SSE message is oneVersionsobject, the same schema as the poll. - Scope: the instance maps each change to the connected viewers that it can affect. Only those streams read
readVersionsagain. - Fallback:
useVersionsPollstays. It slows to 60 s while the stream is healthy and goes back to 5 s when the stream fails.
Signal
| Option | For | Against |
|---|---|---|
| Poll the DB inside each stream | No new infrastructure | Same DB load as today. It saves only HTTP overhead. |
| Publish from app write paths to Redis pub/sub | Redis is already the default state adapter. No new DB session. | Every write path must publish after commit. A publish before commit races the read. Review items, Turn cost metrics, and pins have no hook. |
Postgres LISTEN/NOTIFY from triggers |
Postgres delivers at commit, never for a rollback, so a read after the message sees the write. A trigger catches every write path, also raw sql. |
Needs a direct session: Neon's -pooler endpoint runs PgBouncer in transaction mode, which does not support LISTEN. The HTTP neon() query cannot listen. NOTIFY serializes the commits of transactions that notify. A held session keeps a Neon compute awake. |
| Vercel Queues | Already wired | A message goes to one consumer. There is no fan-out to every instance. |
Recommend NOTIFY:
- Triggers send ids only: the root Conversation id when
junior_conversations.detail_versionmoves, the Review item id, the actor identity of a Turn cost metric, and the user id of a participant or pin change. junior/touch-conversation-detailalready makes every Conversation write movedetail_version, so one trigger on that column covers the feed and detail tokens. Pins do not move it, so participants need their own trigger.- The direct URL comes from
DATABASE_URLwithout-pooler, asreadMigrationDatabaseUrldoes forjunior upgrade. - A payload only selects viewers to read again. No content comes from a payload.
Transport
SSE, not WebSocket:
- Data goes one way. When the open Conversation changes, the client opens a new stream with the new
conversationId. That costs one read, the same as the poll on navigation today. EventSourcereconnects by itself and sends the same-origin session cookie, sorequireViewerworks with no change. WebSocket on Vercel is a public beta, needs Nitrofeatures.websocket, and needs reconnect code that we write.- Junior already runs SSE on Vercel for ACP (
api/acp/transport.ts), with a 15 s keep-alive. Reuse that shape.
Vercel limits (Fluid compute):
- Max duration counts streamed time: 300 s default, 800 s max on Pro and Enterprise, 1,800 s in beta.
nitro.tssets 300 s for every function. The stream ends by itself before the limit, with a jitteredretry:, so it never ends as a 504. - Billing is active CPU plus provisioned memory. An idle stream uses no CPU, but it keeps its instance's memory billable. Streams on one instance share that cost.
- One instance has 1,024 file descriptors, shared with DB and HTTP sockets.
- Browsers allow 6 HTTP/1.1 connections per origin. Vercel serves HTTP/2, so many tabs are safe. A proxy that forces HTTP/1.1 is not.
Per-viewer scope:
- The stream authenticates once. Each message is
readVersions(viewer, { conversationId }), the same query and access as the poll. No new data leaves the server. - The first message is the baseline, so the change that
refreshChangedReadscan miss between a read and the first poll no longer waits. - After a dirty mark, the stream reads at once. Then it reads at most once each 5 s, so a busy viewer costs no more than today. It sends only tokens that differ from its last message.
- The instance listener batches payloads for about 250 ms. One query maps the batch to its connected viewers, with the feed's membership filter and
reviewItemNeedsUser. An extra match costs one read. A missed match loses the change until the next reconnect or safety poll. - When the
LISTENsession drops and comes back, every stream on the instance is dirty.
Cost model
readVersions reads per tab per minute:
| Tab | Poll today | Push |
|---|---|---|
| Visible, present | 12 | about 0.2 for reconnects, 1 for the safety poll, plus 1 for each change that affects the viewer (max 12) |
| Visible, away | 6 | the same as present |
| Hidden | 1 | 1 (poll only, no stream) |
- New costs: one direct Neon session per instance (counts toward
max_connections), one mapping query per batch per instance, oneNOTIFYper write transaction, and provisioned memory for open streams. - Push does not make one read cheaper.
Fallback
- While the stream is healthy (a message or keep-alive in the last 30 s),
useVersionsPollruns at 60 s as a safety net. - After a stream error or 3 failed reconnects, the poll goes back to 5 s. The client tries the stream again after 5 min.
- A hidden tab closes its stream and polls at 60 s, as today. It opens the stream again when it shows.
- With push off, or no direct session (memory state, no Neon direct endpoint), the route returns 204.
EventSourcestops on a 204, and the client polls.
Rollout and kill switch
- Triggers and the instance listener behind
JUNIOR_VERSIONS_STREAM_ENABLED, with no client. Record the notify rate, the mapping query time, and commit latency of the notifying writes. - The stream route and the client, behind the same flag. Record reads per tab, stream lifetime, reconnect rate, and how often the safety poll sees a move before the stream.
- Drop the safety poll only when step 2 shows that the stream never lags it.
Kill switch: set the flag off. The route returns 204, and each client goes back to the poll at its next reconnect, at most one stream duration later. A migration drops the triggers if their write cost is the problem.
Not doing
- No per-viewer token cache. A cached token goes stale and hides a change. Each message comes from a fresh
readVersions. A stream keeps only its last message to skip duplicates, and never serves it to another request. - No content in messages. The client still refetches the reads whose token moved.
- No WebSocket, and no sharing of one stream across tabs.
Decisions
- Signal:
NOTIFYwith one direct Neon session per instance. - Hidden tabs keep the 60 s poll and open no stream.
- Streams last 300 s, the current default.
- A revoked session stops at the next reconnect, up to 300 s later. That lag is accepted.
Status
Parked. Revisit when dashboard load or update latency needs it. The first step then is a spike that proves the Neon WebSocket Client delivers LISTEN notifications on Vercel.
via @BYK.
--
Activity
sentry-junior commentedon Nov 24, 2026
Spike result: go, with conditions
The spike (#4194, now closed) proved that a Vercel Fluid function can hold a Neon
LISTENsession on the direct (non-pooler) endpoint for a full streamed response and receiveNOTIFYfrom another connection.Preview (Vercel,
junior-example, shared example database)Two concurrent SSE streams on one Fluid instance. Each held one direct
ClientwithLISTENon a test channel, plus aSELECT 1keepalive every 15 s. A second route sent 36NOTIFYs over about 4.5 min through the pooled HTTPneon()driver. The spike read and wrote no tables.Clientconnect: 39 ms and 30 ms. A new stream opened right after the first one ended connected in 28 ms.NOTIFYover the pooled HTTP driver: 10–37 ms, p50 about 14 ms.max_connectionsis 450. The Neon compute did not restart or suspend during the run.Local (Neon wsproxy, Postgres 17,
@neondatabase/serverless1.1.0)NOTIFYs delivered over 300 s. Latency p50 3 ms, p95 4 ms, max 7 ms.pg_terminate_backend, the listener reconnected in 125–190 ms. The oneNOTIFYsent in that gap was lost.SELECT 1with a 5 s timeout detected it within 20 s.Disconnect leak
On the preview, closing the client did not cancel the function's stream. A stream that the client closed after 25 s kept its listener backend open until the 285 s cap ended it. Do not trust client abort to release the listener.
Not tested on the preview
Conditions for the build
SELECT 1heartbeat on the listener session.NOTIFYin the gap is lost.feat(dashboard): Push version tokens over SSE behind a flag (#4224)