System designInterview preparation
Advanced·Concept·2 min read

What are idempotency and the outbox pattern?

Practice this question

Definition:

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:

  1. Start a database transaction.
  2. Save the business change and an outbox row with a stable event ID.
  3. Commit both together.
  4. A publisher reads unsent outbox rows and sends the events.
  5. Mark publication progress; a crash before this step can cause redelivery.
  6. 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.
Adapted from 07_SYSTEM_DESIGN_ARCHITECTURE.md · “What are idempotency and the outbox pattern?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.