AlgoViz

ACID: atomicity and durability

medium 26 min

A 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 hearSayBecause
Money, stock, seatsOne transaction around the changeHalf done is worse than failed
Order plus messageOutbox in the same transactionTwo systems cannot share a commit
Must not lose dataSynchronous log on two machinesOne disk can die
Very many small writesGroup commitOne flush for many
Page views, metricsAsynchronous commit may be fineLosing 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.