{ orderRepository.createFrom(event); processedMessages.insert(msgId, Instant.now()); return null; }); } Implementation strategies: Deduplication table — persist message IDs (or idempotency keys) in a table. Before processing, check if the ID exists. Insert the ID and process in the same transaction so a crash between processing and acknowledging still results in a consistent state on retry. Natural idempotency — design operations that are inherently idempotent. UPDATE orders SET status='CONFIRMED' WHERE id=42 is idempotent; running it twice has no extra effect. But INSERT INTO charges (amount, orderId) VALUES (59.99, 42) is not — it creates duplicate rows. Conditional update — use an optimistic locking version or a state machine check (WHERE status='PENDING') to ensure the operation only applies in the correct state, making reprocessing a no-op if the state has already advanced. The deduplication store must be co-located or transactionally integrated with the main data store, otherwise the check-then-insert itself has a race condition."> { orderRepository.createFrom(event); processedMessages.insert(msgId, Instant.now()); return null; }); } Implementation strategies: Deduplication table — persist message IDs (or idempotency keys) in a table. Before processing, check if the ID exists. Insert the ID and process in the same transaction so a crash between processing and acknowledging still results in a consistent state on retry. Natural idempotency — design operations that are inherently idempotent. UPDATE orders SET status='CONFIRMED' WHERE id=42 is idempotent; running it twice has no extra effect. But INSERT INTO charges (amount, orderId) VALUES (59.99, 42) is not — it creates duplicate rows. Conditional update — use an optimistic locking version or a state machine check (WHERE status='PENDING') to ensure the operation only applies in the correct state, making reprocessing a no-op if the state has already advanced. The deduplication store must be co-located or transactionally integrated with the main data store, otherwise the check-then-insert itself has a race condition." />

Prev Next

API / Microservices Design Patterns Interview Questions

What is the Idempotent Consumer pattern and why is it essential in event-driven systems?

The Idempotent Consumer pattern ensures that processing the same message more than once produces the same outcome as processing it exactly once. It is essential because virtually all message brokers (Kafka, RabbitMQ, SQS) guarantee at-least-once delivery — a message may be redelivered after a consumer crashes before acknowledging, after a network partition, or during broker rebalancing. Without idempotency, redelivery causes duplicate side effects: double charges, duplicate shipments, over-reserved inventory.

// Idempotent consumer using deduplication table
public void handleOrderPlaced(OrderPlacedEvent event) {
    String msgId = event.getMessageId();   // unique per message

    if (processedMessages.exists(msgId)) {
        log.info("Duplicate message {}, skipping", msgId);
        return;   // idempotency guard: already processed
    }

    // process inside a transaction that also inserts the msgId
    transactionTemplate.execute(status -> {
        orderRepository.createFrom(event);
        processedMessages.insert(msgId, Instant.now());
        return null;
    });
}

Implementation strategies:

  • Deduplication table — persist message IDs (or idempotency keys) in a table. Before processing, check if the ID exists. Insert the ID and process in the same transaction so a crash between processing and acknowledging still results in a consistent state on retry.
  • Natural idempotency — design operations that are inherently idempotent. UPDATE orders SET status='CONFIRMED' WHERE id=42 is idempotent; running it twice has no extra effect. But INSERT INTO charges (amount, orderId) VALUES (59.99, 42) is not — it creates duplicate rows.
  • Conditional update — use an optimistic locking version or a state machine check (WHERE status='PENDING') to ensure the operation only applies in the correct state, making reprocessing a no-op if the state has already advanced.

The deduplication store must be co-located or transactionally integrated with the main data store, otherwise the check-then-insert itself has a race condition.

What delivery guarantee makes the Idempotent Consumer pattern necessary in message-driven systems?
How does a deduplication-table approach implement consumer idempotency?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is the Decompose by Business Capability pattern and how do you identify business capabilities? What is the Decompose by Subdomain pattern and how does it relate to DDD Bounded Contexts? What is the Strangler Fig pattern and when should you use it to migrate a monolith? What is the Anti-Corruption Layer (ACL) pattern in microservices? What is the Branch by Abstraction pattern for incremental migration? What is the Parallel Run pattern and how does it reduce migration risk? What is the Bulkhead decomposition pattern and how does it isolate failure domains? What is the Database per Service pattern and what problem does it solve? What is the Shared Database anti-pattern and why should it be avoided in microservices? What is the Saga pattern and how does it manage distributed transactions across microservices? What is the difference between Choreography-based and Orchestration-based Sagas? What is CQRS (Command Query Responsibility Segregation) and when should you use it? What is Event Sourcing and how does it complement CQRS? What is the API Composition pattern for querying data across services? What is the Outbox Pattern and how does it solve the dual-write problem? What is the Saga rollback / compensating transaction pattern? What is the API Gateway pattern and what responsibilities should it have versus a BFF? What is the Backend for Frontend (BFF) pattern and when does it replace a general API Gateway? What is the Service Mesh pattern and how do data-plane proxies such as Envoy implement it? What is the Message Broker pattern and how does it enable asynchronous microservice communication? What is the Request-Reply (Correlation ID) pattern for async messaging? What is the Idempotent Consumer pattern and why is it essential in event-driven systems? What is the Event-Driven Architecture pattern and how does it differ from synchronous request/response? What is Gateway Aggregation versus Gateway Routing versus Gateway Offloading? How does the Circuit Breaker pattern work and what are its three states? What is the Retry pattern with exponential backoff and jitter, and when should you NOT retry? What is the Timeout pattern and how does it prevent cascading failures? What is the Bulkhead pattern for resource isolation (thread pools, connection pools)? What is the Health Check API pattern and what should a /health endpoint return? What is the Rate Limiting pattern and what algorithms are commonly used? What is the Fallback pattern and how does it relate to the Circuit Breaker? What is the Throttling pattern and how does it differ from Rate Limiting? What is the Log Aggregation pattern and how does a centralised logging pipeline work? What is the Application Metrics pattern and what is the difference between push and pull metric collection? What is the Audit Logging pattern and what events should always be captured? What is the Distributed Tracing pattern and how do trace context headers propagate across services? What is the Access Token pattern (JWT/OAuth2) for service-to-client authentication? What is the Mutual TLS (mTLS) pattern for service-to-service authentication? What is the Secrets Management pattern and how do tools like Vault or AWS Secrets Manager implement it? What is the Sidecar pattern and what responsibilities does a sidecar container take on? What is the Ambassador pattern and how does it proxy outbound traffic for a service? What is the Adapter pattern in the context of microservice containers? What is the Canary Deployment pattern and how does it differ from Blue-Green deployment? What is the Service Registry and Discovery pattern — client-side versus server-side discovery? What is the Self Registration versus Third-Party Registration pattern for service discovery?
Show more question and Answers...

BigData

Comments & Discussions