Skip to content

Ordering and message groups

A message group is a key that ties related messages together, usually the id of the thing they are about: sales:order:1042. Messages in the same group are delivered first in, first out; messages in different groups are delivered in parallel. A message without a group is not ordered at all.

  • An event’s messageGroup is copied onto every dispatch job the event raises, prefixed with the subscription: the job’s group is <subscription id>:<event group>, so ordering and BLOCK_ON_ERROR are per subscriber (see below). The envelope the subscriber receives still shows the event’s own group.
  • A directly created dispatch job carries its own messageGroup.

Pick the key so that it covers exactly the messages whose order matters. The id of the aggregate (the order, the shipment) is usual: every change to order 1042 is delivered in the order it happened, while changes to other orders flow in parallel. A constant key (“all orders”) would deliver everything one at a time; no key at all gives no order.

A subscription (or a direct job) chooses how its group treats a failure:

Mode Order When a job fails
IMMEDIATE None. Jobs are delivered concurrently. Nothing else is affected.
NEXT_ON_ERROR (the default) First deliveries go out in order. The group moves on. A job waiting for a retry, or FAILED, does not hold the jobs behind it, so they can overtake it.
BLOCK_ON_ERROR Strict. The group stops behind the job until it gets through or an operator resolves it.

Use BLOCK_ON_ERROR when a later message makes no sense without an earlier one: “item added” for an order whose “order created” never arrived. Use NEXT_ON_ERROR when order is preferable but a stuck message should not hold up the rest, and IMMEDIATE when order does not matter.

Ordering is defined only within a group. A job in an ordered mode without a group is delivered concurrently, exactly like IMMEDIATE. That is what makes an ordered default safe: ungrouped traffic is never squeezed through one ordered lane.

Within a group, jobs are ordered by (sequence, created_at, id):

  • sequence comes from the subscription (default 99; lower goes first);
  • created_at is the raising event’s time (or the direct job’s);
  • id is a TSID, which grows with time, and breaks ties.

A group is per subscription. The job’s group is <subscription id>:<event group> (a long event group is shortened to a digest to fit the 200-character column). Jobs for different subscribers of the same event group therefore do not share a group and never wait for each other: under BLOCK_ON_ERROR, a failed job holds only the later jobs of its own subscription, and another subscriber keeps receiving the same events. sequence orders jobs within one subscription’s group. The subscriber is still told the event’s own group in the envelope’s messageGroup. Jobs created any other way (SDK routes, functions, ingestion) keep the group their creator gave. Jobs raised before this change keep the bare event group.

Order is not enforced in one place. Each stage of the pipeline preserves it, so that no later stage receives a group out of order.

flowchart LR
  A[Outbox relay<br/>serial per group] --> B[Ingest<br/>stored in arrival order]
  B --> C[Fan-out<br/>events in time order<br/>one active copy]
  C --> D[Dispatch scheduler<br/>claims in group order<br/>one lane per group<br/>one active copy]
  D --> E[Queue<br/>FIFO per group]
  E --> F[Router<br/>FIFO buffer per group<br/>active router only]
  F --> G[Delivery callback<br/>BLOCK_ON_ERROR hold]
Stage How it keeps order
Outbox relay Claims rows by (message_group, created_at, id) and sends a group’s rows one at a time. A retryable failure holds the group with a backoff (1 second up to 5 minutes) and no later row overtakes the held one; a refused row blocks its group, across restarts, until an operator unblocks or skips it. A 429 holds the whole group. A group with a row left claimed by a relay that died is skipped until crash recovery returns it, so a crash does not reorder the group. See the outbox.
Fan-out Reads new events in time order, in one active copy, and gives each job its event’s time.
Dispatch scheduler Claims due jobs ordered by group and (sequence, created_at, id). Each group always goes to the same publishing lane, so a group’s jobs are published in claim order. One active copy.
Queue SQS FIFO orders by message group. The PostgreSQL queue makes only the earliest eligible message of a group claimable. NATS JetStream has no per-group hold on redelivery, so a deferred message’s successors are not held back by the queue itself.
Router Keeps a first-in, first-out buffer per group in each pool and drains it serially. When it hands a message back to the queue, it hands back the whole group behind it, so the head cannot return behind its successors.
Delivery callback Refuses to deliver a BLOCK_ON_ERROR job while an earlier job of its group holds the group (see below).

The fan-out, the scheduler and the router each keep part of the order in memory, so each must have exactly one active copy. With FC_STANDBY_ENABLED they elect a leader through Redis, and a copy that cannot hold an election does nothing rather than risk a second active copy. Without it, run one replica of each.

A job holds its group when it is ahead of a job and has not got through:

  • it is FAILED, waiting for an operator; or
  • it is PENDING with a future scheduled time, waiting out a retry backoff or a deferral.

QUEUED and PROCESSING do not hold: that is normal flow, where the scheduler hands a group’s run of jobs to the router and the router’s buffer delivers them in order. “Ahead” is positional: a job holds only the jobs after it in (sequence, created_at, id), never itself.

The hold is applied at three points, because a job can be at any stage when the one ahead of it stalls:

  1. When the scheduler claims. A BLOCK_ON_ERROR job behind a holder is not published; it stays PENDING.
  2. When the callback is called. A job that was already on the queue when the one ahead stalled is put back to PENDING without delivering and without spending an attempt. Its queue message is acknowledged; the scheduler publishes it again, in order, once the group moves.
  3. When the router sees the failure. If the router itself sees the head fail for good (for example the callback refuses the call), it acknowledges the untried messages buffered behind it instead of handing them back, and reports their ids to the platform, which returns those jobs to PENDING. A sweep every 2 minutes catches any it missed: jobs left QUEUED behind a FAILED head (or PROCESSING and untouched for 45 minutes) go back to PENDING, with the reason in lastError.

Why acknowledge the siblings instead of handing them back. Handing them back reads as the safer choice and is the opposite. The queue would redeliver them on its own timer, which knows nothing about the failure; by then the failed head has been acknowledged, so the first sibling becomes the new head and is delivered: “item added” to an order that was never created. Nothing is lost by acknowledging them, because the queue message is only a pointer. The job rows stay in the platform, and are published again in order once the head is resolved.

Resolving a blocked group. The group waits until the head is dealt with:

Operator action on the head Result
Requeue it It is delivered again; if it succeeds, the jobs behind it follow in order.
Cancel it The group continues past it; the head is never delivered.
Mark it completed The group continues past it, as if it had been delivered.