Implement the Inbox pattern so duplicate messages cause only one consumer effect
The Inbox pattern records a message’s processing ID together with the consumer’s business change so broker redelivery does not apply the same side effect twice. Checking an ID and then doing the work in a separate step leaves a failure window that can repeat the work; put the duplicate marker and local data change in the same transaction. Side effects outside that transaction, such as a third-party payment or email, need a separate idempotency key or follow-up outbox.
The Inbox pattern records a message’s processing ID together with the consumer’s business change so broker redelivery does not apply the same side effect twice. Checking an ID and then doing the work in a separate step leaves a failure window that can repeat the work; put the duplicate marker and local data change in the same transaction. Side effects outside that transaction, such as a third-party payment or email, need a separate idempotency key or follow-up outbox.
Why messages arrive more than once
An at-least-once broker retries delivery so a message can reach a consumer despite failures. A producer may resend after missing an acknowledgement, and a broker may redeliver if a consumer saves its business change but stops before acknowledging. This is not only because messaging is not exactly-once: it is also difficult to include side effects in external systems, which the broker cannot control, in one atomic operation with message delivery.
Inbox processing flow
**Choose a stable message ID.** Use a producer-assigned event ID or logical operation ID that stays the same across redelivery. Do not use a delivery tag regenerated by the broker, receive time, or a correlation ID shared by several messages.
**Scope the processing key by consumer.** If multiple subscribers independently process the same message, use `(consumer or subscriber ID, message ID)`. A shared store keyed only by message ID could let the first consumer suppress processing by the others.
**Record the duplicate marker and local business change in one transaction.** Insert the key into an inbox table, update business data, and commit both together. A unique constraint or primary key ensures only one concurrent insert succeeds. If the key already exists, treat the delivery as a duplicate and acknowledge it without repeating the change. If the business update fails, roll back the whole transaction so a later redelivery can safely retry.
**Acknowledge after commit.** Acknowledging before business data and the Inbox marker are durable can lose work. If the process stops after commit but before acknowledgement, redelivery should be filtered by the Inbox key.
**Prevent concurrent duplicates in the data store.** Two consumers may receive the same message simultaneously and both observe that it has not been processed. A prior lookup alone does not solve this race. Use a uniqueness constraint, atomic conditional insert, or equivalent atomic storage operation; treat a conflict as a duplicate.
**Set retention and ordering requirements.** Deleting Inbox keys too soon can let a late redelivery or dead-letter replay repeat a side effect. Retain them longer than the broker redelivery window and operator replay policy, while accounting for storage cost. Deduplication does not guarantee message order; design versions, sequence checks, or partition/session guarantees separately if order matters.
Handle external side effects separately
A third-party API call that cannot join the database transaction cannot be made atomic with Inbox processing. Consider a state machine that records an in-progress status before the external call and stores the outcome afterward, or a downstream outbox that commits the consumer’s local change and follow-up intent together. Pass a stable idempotency key to the third-party API and reconcile uncertain outcomes after failures. These approaches still depend on the external API’s behavior and result-query contract.
How to verify
Test redelivery of the same message, the same event processed by distinct consumers, two consumer instances handling it concurrently, process termination after the business change and just before ACK, and dead-letter replay after Inbox retention. Confirm each consumer handles its own copy, local effects occur once, and a failed local transaction can be retried. Verify separate idempotency and recovery for external effects. No live broker or database was run or tested for this article.
Sources
[Microservices.io: Idempotent Consumer pattern](https://microservices.io/patterns/communication-style/idempotent-consumer.html)
[Microsoft Azure Architecture Center: Idempotent Consumer pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/idempotent-consumer)