Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Why must event handlers in a typical event-driven microservices system be designed to be idempotent?
💻 Code Challenge | +75 XP
Write an idempotent onPaymentRequested event handler that checks whether an event ID has already been processed (via a persisted set of processed IDs) before charging a card, safely no-op-ing on a duplicate delivery.
A customer was accidentally charged twice for the same order after a message broker redelivered an event following a brief network blip. Reorder the steps to fix the handler to be idempotent.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Writing an event handler that assumes each event will be delivered and processed exactly once
// Wrong: assumes exactly-once delivery, will eventually double-process
async function onPaymentRequested(event) { await chargeCard(event.amount); }
// Correct: idempotent, safe under duplicate delivery
async function onPaymentRequested(event) {
if (await alreadyProcessed(event.id)) return;
await chargeCard(event.amount);
}The Solution //
Most message brokers provide at-least-once delivery guarantees, not exactly-once, meaning duplicate delivery of the same event is an expected, normal occurrence, not an edge case. A handler performing a non-idempotent action (like charging a payment) on every delivery will eventually produce incorrect results, such as a duplicate charge.
The Error //
Attempting a single ACID transaction spanning multiple services' separate databases
// Not possible: a single transaction can't span separate service databases
// BEGIN TRANSACTION; UPDATE inventory_db...; UPDATE payment_db...; COMMIT;
// Correct: Saga pattern, local transactions + compensating actions
reserveInventory() -> compensate: releaseInventory()The Solution //
A traditional ACID transaction cannot span multiple independent databases owned by different services — this is a fundamental constraint of a distributed system, not a technical limitation to work around. The Saga pattern (a sequence of local transactions with explicit compensating actions for rollback) is the standard way to coordinate a multi-step process across service boundaries instead.