Distributed Systems

Idempotent consumers: the pattern that keeps distributed systems boring

At-least-once delivery is not a bug, it is the guarantee most brokers give you. Idempotent consumers turn that guarantee into a feature. A walkthrough of the deduplication, upsert, and replay patterns that quietly hold event-driven systems together.

6 min readBy Gonzalo Patino
producerpublish evtat-least-once busevt-42copy 1evt-42copy 2idempotent consumerseen(evt.id) ?processed-events logprocesscopy 1dropcopy 2

Every event broker worth using promises at-least-once delivery, not exactly-once. Under retries, network partitions, or a consumer restarting mid-batch, the same event will be delivered again. If the consumer treats every delivery as a new fact, the system produces duplicates, double-charges, phantom notifications, and inconsistent projections. The fix is not to hunt for exactly-once semantics. The fix is to make consumers idempotent, so redelivery is safe and boring.

Two things every event needs

An idempotent consumer needs two guarantees from the event itself. First, a stable event identifier that is generated at the source and does not change on redelivery. Second, an event-time timestamp that reflects when the event happened, not when it was processed. With those two fields, the consumer can decide whether it has already handled this event and, if it has been handled out of order, whether the new event is still relevant.

Deduplication by processed-event log

evt-42event.idBEGIN TRANSACTIONprocessed_eventsevt-40evt-41evt-42insertevt-43ordersorder_42 status=paidorder_41 status=paidbusiness writeCOMMIT — atomic
The processed-events log lives inside the same transaction as the business write. Insert and change commit together, or not at all.

The simplest pattern is a processed-events table: a small store of event IDs the consumer has already acted on, with a retention window that comfortably exceeds the broker’s redelivery window. On every event, the consumer checks the log inside the same transaction as the business write. If the ID is present, drop the event. If not, do the work and insert the ID.

This works because the check and the write are atomic. The consumer never gets into a state where the business change happened but the log update did not, or vice versa.

Upsert-based projections

For projections and materialized views, the deduplication log is often unnecessary. If the projection is derived from the latest event for a given entity, upserts with a monotonic version number are enough. On each event, upsert the row when the incoming event-time is greater than the stored one. Older events are silently dropped. Duplicates converge to the same state.

The invariant is simple: two consumers replaying the same event stream, in any order, converge to the same projection.

Side effects live outside the transaction

database (single txn)ordersorder_42 status=paidbusiness writeoutboxsend_receipt(order_42)intent to publishoutbox publisherpoll → dispatchretry until successemail APIexternalmay fail
The outbox pattern separates "we decided to send" from "we successfully sent". The publisher retries until the external call succeeds.

Idempotent consumers are easy for database writes. They are harder for side effects: sending an email, calling an external API, publishing another event. Those side effects cannot participate in the local database transaction.

The transactional outbox pattern solves this. Instead of calling the external system directly, the consumer writes an outbox row in the same transaction as its business write. A separate publisher reads the outbox and dispatches the side effect, marking it complete on success. If the publisher fails or restarts, it retries. If the external system fails, the outbox row stays until it can be delivered. The business write and the intent to produce a side effect are now atomic; the actual side effect is retried until it succeeds.

Dead-letter handling is part of the design

Not every event will succeed on retry. A malformed payload, a stale reference to a deleted entity, a bug in a specific consumer version. If retries loop indefinitely, they starve every other event on the partition.

A dead-letter queue with a clear retry budget and observability is not optional. Send the event there after a bounded number of retries, alert on non-empty dead-letter queues, and keep a tool that can replay them once the underlying issue is fixed. The goal is never zero dead-letters; the goal is knowing when dead-letters appear and being able to act on them.

What idempotency gets you

  • Safe redelivery: any event can be replayed without corrupting state
  • Safe recovery: after an incident, you can replay from an earlier offset without cleaning up manually
  • Safe deploys: consumers can be restarted, scaled, or rolled back without coordinating with the broker
  • Cheaper testing: contract tests can send the same event twice and assert the state is unchanged

Distributed systems become boring when the interesting failure modes stop being interesting. Idempotent consumers are one of the few patterns that reliably move failures from "outage" into "log line". That is the point.

Enjoyed this piece?

Share it with a colleague or on your feed. It helps more than you think.