Skip to content

Lesson 6 of 9 · 25m

At-Least-Once Reality: Duplicates, Ordering, and Idempotent Consumers

Accept RabbitMQ's real delivery semantics and build consumers that survive duplicates, retries, redelivery, and imperfect ordering.

The phrase "exactly once" causes more architectural damage than most outages. In distributed messaging, exactly-once side effects are not a broker feature you can buy. RabbitMQ's practical model is at-least-once when you use acknowledgements and retries correctly. That means duplicates are not anomalies; they are the cost of not losing work. A production consumer that cannot tolerate a duplicate is not production-ready.

Where duplicates come from

Duplicates appear in boring, legitimate ways:

consumer commits database write -> connection drops before ack -> redelivery
publisher sends -> confirm lost during reconnect -> publisher retries
retry queue dead-letters back after partial processing
operator replays failed messages after bug fix
load balancer or client library retries an ambiguous publish

Any design that treats redelivery as impossible will eventually corrupt state. Usually not immediately, which is why the flaw survives testing.

Idempotency is a datastore contract

The consumer needs a stable operation key. Sometimes it is the message ID; sometimes it is the business operation ID, which is better because replays and publisher retries can preserve it.

CREATE TABLE processed_operations (
    operation_id text PRIMARY KEY,
    consumer_name text NOT NULL,
    processed_at timestamptz NOT NULL DEFAULT now()
);

A simplified consumer transaction looks like:

with db.transaction():
    inserted = db.execute(
        "INSERT INTO processed_operations(operation_id, consumer_name) VALUES (%s, %s) ON CONFLICT DO NOTHING",
        operation_id,
        "invoice-worker",
    )
    if inserted.rowcount == 0:
        duplicate = True
    else:
        apply_business_effect(db, payload)

Then acknowledge after the transaction commits. This is not glamorous, but it is the difference between a retry and a duplicate invoice.

Idempotency keys must match business identity

Do not use a random publish ID if the operation itself has identity. If order_created for order 018f-... is replayed through a new message ID, a message-ID-only dedupe table will process it twice. Dedupe on the business operation where possible:

bad key:  random message id generated per retry
good key: order_id + operation type
also key: payment provider event id for webhook ingestion

For operations that cannot be naturally idempotent, use state transitions. UPDATE payments SET status='captured' WHERE payment_id=$1 AND status='authorized' is safer than blindly incrementing state.

Ordering is narrower than teams assume

RabbitMQ generally preserves order within a queue for a single consumer under simple conditions. Add competing consumers, redelivery, retries, priority, or multiple queues and strict ordering disappears as a usable guarantee. If order matters, include sequence numbers or version vectors and design the consumer to buffer, reject stale updates, or rebuild state.

{
  "aggregate_id": "order-9182",
  "sequence": 41,
  "type": "order.address.changed"
}

Without a sequence or version, the consumer cannot distinguish an old replay from a new fact.

Decide what must be true, not what sounds good

Ask these questions per workflow: may we lose a message, may we process twice, may we process out of order, and how quickly must a human know? A metrics event and a ledger movement deserve different answers. The right guarantee is the cheapest one that keeps the business invariant intact.

Field notes

  • RabbitMQ transactions exist, but they are heavy and still do not magically make downstream side effects exactly once.
  • Deduplication tables need retention and ownership; an unbounded table becomes its own operational incident.
  • Idempotency does not help if the ack happens before the transaction commit.
  • Replays are production events. Treat them with the same care as deploys because they can duplicate side effects at scale.

Was this lesson helpful?

Sign in to rate