Loading MQ::STREAM...
“Delivery semantics dictate what happens during network partitions and server crashes. At-Most-Once risks message loss. At-Least-Once guarantees delivery but may duplicate messages. Exactly-Once requires idempotent consumers or distributed two-phase transaction commits.”
Navigating At-Most-Once, At-Least-Once, and Idempotent Exactly-Once processing.
// Idempotent Event Consumer Handler
async function handlePaymentEvent(event) {
const existing = await db.processedEvents.findOne({ eventId: event.id });
if (existing) {
console.log('Duplicate event detected. Skipping execution:', event.id);
return;
}
await db.transaction(async (tx) => {
await tx.accounts.incrementBalance(event.userId, event.amount);
await tx.processedEvents.insert({ eventId: event.id, processedAt: new Date() });
});
}Designing business logic to be inherently idempotent (e.g. UPSERT or tracking processed event IDs) makes At-Least-Once queues behave with Exactly-Once correctness at near-zero overhead.