Skip to content
Patterns
Symptom

Duplicate messages after a retry

Consumers see the same message twice after a timeout or a restart. Find the cause in two checks, and learn when the fix is not Outbox or Retry.

What you see

After a timeout or a restart, consumers process the same event twice. The database holds one order row. The broker holds two messages for it.

Why it happens

The service writes to its database and publishes as two separate steps. When the broker stores a message and the acknowledgement is lost, a retry sends it again. The broker then holds two copies.

The missing pattern

How it happens

The handler commits the order, then calls the broker. The broker accepts the message, but the acknowledgement is lost to a timeout. The client library retries. Nothing in the second send says it is the same message, so the broker stores both.

A crash opens a gap on each side of the publish. If the process dies before the publish, the message is lost. If it dies after the publish but before it records the work as done, a replay publishes again.

A Transactional Outbox closes the loss, because the message commits with the order. Its relay can crash after the broker accepts a message and then publish it again. Give each message an id when you write it, and make every consumer deduplicate by that id.

Diagnosis

Run every check. Each check that matches names a missing pattern.

  1. Check 1

    The publish call runs after the database commit, in the same handler, with no shared transaction.

    It proves: a dual write. A crash before the publish loses the message, and a replay after the publish sends it again. An Outbox stops the loss, but its relay can publish twice, so consumers must deduplicate by message id.

  2. Check 2

    Producer logs show a send that timed out, then a retry, and the broker holds both copies.

    It proves: a retry without an idempotency key. The first send landed. The broker cannot tell the retry from a new message.

When it is not this pattern

  • The duplicates appear after a consumer crash or a deploy, and the producer sent each message once.Consumer redelivery before the offset commit. You need an idempotent consumer that records the message ids it processed.
  • The duplicates cluster around a consumer group rebalance.Partitions moved before the offsets were committed. Commit offsets when partitions are revoked, and make the consumer idempotent.