Messaging

Idempotency, deep dive: why a duplicate check is not enough

Mauro Servienti
Messaging

A deep dive into what idempotency really requires in a message-based system: atomic checks, repeatable outgoing messages and versioned data, plus idempotency keys for the HTTP edges. Duplicates are a when, not an if.

Key points

  • Idempotent means the same state and the same observable side effects, not just one stored row.
  • State-style messages such as setting a price to 120 are naturally idempotent; deltas are not.
  • Check-then-act breaks under scale-out: two instances can both find no shipment and insert two.
  • Make the check atomic with a unique constraint, optimistic concurrency or a conditional write.
  • On a duplicate, still publish outgoing messages, ideally with a stable message identifier.
  • Version the data and ignore messages whose version is older than or equal to the stored one.

Why it matters

Most teams I meet close idempotency with a single unique index and move on, and this piece shows exactly where that stops working: once competing consumers scale out, or once a publish is added after the insert. If you run NServiceBus, MassTransit or Wolverine, the outbox already covers the state-plus-outgoing-messages pairing, so the real homework is elsewhere. Audit which handlers lean on a natural check, push that check into the database as an atomic operation, and give published events stable identifiers. Then compare your deduplication retention with the longest realistic error-queue retry window — that gap is where duplicate side effects quietly come back.

Real-world use

  • An order-shipping handler that must end up with exactly one shipment and one OrderShipped event per order.
  • A price-update flow where messages from Sales can be redelivered and arrive out of order.
  • An HTTP client retrying a POST, where the server needs an Idempotency-Key to replay the original outcome.
  • Retrying a failed message from the error queue weeks later, after deduplication records have expired.

This summary was generated by the agent from the original post on Mauro Servienti. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message