ACID: atomicity and durability
medium 26 minA transaction turns several changes into one: all of them happen or none do, and once committed they survive a crash.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Money, stock, seats | One transaction around the change | Half done is worse than failed |
| Order plus message | Outbox in the same transaction | Two systems cannot share a commit |
| Must not lose data | Synchronous log on two machines | One disk can die |
| Very many small writes | Group commit | One flush for many |
| Page views, metrics | Asynchronous commit may be fine | Losing a moment does no harm |
Options
Compare your moves
Checkout writes an order and charges a card at another company. Where do you put the charge?
BreaksInside the database transaction
- How
- BEGIN, save, charge, COMMIT.
- Limit
- A failed commit leaves a charge without an order, and locks wait for the network.
- Choose when
- Never.
WorksAfter the commit, in the same request
- How
- Save the order as pending, then charge.
- Limit
- A crash in between leaves orders pending until someone checks.
- Choose when
- Small volume with a repair job.
StrongPending order, outbox and worker
- How
- One transaction for order and outbox row. A worker charges with an idempotency key and marks the order paid.
- Limit
- The result comes a little later, and more parts to run.
- Choose when
- Payments that must not get lost or happen twice.