Skip to main content

FIFO queues and ordered delivery

PostgreMQ provides FIFO delivery within a message group in each queue. Publish related messages with the same group key: the next message waits until the preceding unsettled message is acknowledged, removed, or dead-lettered.

FIFO controls ordering. Durable and Exclusive control queue lifetime. Either queue type can receive grouped messages, and a queue can contain several groups plus ungrouped work.

Publish an ordered sequence​

Create the events topic and jobs queue using Durable queues. Use a fresh queue and group with no earlier unsettled messages for this demonstration. These examples publish messages 1 and 2 with the same key using an existing SDK connection. Each standalone publication commits before the next call begins.

for _, number := range []int{1, 2} {
payload, err := json.Marshal(map[string]int{"number": number})
if err != nil { return err }
if _, err := conn.Publish(ctx, "events", payload,
postgremq.WithGroupKey("sequence-a")); err != nil { return err }
}

Run two consumers against jobs. Only message 1 can be claimed from this group initially. Message 2 remains blocked while message 1 is processing or waiting for retry. After message 1 is acknowledged, message 2 can be claimed. A second queue on the topic progresses through its own copy independently.

Publish another sequence with sequence-b to permit progress across the two groups. Ordering does not require every group to wait for the slowest one.

Choose the ordering boundary​

Use a group key for the state that must be updated in order: an entity ID, a partition key, or another application-defined sequence. Keys are scoped by topic and can contain up to 255 characters; the empty string is rejected. Omit the key for ungrouped delivery.

One shared key for all publications produces a single sequence within each subscribed queue. That also limits each queue to one active group head. Choose it only when global ordering within that topic is required. More consumers cannot increase parallelism within that key. Publication also holds the group’s row lock until the publishing transaction ends, so a long transaction delays other publishers using the same topic and group key.

Concurrent publishers in one group are ordered by grouped publication commit order, not caller wall-clock timestamps or the order their requests started. Transactions publishing several groups should use a consistent group order to reduce deadlocks and retry the whole transaction when required.

Delays and failures block successors​

message 1: delayed → claimed → retry delay → claimed → acknowledged
message 2: ------------------------------------------------------→ claimable

A delivery time applies to the group head as well as the individual message. An immediately eligible successor cannot pass an earlier delayed message. Similarly, repeatedly failing work blocks its group until it succeeds or leaves the unsettled sequence.

Use finite attempt limits when one poison message must not block its group forever. A final nack can dead-letter the head immediately; a final-attempt crash requires fast maintenance after lease expiry. Other groups remain independently eligible, although they share database resources.

Replay and duplicate effects​

Dead-lettering or deletion lets later messages proceed. Replay preserves the original group sequence and can return older work after newer work already completed. It does not rewind application state or restore the original processing history.

FIFO does not deduplicate publications. Retrying an uncertain publish can create another message in the group. It also cannot fence an external system against an old worker that continues after losing its claim. Respond to cancellation and use Idempotent consumers where effects must tolerate repetition.

For scheduling, see Delayed messages. For recovery decisions, see Retries and Dead-letter replay.