Skip to main content

Durable queues

A durable queue has a stable identity and retains its backlog when consumers disconnect. Applications reconnect to the same queue by name to continue processing. The queue also accepts new messages while no consumers are connected.

Use one durable queue to distribute work among several workers, or a separate durable queue for each subscriber that needs its own delivery. In both cases, progress belongs to the queue and survives the processes consuming from it.

For subscriptions that should expire after the application goes away, use an Exclusive queue.

Queue identity and lifetime​

Create a queue before publishing the messages it must receive. Its subscription starts when it is created; creating a queue later does not recover earlier publications from the topic.

A durable queue remains until explicitly deleted, either directly or by deleting its topic. Disconnecting its last consumer does not delete it, clear its backlog, or stop new messages from arriving. Completed deliveries and their stored payloads are cleaned up separately through retention maintenance.

Reconnecting to an existing queue resumes that queue's remaining work. Deleting it and creating another queue with the same name creates a new subscription with no old backlog. Existing consumers do not silently switch to the replacement.

Durability preserves queue state; it does not guarantee that every message will be processed successfully. Acknowledgement completes a delivery, repeated failures can send it to the dead-letter queue, and an operator can delete work. Recovery from a database failure also depends on PostgreSQL's durability and backup configuration.

Create a durable queue​

These examples use the SDK 0.4.0 connections from the Quick start, with SQL 0.1.1 installed. They create a topic named events and a durable queue named jobs.

if err := conn.CreateTopic(ctx, "events"); err != nil {
return err
}
if err := conn.CreateQueue(ctx, "jobs", "events", false,
postgremq.WithMaxDeliveryAttempts(5)); err != nil {
return err
}

The false argument selects a durable queue in Go and TypeScript. Rust's default queue options select a durable queue. All three examples allow five delivery attempts.

For deployment-time setup, the equivalent SQL is:

SELECT postgremq.create_topic('events');
SELECT postgremq.create_queue('jobs', 'events', 5, false);

Repeating a declaration with identical settings uses the existing queue and preserves its backlog. Declaring the same name with a different topic, attempt limit, Exclusive setting, or keep-alive interval fails; it does not change the queue. The keep-alive interval is compared even for Durable queues, although it does not expire them; keep that declaration parameter consistent. Queue names accept ASCII letters, digits, _, :, ., and -, up to 57 bytes.

Share work between consumers​

Consumers using the same queue name share its deliveries:

→ worker A
Topic → jobs queue
→ worker B

Publishing one message creates one delivery in jobs. One worker receives that delivery for processing. Adding a worker increases the number of consumers available to process the backlog; it does not create another copy of each message.

If a worker fails before acknowledging, the delivery can be attempted again by the same worker or another one. A timeout can also occur while the original worker is still running. Applications must tolerate repeated effects and respond when the SDK signals that a delivery is no longer theirs to process.

For a runnable worker in Go, Rust, or TypeScript, see Consuming messages. The examples run two processes against jobs and show the completed delivery count.

Give each subscriber its own delivery​

For durable publish/subscribe, create a separate queue for each independent subscriber. A subscriber can itself have multiple workers sharing its queue.

→ subscriber-a queue → consumer A
Topic → each message
→ subscriber-b queue → consumer B

Create both queues before publishing:

if err := conn.CreateTopic(ctx, "events"); err != nil { return err }
for _, queue := range []string{"subscriber-a", "subscriber-b"} {
if err := conn.CreateQueue(ctx, queue, "events", false,
postgremq.WithMaxDeliveryAttempts(5)); err != nil { return err }
}
id, err := conn.Publish(ctx, "events", json.RawMessage(`{"number":1}`))
if err != nil { return err }
fmt.Printf("Published %d to both subscriptions\n", id)

Each queue receives a delivery with the returned message ID. Acknowledging it in subscriber-a completes only that queue's delivery. subscriber-b retains its delivery until its own consumer acknowledges it or its retry policy or an administrative operation removes it.

Run the consumer example with the appropriate queue name for each subscriber. If consumer B disconnects, its queue continues accumulating messages. When it reconnects to subscriber-b, it can process that backlog. Consumer A does not have to wait for B to acknowledge the same message.

To give every application instance its own delivery, each instance needs a distinct queue name. Reusing one name means sharing work, even when the consumers belong to different applications.

Independent delivery progress does not mean isolated database resources. A subscriber that falls behind retains data and contributes to database load. Monitor each queue's backlog and ready-message age, and resolve dead letters as part of operating the subscriber.

Delivery guarantees​

A queue retains pending work across consumer disconnects. Receiving does not remove the delivery; a successful acknowledgement completes it. If a consumer fails before acknowledging, the delivery can be retried after its visibility timeout. Repeated delivery is possible, including when work completed but its acknowledgement was lost.

Retry policy applies independently to each queue. In these examples, a fifth failed attempt ends in the dead-letter queue. A consumer crash on the final attempt requires scheduled postgremq.pmq_maintenance_fast() to retire it after the timeout. The attempt limit is set per queue and defaults to 0 (unlimited); set a positive limit to enable automatic dead-lettering after exhausted attempts.

For FIFO delivery, publish related messages with the same group key. Ordering is per group within each queue and works with both Durable and Exclusive queues. Different groups can proceed concurrently; ungrouped messages have no strict FIFO guarantee. The publishing guide explains the effect of retries and delays on a group.