Idempotency & the outbox pattern
Idempotency keys, exactly-once as idempotent processing (not delivery), transactional outbox for atomic DB-write-plus-publish. The heart of payment-grade design.
The mental model
Idempotency is the property that pressing the button twice is the same as pressing it once. A light switch is idempotent — flip it to "on" and flipping it to "on" again changes nothing. A "add $10" button is not — press it twice and you have $20. The whole discipline of payment-grade backend design is turning the not-idempotent operations (charge a card, apply a credit, place an order) into ones where a retry, a duplicate delivery, or a double-click cannot cause a second effect.
Two problems live here, and they are the two halves of "make this operation happen exactly once no matter how many times it is triggered":
- Idempotency keys — a client attaches a unique key to a request; the server remembers that key and its result, so a retry returns the original result instead of doing the work again.
- The transactional outbox — how to atomically do a database write and publish an event about it, when the database and the message broker are two separate systems that can fail independently.
**Why this exists — what breaks without it.** Networks time out. A client sends "charge $50", the server charges the card, and the response is lost on the way back — so the client, seeing a timeout, retries, and the customer is charged twice. This is not a rare edge case; it is the *guaranteed* consequence of retries meeting non-idempotent handlers, and at any real scale it happens continuously. The interview framing is exact: **retries plus non-idempotent handlers equals double charges.** The outbox solves the sibling problem: if you write the order to the database and then publish an order_placed event and the process crashes in between, you have an order nobody was told about — or, if you publish first, an event for an order that was rolled back. Idempotency and the outbox are the heart of payment-grade design, and fintech interviewers push on both until they find the edge of your knowledge.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision