Handle the same message twice without doing the work twice, by remembering what you already processed.
An idempotent consumer is a message receiver that can handle the same message twice and still do the work only once. The inbox pattern is the most reliable way to build one: the consumer saves the message ID and does the work in one database transaction.
Imagine a consumer that reads a message saying "pay $10 for order 7". It pays, and then it must ack (confirm) to the broker. If the consumer crashes, or the ack gets lost, the broker thinks the message was never handled and delivers it again. A naive consumer then pays a second time.
Repeats happen for many ordinary reasons:
Because repeats are normal, the receiver must make them harmless. There are two ways:
| Technique | How it works | Good for | Watch out |
|---|---|---|---|
| Natural idempotency | Design the action so repeating it changes nothing: set a value, upsert by ID, delete by ID | Updates that overwrite state | Not possible for 'add 10' or 'send email' |
| Processed-ID check | Look up the message ID in a table, act, then record the ID | Simple cases with one worker | Races and crashes between the steps (see below) |
| Unique constraint | Insert the ID with a primary key. A duplicate insert fails or does nothing | Any database with unique keys | Must be in the same transaction as the work |
| Inbox pattern | Insert the ID and do the work in one transaction, then ack | Anything that changes your own database | Needs a table and clean-up |
| Version check | Apply a change only if its version is newer than the stored one (optimistic check) | State that has a version or sequence number | Needs a version in each message |
| Idempotency key to another service | Send the message ID along with the outside call, so that service ignores repeats | Payments, email and other outside APIs | The other service must support it |
CREATE TABLE inbox (
message_id TEXT PRIMARY KEY, -- from the producer
processed_at TIMESTAMP NOT NULL
);
BEGIN;
-- Fails (or inserts 0 rows) if this message was already handled
INSERT INTO inbox (message_id, processed_at)
VALUES ('evt-8841', now())
ON CONFLICT (message_id) DO NOTHING;
-- Only do the work if the insert really inserted a row
UPDATE accounts SET balance = balance - 10 WHERE id = 7;
COMMIT;
-- then ack the brokerDoes the work for every delivery, then acks. It keeps no record, so it cannot tell a repeat from a new message.
A simplified teaching model with no random numbers, so the same settings replay the same story. Messages m1 to m6 each pay $10 from an account that starts at $100 (made-up numbers), so the right final balance is $40. Message m3 is always delivered twice (its first ack is lost). A crash always hits m5, and the broker then delivers m5 again. The naive consumer keeps no record, so both crash choices behave the same for it.
| Term | Meaning in easy words |
|---|---|
| Idempotent | Doing it more than once has the same effect as doing it once |
| Idempotent consumer | A receiver that can safely handle the same message more than once |
| Inbox pattern | Save the message ID and do the work in one transaction, to ignore repeats |
| Inbox table | A table of message IDs already handled, with a unique key |
| At-least-once delivery | Every message arrives, but some may arrive more than once |
| Effectively-once | At-least-once delivery plus idempotent handling, so the effect happens once |
| Ack | A confirmation to the broker that the message was handled |
| Redelivery | The broker sending a message again because it never got an ack |
| Unique constraint | A database rule that refuses a second row with the same key |
| Idempotency key | An ID sent with a request so the receiver can spot repeats |
| Dedupe window | How long you keep message IDs, so repeats within that time are caught |
| Choice | We gain | We lose |
|---|---|---|
| Inbox table | Strong protection against repeats | One more write and some storage per message |
| Natural idempotency instead | No table, no clean-up | Only works for overwrite-style actions |
| Long retention of IDs | Catches very late repeats | A bigger table and slower clean-up |
| Short retention | A small, fast table | A very late repeat may slip through |
| Same database for inbox and work | One atomic transaction | Couples the inbox to that database |
| Check-then-act without a transaction | Easy to write | Races and crash gaps can still cause duplicates |