Consumidor Idempotente
Inbox
Escrito por Darjus Kazakevic · revisado el
Remember which messages you processed, and skip the second copy.
Brokers deliver at-least-once, so a message may arrive twice. A consumer crashes after the work but before the ack, and the broker sends it again. Stripe resends a webhook for up to three days until your endpoint returns 2xx. If the handler charges a card or adds to a balance, a duplicate does real damage.
An Idempotent Consumer stores the id of every message it has processed, in a table often called an inbox. Before doing the work, it tries to insert the id. The insert and the work commit in one transaction, so each message takes effect exactly once.
En esencia — Insert each message id into a processed-messages table in the same transaction as the work; if the id is already there, skip, so duplicates have no effect.
Cuándo recurrir a él
Recurre a él cuando
- Consuming from an at-least-once broker such as SQS or RabbitMQ
- Receiving webhooks that the sender retries, like Stripe's
- The effect is not safe to repeat: a charge, an email, a counter
Reconsidéralo cuando
- The effect is already idempotent, like setting a value by key
- The sender gives no stable message id: fix that first
- The effect is in another system: use its idempotency key instead
The moving parts
- A stable message id
- A unique id set by the sender, such as an outbox row id or a webhook event id (Stripe's evt_…). Without one, the consumer cannot tell a duplicate from a new message.
- The processed-messages table
- A table keyed by message id, also called an inbox. The primary key is the guard: a second insert of the same id conflicts and inserts nothing.
- Naturally idempotent effects
- Some writes are safe to repeat, such as SET status = 'paid' or an upsert by key. You need the table only when the effect is not, like balance = balance + 50.
Applying it well
- Commit the id and the effect together
- Commit the id alone first, and a crash loses the work. Commit it after the work, and a crash repeats the work. Only one transaction for both, guarded by a unique key, makes a duplicate harmless.
- Let the unique key be the lock
- SELECT then INSERT races: two workers both see no row. Insert with ON CONFLICT DO NOTHING instead. A racing insert waits for the first transaction, then inserts nothing if it committed.
- Ack duplicates; do not reject them
- A duplicate is a success that already happened. Acknowledge it so the broker stops sending it. Rejecting it sends a correct message to the dead-letter queue for no reason.
- Pass the id on to external calls
- An email or payment API is outside your transaction. Send the message id as the provider's idempotency key, such as Stripe's Idempotency-Key header, so its retry is ignored too.
Errores habituales
- The id insert and the effect in separate transactions, so a crash loses or repeats the work.
- Using a delivery id, such as an SQS receipt handle, as the key. It changes on every redelivery.
- Deleting ids sooner than the broker can redeliver, so a late duplicate gets through.
- Using a hash of the payload as the id, so two real, identical orders count as one.