Messagingidempotent consumerinbox tableprocessed messages table

Inbox pattern

Definition

The inbox pattern makes a consumer idempotent by inserting the message id into an inbox table with a unique key, inside the same database transaction as the work the message triggers.

In practice

At-least-once brokers redeliver messages after crashes, slow handlers and producer republishes. With an inbox, the consumer starts a transaction, inserts the message id, applies the change and commits. A duplicate fails the unique insert, so the consumer skips it and acks. Paired with the transactional outbox on the producer side, it gives effectively-once processing.

It only works when the inbox and the side effect share one transaction. An email or an external API call cannot join that transaction, so those need an idempotency key passed downstream. The table grows with every message, so prune ids older than the longest possible redelivery window.

Example

INSERT INTO inbox (message_id) VALUES ('evt-5521') and the stock decrement commit together; when evt-5521 arrives again, the insert hits the primary key and the handler acks without changing stock.