Skip to main content

Retries and dead-letter queues

Queue retries repeat application processing. Database-operation retries repeat a failed SQL operation. Configure them separately: a safe retry of an aborted publish transaction is different from retrying a handler whose external request may already have succeeded.

A queue's attempt limit applies independently to each delivery in that queue. Zero means unlimited attempts; use a finite value when repeatedly failing work should leave the ordinary queue for investigation.

Select an attempt policy​

The Durable queue example sets five attempts in every language. Each successful claim increments the count, including the initial claim. Nack does not add another increment; the next claim does. Releasing unattempted work refunds its claim increment.

A finite limit permits at most that many ordinary claims before retirement. An explicit final-attempt nack moves the delivery to the DLQ. If the last worker crashes, fast maintenance moves the exhausted delivery after its visibility timeout. Keep maintenance running even when consumers normally settle promptly.

Delay attempted failures​

These examples operate on a stream delivery named message after a retryable processing failure. They choose a delay of 5, 10, 20, then at most 30 seconds from its attempt number. This is application policy; PostgreMQ does not choose exponential business backoff automatically.

delaySeconds := 5
for attempt := 1; attempt < message.DeliveryAttempt && delaySeconds < 30; attempt++ {
delaySeconds *= 2
}
if delaySeconds > 30 { delaySeconds = 30 }
if err := message.Nack(ctx, postgremq.WithDelayUntil(
time.Now().Add(time.Duration(delaySeconds)*time.Second))); err != nil {
return err
}

For SQL clients, compute the delay from the delivery_attempts returned by the claim and supply the resulting timestamp to nack_message. See SQL usage for retaining the claim token and transaction boundary.

A delayed retry is eligible no earlier than its deadline; it is not promised to start at that instant. On the final allowed attempt the delivery goes to the DLQ instead, regardless of delay. A FIFO head continues to block successors while awaiting its retry.

Distinguish business outcomes​

For transient failures, return the attempted work with an appropriate delay. For invalid data or a permanent application failure, preserve diagnostic context and follow a deliberate policy. The current API has no direct reject-without-requeue operation that immediately moves an arbitrary delivery to the DLQ.

Do not acknowledge failed business work merely to make retries stop unless discarding that work is the intended application outcome. Do not use release for a failure that was actually attempted: it refunds the attempt and can bypass the finite retry policy.

Handler consumers settle errors automatically, but the language contracts differ. Rust can return Err; TypeScript can throw. Go handlers have no error return and must nack explicitly for a business failure. An explicit delayed nack in any handler prevents a second automatic settlement.

Database retry boundaries​

Standalone publishing automatically retries only serialization failure (40001) and deadlock (40P01), which establish that the transaction aborted. An ambiguous transport failure is not proof that publication failed. Application retry can create a duplicate message.

Transaction-scoped publication and acknowledgement do not apply the SDK's internal retry policy. The caller must roll back and retry the whole transaction where appropriate. After an SDK delivery has attempted transactional acknowledgement, it cannot be settled again through that object; an uncommitted delivery needs a new claim after lease expiry. Retrying a single statement inside an aborted transaction cannot repair it.

Keep operational retry budgets bounded and observe the underlying SQLSTATE. SQL errors separates invalid arguments, lost ownership, missing queues, aborted transactions, and uncertain outcomes.

Recover dead letters​

Fix the failure cause before replay. Queue-wide replay resets attempts, so repeatedly replaying an unchanged failure policy can create an unbounded cycle outside the original attempt limit. Grouped replay also returns older work after successors may have completed.

Use Dead-letter queues and replay for inspection, scope, and recovery. Use Idempotent consumers to ensure a retry does not repeat an already completed effect.