Skip to content

feat(message-bus): JetStream queue groups and BROADCAST subscription mode - #58

Merged
fraliv13 merged 3 commits into
mainfrom
fix/jetstream-consumer-groups
Oct 28, 2026
Merged

fraliv13 merged 3 commits into
mainfrom
fix/jetstream-consumer-groups

Conversation

@fraliv13

@fraliv13 fraliv13 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

PUB_SUB is documented as "non-durable, within a queue group", and the NATS Streaming and Rusi transports subscribe it that way: instances of a service compete for messages.

The JetStream transport gave every PUB_SUB subscriber its own unnamed ephemeral consumer, so every instance received every message. The same mode name delivered differently depending on the transport.

There was also no mode meant for delivering every message to every instance. RPC happens to behave that way, but it exists for sendCommandAndReceiveEvent.

Change

JetStream consumers

Mode Before After Instances
STREAM_PROCESSOR durable <clientId>__<subject> unchanged compete
PUB_SUB unnamed ephemeral per subscriber (fan-out) ephemeral <clientId>__<subject>, shared compete
RPC unnamed ephemeral per subscriber ephemeral <clientId>__<subject>__<uuid> every subscriber gets every message
BROADCAST (new) – ephemeral <clientId>__<subject>__<uuid> every subscriber gets every message

The shared PUB_SUB consumer is created by the first subscriber. jsm.consumers.add only creates, so a later subscriber whose config differs fails with consumer already exists (10148). This happens even when the configs look identical, because the server stores defaults that the request leaves out. On that error the transport now calls jsm.consumers.update, so the latest subscriber's config wins, as with NATS.Net's CreateOrUpdateConsumerAsync in NBB. Settings the server can't change on an existing consumer, such as the deliver policy, still fail.

PUB_SUB also sets inactive_threshold explicitly to the server default (5 s): the consumer is deleted 5 s after the last subscriber stops pulling.

New BROADCAST mode

SubscriptionOptions.BROADCAST is non-durable, at-most-once and has no queue group, so every instance receives every message. Typical uses are refreshing an in-memory cache or pushing to the clients connected to each instance. It matches SubscriptionTransportOptions.Broadcast in NBB.

  • Enum: appended after RPC, so the existing numeric values don't change.
  • NATS Streaming: plain subscription, new messages only.
  • JetStream: an ephemeral consumer per subscriber, as above.
  • Rusi: sends qGroup = false, durable = false, new messages only.
  • Configuration: its own environment variables per transport (NATS_BROADCAST_MaxInflight / NATS_BROADCAST_AckWait, JETSTREAM_BROADCAST_MaxConcurrentMessages / JETSTREAM_BROADCAST_AckWaitTime, RUSI_BROADCAST_MaxConcurrentMessages / RUSI_BROADCAST_AckWaitTime), with a default concurrency of 100 like PUB_SUB.

RPC stays as it is, not deprecated: its name is part of existing environment variable configuration, and sendCommandAndReceiveEvent keeps using it.

The README documents BROADCAST, its environment variables, and how queue groups map onto JetStream consumers. Two changesets bump @totalsoft/message-bus as minor.

⚠️ Behaviour change

A service that relied on PUB_SUB fanning out on JetStream will now see each message on only one instance. It should switch to BROADCAST. Raise the bump to major if you consider this breaking.

Consistency

The same behaviour is implemented in:

  • NBB's JetStream transport, which also adds the Broadcast preset: osstotalsoft/nbb#299
  • the Rusi sidecar's JetStream component, which the Rusi transport delegates to: osstotalsoft/rusi, branch fix/jetstream-consumer-groups

Testing

  • Unit tests: jest passes, 41 tests. That includes a new Rusi transport test checking the flags BROADCAST sends to the sidecar.

  • Build and lint: tsc succeeds and eslint reports no new warnings.

  • Live check: message-bus has no automated tests for the JetStream transport. I ran the compiled transport against NATS 2.11 in Docker, with two subscribers per mode and 10 messages each:

    Mode Subscriber 1 Subscriber 2
    STREAM_PROCESSOR 5 5
    PUB_SUB 5 5
    RPC 10 10
    BROADCAST 10 10

    After unsubscribing, the server removed every ephemeral consumer within the 5 s threshold and kept only the durable one.

  • Config mismatch: two separate processes subscribed with PUB_SUB using different JETSTREAM_PUB_SUB_AckWaitTime values (30 s, then 10 s). Both subscribed, the shared consumer's stored ack wait changed from 30 s to 10 s, and the two split 10 messages 5/5.

Liviu Frateanu and others added 2 commits October 27, 2026 16:26
…ptions

PUB_SUB is documented as "within a queue group", and the NATS Streaming
and Rusi transports subscribe it that way. The JetStream transport gave
every PUB_SUB subscriber its own unnamed ephemeral consumer, so every
instance received every message.

PUB_SUB now uses one named ephemeral consumer per client id and subject,
<clientId>__<subject>, shared by all subscribers, so they compete. Its
inactive threshold is set explicitly to the server default (5 s):
without it the stored config differs from the requested one and the
second subscriber fails with "consumer already exists" (10148).

RPC keeps fan-out with a named ephemeral consumer per subscriber,
<clientId>__<subject>__<uuid>. STREAM_PROCESSOR is unchanged.

This matches NBB's JetStream transport (osstotalsoft/nbb#299).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BROADCAST is non-durable, at-most-once and has no queue group, so every
instance of a service receives every message. It matches
SubscriptionTransportOptions.Broadcast in NBB (osstotalsoft/nbb#299).

- Appended to the SubscriptionOptions enum after RPC, so existing
  numeric values do not change.
- NATS Streaming: plain subscription, new messages only.
- JetStream: ephemeral consumer per subscriber,
  <clientId>__<subject>__<uuid>, new messages only.
- Rusi: qGroup = false, durable = false, new messages only.
- Own environment variables per transport (*_BROADCAST_MaxInflight /
  MaxConcurrentMessages and AckWait / AckWaitTime), with a default
  concurrency of 100 like PUB_SUB.

RPC is unchanged: its name is part of existing environment variable
configuration and sendCommandAndReceiveEvent keeps using it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 24c0db6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@totalsoft/message-bus Minor
@totalsoft/messaging-host Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…differs

jsm.consumers.add only creates, so a subscriber whose config differs from
the shared PUB_SUB consumer's (for example a different
JETSTREAM_PUB_SUB_AckWaitTime during a rolling update) failed with
"consumer already exists" (10148). On that error the transport now calls
jsm.consumers.update, so the latest subscriber's config wins, as with
NATS.Net's CreateOrUpdate in NBB. Settings the server cannot update, such
as the deliver policy, still fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fraliv13
fraliv13 merged commit 965e068 into main Oct 28, 2026
1 check passed
@fraliv13
fraliv13 deleted the fix/jetstream-consumer-groups branch October 28, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants