Idempotent consumer
Design a consumer so that processing the same message repeatedly has the same effect as processing it once.
Structure and behavior
Components and placement principles (research-based overview; connection order and topology are not specified)
The key component is a store that holds the inbox or deduplication record and the business side effect in the same transaction.
Persist the idempotency key where the side effect occurs, not solely in volatile pod memory.
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.
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.
Cloud
Confirm managed queues’ or event services’ at-least-once and redelivery behavior. Give functions or consumers idempotency keys and durable deduplication storage. Configure autoscaling, retry policy, a DLQ, and monitoring together.
Kubernetes
Pods may restart or be rescheduled at any time. Store keys and processing status durably, such as in a PVC, database, or Redis. Operate readiness, graceful shutdown, retry backoff, DLQ, and throughput metrics.
Incident response
Typical issues include duplicate payment or notifications, a downstream timeout with an uncertain outcome, and replay storms. First inspect per-message history and check whether the business effect already happened. If so, skip or compensate; otherwise replay safely with the key. Pause consumption or back off for dependency outages, isolate invalid data in a DLQ, and rate-limit large replay jobs.
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.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.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 transactionData 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 podTypical 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.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 eventWhen 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 deduplicationOn 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 migrationsCloud: 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.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.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.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 customerDrawbacks 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 onRepresentative 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.Additional references (22)
- Idempotent Consumer Pattern - Azure Architecture Center | Microsoft Learn ↗
- Handling duplicate messages using the Idempotent consumer pattern ↗
- Idempotent Consumer pattern: when to use it, trade-offs and pitfalls · MicroservicePatterns.com ↗
- Inbox Pattern (Idempotent Consumer): DBRaven ↗
- Inbox Pattern: Idempotent Message Consumers in .NET | Relay ↗
- Idempotency | Event-driven Architecture on AWS ↗
- Resilient Design Guidance for Event Hubs and Functions - Azure Architecture Center | Microsoft Learn ↗
- REL04-BP04 Make mutating operations idempotent - AWS Well-Architected Framework ↗
- Automated Multi-Cloud Large Scale K8s Cluster Lifecycle Management ↗
- Welcome - kOps - Kubernetes Operations ↗
- akmukhi/Control-Plane ↗
- Idempotent Consumer Pattern - Azure Architecture Center | Microsoft Learn ↗
- mr-kaakyire/BOOTK8S ↗
- Cloud Native Architecture | ModernOnPrem ↗
- Module 9.2: Managed Message Brokers & Event-Driven Kubernetes | KubeDojo ↗
- Orchestrating Kubernetes Cluster Deployment with Kubespray and Progress Chef 360 ↗
- Idempotent Event Consumers: Patterns & Library ↗
- Idempotent Consumer ↗
- Idempotent Reader ↗
- Dead Letter Queue Microservices: Poison Messages & Replay · OneEnv ↗
- The Duplicate Event That Cost Us More Than the Outage · JUICE ↗
- Building Reliable Kafka Consumers: Backpressure, Idempotency, Retries, and Failure Recovery - Backend engineering chronicles ↗