Skip to content

Push dashboard version tokens over SSE #3776

Open

Description

@sentry-junior

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 NOTIFY from triggers on the tables that the tokens read. Each function instance holds one LISTEN session.
  • Transport: SSE at GET /api/versions/stream. Each SSE message is one Versions object, the same schema as the poll.
  • Scope: the instance maps each change to the connected viewers that it can affect. Only those streams read readVersions again.
  • Fallback: useVersionsPoll stays. 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_version moves, 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-detail already makes every Conversation write move detail_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_URL without -pooler, as readMigrationDatabaseUrl does for junior 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.
  • EventSource reconnects by itself and sends the same-origin session cookie, so requireViewer works with no change. WebSocket on Vercel is a public beta, needs Nitro features.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.ts sets 300 s for every function. The stream ends by itself before the limit, with a jittered retry:, 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 refreshChangedReads can 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 LISTEN session 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, one NOTIFY per 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), useVersionsPoll runs 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. EventSource stops on a 204, and the client polls.

Rollout and kill switch

  1. 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.
  2. 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.
  3. 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: NOTIFY with 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.

--

View Session [Sentry]

Activity

  1. sentry-junior commented on Nov 24, 2026

    @sentry-junior
    ContributorAuthor

    Spike result: go, with conditions

    The spike (#4194, now closed) proved that a Vercel Fluid function can hold a Neon LISTEN session on the direct (non-pooler) endpoint for a full streamed response and receive NOTIFY from another connection.

    Preview (Vercel, junior-example, shared example database)

    Two concurrent SSE streams on one Fluid instance. Each held one direct Client with LISTEN on a test channel, plus a SELECT 1 keepalive every 15 s. A second route sent 36 NOTIFYs over about 4.5 min through the pooled HTTP neon() driver. The spike read and wrote no tables.

    Stream 1 Stream 2
    Notifies received 36 of 36 36 of 36
    Latency p50 / p95 / max 13 / 22 / 35 ms 14 / 22 / 36 ms
    Held 285,101 ms, ended at the cap 285,103 ms, ended at the cap
    Keepalives, RTT p50 / max 19, 4 / 5 ms 19, 4 / 5 ms
    Listener errors 0 0
    • Direct Client connect: 39 ms and 30 ms. A new stream opened right after the first one ended connected in 28 ms.
    • NOTIFY over the pooled HTTP driver: 10–37 ms, p50 about 14 ms.
    • Backends: 2 before, 4 with both listeners open, 2 after. max_connections is 450. The Neon compute did not restart or suspend during the run.

    Local (Neon wsproxy, Postgres 17, @neondatabase/serverless 1.1.0)

    • 297 of 298 NOTIFYs delivered over 300 s. Latency p50 3 ms, p95 4 ms, max 7 ms.
    • After pg_terminate_backend, the listener reconnected in 125–190 ms. The one NOTIFY sent in that gap was lost.
    • A silently dropped socket went undetected with no heartbeat. A 15 s SELECT 1 with 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

    • Delivery to a stream opened after a reconnect. The notify loop ended before the reconnect stream opened. It worked locally.
    • Backend kill and drop detection. Tested locally only.

    Conditions for the build

    • End each stream at a short cap, or close it when a write fails. Do not trust client abort.
    • A 15 s SELECT 1 heartbeat on the listener session.
    • Mark every stream dirty after the listener reconnects, because a NOTIFY in the gap is lost.
    • One shared listener per instance on the direct endpoint. Close it when the last stream ends.
    • Viewer-gated streams.
    • Keep 5 s polling as the fallback.
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions