Start With Transactional Boundaries, Not Broker Sprawl
Many teams jump straight to multi-topic streaming clusters the moment an email notification or webhook needs to run asynchronously. Yet most operational bugs stem from dual-write races: committing a database row while failing to publish the matching event.
Before introducing extra infrastructure, anchor state changes and pending jobs inside the same ACID database transaction using the transactional outbox pattern.
- Write business records and outbox events inside a single SQL transaction
- Enforce idempotency keys on every downstream consumer handler
- Route repeated failures to an inspectable dead-letter table with replay tooling
Writing Idempotent Event Consumers
Network retries guarantee that messages will occasionally arrive more than once. Designing consumers so duplicate deliveries produce the same final state eliminates entire classes of double-billing or duplicate-alert incidents.
export async function handlePayoutEvent(event: { id: string; payoutId: string }) {
const inserted = await db.processedEvents.insertIfAbsent(event.id);
if (!inserted) {
return { status: "already_processed" as const };
}
await db.payouts.markSettled(event.payoutId);
return { status: "settled" as const };
}Practical rule: Reliable queues are built on idempotent handlers and clear replay controls—not complex broker topologies.