What are idempotency and the outbox pattern?
Practice this questionDefinition:
Idempotency makes repeated requests for one logical operation produce one intended effect. The outbox pattern stores an event record in the same database transaction as the business change, allowing a later publisher to send it reliably. These solve related but different failure windows: duplicate work and losing the message between a commit and publication.
Key points:
-
Idempotency makes repeated processing have one intended effect; enforce keys atomically.
-
Outbox: save business changes and an event record in one database transaction.
-
A publisher sends stored events later; consumers still need duplicate handling.
-
Store a unique operation key and result atomically, and reject reuse of a key for a different request payload.
Outbox flow:
- Start a database transaction.
- Save the business change and an outbox row with a stable event ID.
- Commit both together.
- A publisher reads unsent outbox rows and sends the events.
- Mark publication progress; a crash before this step can cause redelivery.
- A consumer uses the event ID or an idempotent operation to avoid duplicate effects.
Interview point:
- The outbox publisher can resend after a crash, so consumers still need deduplication or idempotent state changes.