Browse patterns / Service architecture / B031
idempotent consumer

Idempotent consumer

Design a consumer so that processing the same message repeatedly has the same effect as processing it once.

Recorded evidence 14/14Compare →Source
01 / UNDERSTAND

Structure and behavior

Components and placement principles (research-based overview; connection order and topology are not specified)

Components

The key component is a store that holds the inbox or deduplication record and the business side effect in the same transaction.

Placement principles

Persist the idempotency key where the side effect occurs, not solely in volatile pod memory.

Problem addressed

Prevents side effects such as duplicate records or charges under at-least-once delivery.

Expected benefits

Prevents duplicates such as repeated records and double charges, supporting reliability and safe retries.

Trade-offs

Without a stable ID deduplication is impossible, and the effect and deduplication record must be stored in one transaction.

Deduplication is unnecessary for already-idempotent operations such as reads, cache assignment, or primary-key upserts.

02 / APPLY

Operations and adoption guide

Synthesis, distinct from items backed by source excerpts. Validate before applying in production.

When to use

Use for asynchronous payment, order, inventory, or notification messages that could otherwise cause double charges, duplicate sends, or wrong inventory. It is common for brokers with at-least-once delivery or retries.

Scenario

An e-commerce platform handles millions of order events per month and uses messages to authorize payment and award points. The broker delivers at least once, so a consumer can receive an order again during restarts or redeployments. One order must have exactly one payment and reward effect, be safe to replay after failures, and leave an auditable duplicate-processing record.

Placement principles

  • Include a stable message ID, event ID, or idempotency key
  • Store the inbox or deduplication record in the same transaction store as business data
  • Persist the key at the DB or external API boundary where the side effect occurs, not only in pod memory
  • For an external API, use its provider’s idempotency key together with the local inbox
  • Retain processing status and the original message ID for replay and audit.

On premises

Check transactional database and broker high availability, backups and recovery, and independence from restart order. Use idempotent migrations and operational automation, and document rolling restarts and replay assuming duplicate consumption during deployments.

03 / VERIFY

Evidence by criterion (14 total)

Evidence availability at the time of research. Assess source accuracy and currency separately.

Definition and overviewEvidence available

Design a consumer so that processing the same message repeatedly has the same effect as processing it once.

Design message consumers so that processing the same message more than once has the same effects as processing it once.

learn.microsoft.com original ↗

Problem addressedEvidence available

Prevents side effects such as duplicate records or charges under at-least-once delivery.

Messaging systems that guarantee at-least-once delivery can deliver the same message multiple times.

learn.microsoft.com original ↗

Key components and resourcesEvidence available

The key component is a store that holds the inbox or deduplication record and the business side effect in the same transaction.

The inbox only works when the deduplication record and the side effect are in the same transaction

www.dbraven.com original ↗

Data and asset placementEvidence available

Persist the idempotency key where the side effect occurs, not solely in volatile pod memory.

Store that key where the side effect happens, not only in memory inside the pod

kube-dojo.github.io original ↗

Typical use casesEvidence available

Useful for payment, billing, email, and inventory events where repeated effects have financial or trust costs.

Duplicate side effects would cost money or trust: charges, emails, stock changes.

microservicepatterns.com original ↗

Example scenarioEvidence available

Repeated downstream processing of one source event can skew aggregates and repeat database effects.

multiple downstream actions associated with the same source event

tryjuice.dev original ↗

When to avoid and caveatsEvidence available

Deduplication is unnecessary for already-idempotent operations such as reads, cache assignment, or primary-key upserts.

Idempotent operations (read, cache set, upsert by primary key) do not require deduplication

www.dbraven.com original ↗

On premises: development, deployment, and operationsEvidence available

On premises, reduce dependence on restart order and consider idempotent migrations and operational automation.

being agnostic of startup order and performing idempotent migrations

onprem.org original ↗

Cloud: development, deployment, and operationsEvidence available

Apply it in cloud-managed event hubs or functions with at-least-once delivery.

a function triggered by an event hub needs to support the Idempotent Consumer pattern.

learn.microsoft.com original ↗

Kubernetes: development, deployment, and operationsEvidence available

Kubernetes pods can restart, so persist keys with the side-effect store rather than in pod memory.

because a restarted pod will not remember what the previous pod attempted.

kube-dojo.github.io original ↗

Incident responseEvidence available

Check whether the business effect already happened before replaying failed messages.

Before replaying a failed message, determine whether its business effect has already occurred.

backend-engineering-chronicles.github.io original ↗

BenefitsEvidence available

Prevents duplicates such as repeated records and double charges, supporting reliability and safe retries.

Resilience against duplicates ensures that reprocessing a message doesn't create duplicate records, double-charge a customer

learn.microsoft.com original ↗

Drawbacks and trade-offsEvidence available

Without a stable ID deduplication is impossible, and the effect and deduplication record must be stored in one transaction.

Without a stable ID, deduplication has nothing to key on

www.dbraven.com original ↗

Representative tools and technologiesEvidence available

Examples include at-least-once brokers such as Azure Service Bus, Azure Event Hubs, Apache Kafka, and RabbitMQ, as well as inbox or deduplication stores and idempotency keys.

Most brokers, including Azure Service Bus, Azure Event Hubs, Apache Kafka, and RabbitMQ, provide at-least-once delivery.

learn.microsoft.com original ↗

Additional references (22)