Case IV · Idempotent consumers
The Twice-Told Message
A queue promises to deliver every message at least once. Read the promise again: at least.
I · Departure
Two systems and a queue between them
ClientPulse turns customer feedback, such as app reviews and support tickets, into product analytics. I co-founded it and wrote its backend. The backend sent tasks over RabbitMQ to an AI engine: export reviews, extract highlights, build a topic taxonomy, categorize. The engine reported the status back.
II · Initiation
The answer that never arrived
A consumer does the work and then sends an acknowledgement. If the acknowledgement is lost, the broker cannot tell "not done" from "done, but I did not hear". It sends the message again. This is correct behaviour, and it is the reason for the rule: a consumer must give the same result when it sees the same message twice.
The simplest way is to remember the ID of each message and skip the ones already seen. The memory and the effect must be saved together, in one transaction.
Sandbox · Idempotent consumers
A model, not production data
Press the steps from 1 to 3 and read the account statement at the bottom.
III · Return
What it cost
A table of seen IDs grows, so it needs a time limit. And the check costs one more read for every message. In return, retries become boring, which is the best thing a retry can be.
Patterns in this case
What to call these things
- At-least-once delivery Messaging
- The queue sends a message again until it hears "done". No message is lost; some arrive twice. In the sandbox: the filled envelopes are second deliveries.
- Idempotent Consumer Messaging
- Handling the same message twice gives the same result as handling it once. In the sandbox: step 3.
- Inbox Messaging
- The consumer saves the ID of each handled message in the same transaction as the work itself, and checks it first. In the sandbox: the "seen IDs" list inside the payment service.
- Transactional Outbox Microservices
- The sending side has the same problem in reverse: it saves the message in its own database with the business change, and a relay publishes it later. In the sandbox: not drawn; it is what makes the left box trustworthy.
- Dead Letter Queue Messaging
- A message that keeps failing is moved aside after a few tries, so it does not block the others. In the sandbox: not drawn.