Skip to content

feat: PostgreSQL LISTEN/NOTIFY for near-instant async projection updates #4183

Closed

Description

@thomas-px

Problem

The async daemon polls PostgreSQL at configurable intervals (250ms fast, 1s slow) to detect new events via the high water mark. This creates a latency floor between when events are appended and when projections process them. For use cases where sub-second projection updates matter, this polling delay is unnecessary overhead.

Proposed solution

Use PostgreSQL's built-in LISTEN/NOTIFY mechanism to wake the async daemon immediately when new events are appended, instead of waiting for the next poll cycle.

The implementation spans two repositories:

1. JasperFx.Events — IDaemonWakeup extension point

PR: JasperFx/jasperfx#164

Introduces an IDaemonWakeup interface that abstracts the delay between polling cycles in the HighWaterAgent. The default TaskDelayDaemonWakeup preserves existing behavior (Task.Delay). Custom implementations can use external signals to wake the agent immediately.

2. Marten — PostgreSQL LISTEN/NOTIFY implementation

PR: #4172

Implements PostgresqlListenWakeup (an IDaemonWakeup) that:

  • Opens a dedicated connection that LISTENs on the mt_events_appended channel
  • Adds a NotifyEventAppendedOperation that calls pg_notify('mt_events_appended', '') in the same transaction as the event append
  • Uses a semaphore to signal the high water agent instantly upon notification
  • Falls back to the configured polling interval if no notification arrives (safety net)

Enabled via:

opts.Events.UseListenNotifyForEventAppends = true;

Considerations

  • Requires the event writer and the async daemon to connect to the same PostgreSQL instance (LISTEN/NOTIFY does not work across read replicas)
  • Requires one additional long-lived PostgreSQL connection per daemon node
  • Not compatible with PgBouncer in transaction pooling mode (LISTEN requires session-level state)
  • Polling remains as a fallback, so the feature is additive and backward-compatible

Activity

  1. jeremydmiller commented on Mar 23, 2026

    @jeremydmiller
    Member

    @thomas-px I'm deeply unenthusiastic about using LISTEN/NOTIFY. It's been years, but my experience from early testing is that it's extremely unreliable

  2. jeremydmiller commented on Apr 6, 2026

    @jeremydmiller
    Member

    I'm moving this to a discussion for now

  3. locked and limited conversation to collaborators on Apr 6, 2026
  4. converted this issue into a discussion #4234 on Apr 6, 2026
  5. added a commit that references this issue on May 18, 2026
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions