Skip to content

Publish Thread Events Server-Side via PostgreSQL LISTEN/NOTIFY

  • Deciders: Julian Bäume
  • Date: 2026-09-03

Status

Accepted

Supersedes the "Notification Architecture (Update 2025-10-29)" section of ADR-0008. The Yjs decision in ADR-0008 is unchanged.

Context

Teams keeps thread state (comments, assignments, status, tags, drafts, imported mails) in PostgreSQL. Users expect a change made by one person to show up for everyone looking at the same inbox without a reload.

Today the browser of the acting user is the publisher. After a write succeeds, the frontend sends a Switchboard message through App Suite core's Socket.IO connection to all members of the inbox, who then re-fetch. The backend publishes nothing. This was a deliberate choice in ADR-0008, made to keep notification logic in one place while Yjs was still a proof of concept.

Reports of comments and assignments not appearing until the page is reloaded exposed the limits of this design. Its failure modes are structural, not a matter of Switchboard uptime alone:

  • No echo to the sender. Switchboard does not deliver a message back to the publisher, so the author's own view is stale and several store actions re-fetch locally to compensate.
  • Lost events. If a recipient's socket is reconnecting or the tab is in the background, the event is gone. There is no server-side queue and no polling. Mentions and assignments survive because they are persisted; plain comments, status changes and list updates have no backstop.
  • Publisher is the least reliable party. Publication depends on the acting user's browser staying open and connected after the write. A write that succeeds but whose follow-up publish fails is indistinguishable from "nothing happened" for everyone else.
  • Server-originated changes notify nobody. The background mail import (ADR-0017) writes new actions but emits no event. Other users see new mail only on their own sync or via pull-to-refresh.
  • Hard dependency on Switchboard. Every recipient needs the Switchboard capability and a live connection owned by App Suite core. Session forwarding mode (ADR-0022) targets deployments where Switchboard is not part of the stack at all, so real-time updates do not exist there.

Two earlier decisions already point at a different shape. ADR-0006 named LISTEN/NOTIFY as one of the reasons for choosing PostgreSQL. ADR-0017 considered pushing sync completion to the client and rejected it only because it would require a new transport for a single use case. With thread events, mail import and sync completion, there are now three use cases for one transport.

Decision

Event publication moves into the backend. PostgreSQL is the fan-out mechanism between replicas, and Teams no longer uses Switchboard for messaging.

Events are emitted where the write happens. Every backend operation that changes a thread, including the background mail import and sync completion, publishes an event through PostgreSQL NOTIFY inside the same transaction as the write. NOTIFY is transactional: the event is delivered on commit and discarded on rollback, so an event can never describe a change that did not happen, and a change can never happen without its event being handed to PostgreSQL.

Events are hints, not data. An event carries identifiers and a type only. Clients keep loading state through the existing REST API, so events never carry thread content, and duplicated or reordered events are harmless.

Each replica listens once. Every backend replica holds one dedicated database connection, outside the regular pool, subscribed to the event channel. It forwards each event to the browsers it currently serves whose user is entitled to see the affected inbox or is the addressed user. Cross-replica fan-out therefore comes from PostgreSQL alone; no message broker is introduced.

Server-Sent Events as the push channel. The backend exposes a one-directional event stream per authenticated user, using the same authentication as the Yjs WebSocket. Server-Sent Events is chosen over a second WebSocket because it reconnects on its own, needs no client library, passes through HTTP infrastructure as an ordinary long-lived response, and this channel never needs client-to-server messages. The sender receives its own events like everyone else.

Correctness does not depend on push. There is no server-side replay. On connect, on reconnect and when a tab becomes visible again, the frontend re-fetches what it currently shows. Push reduces latency; the re-fetch keeps the system correct when push is unavailable.

Switchboard messaging is removed. The frontend subscribes to the backend event stream instead of the App Suite core socket, and stops publishing Switchboard messages. The bell notifications and their persisted unread state are unchanged and remain the source for rehydration after a reload.

The event source is an internal interface. Routes publish to, and the push channel consumes from, a small abstraction so the LISTEN/NOTIFY implementation can be replaced (see alternatives) without touching either side.

Consequences

Positive

  • Fixes the "reload to see it" problem at the root: events originate from the system of record, atomically with the write, and reach every replica and every connected browser including the sender.
  • Real-time updates work in every deployment, including session forwarding mode without Switchboard.
  • New mail from the background import and sync completion can be pushed, closing the gaps left open in ADR-0017.
  • Removes a class of client-side workarounds and the dependency on the App Suite core socket.

Negative / constraints

  • No transaction-mode connection pooler between Teams and PostgreSQL. LISTEN is session state; behind a pooler in transaction mode the subscription lands on an arbitrary backend and events are silently dropped. Operators who run a shared pooler must serve the Teams database in session mode or point Teams at a direct endpoint, which every managed PostgreSQL offering provides. Because this failure is silent, the backend verifies at startup that a published event reaches its own listener, and reports the result through logging and metrics.
  • Connection budget. Each replica needs its pool plus one listener connection, which the PostgreSQL connection limit must accommodate. This has to be documented for operators.
  • A second server-to-client channel next to the Yjs WebSocket. The consolidation noted in ADR-0008 still stands.
  • NOTIFY is not durable. Events during a PostgreSQL failover or a reconnect on either side are lost; the re-fetch on reconnect is what makes this acceptable.
  • Ties Teams closer to PostgreSQL. A future port to another database needs a polling implementation behind the event source interface.

Alternatives Considered

Publish from the backend through Switchboard. Fixes the "browser is the publisher" problem but keeps the hard dependency on Switchboard, its capability and its availability, and does nothing for deployments without it.

Transactional outbox with polling, NOTIFY as accelerator. Persist every event in a table in the same transaction; replicas poll for new rows and use NOTIFY only to wake up early. Works behind any pooler, gives durable replay and is database-portable. Rejected for now because it adds a table, per-replica cursors, retention housekeeping and a polling loop for a problem the pooler constraint solves with documentation. It is the designated fallback if that constraint proves unacceptable for a real deployment, and the event source interface exists to make that swap local.

In-process event bus. Sufficient for a single replica but breaks on scale-out. The service is CPU-bound and scales on replicas, so this would be rewritten shortly after shipping.

Carry actions in Yjs documents. Actions are server-authored facts, not collaborative state, and per-thread documents cannot deliver inbox-level list updates. It would also blur the boundary between CRDT synchronisation and notifications that ADR-0008 established.

WebSocket instead of Server-Sent Events. Feasible with the existing WebSocket support, but offers nothing this one-directional channel needs and costs a client library and reconnect logic.

References