Reference
What is Database transaction?
A database transaction groups several operations together so that they either all succeed or all fail as a unit. It is what prevents an order being recorded while the matching stock movement is not, which would leave the data permanently inconsistent with no way to detect it later.
Also called: ACID transaction · commit rollback
The classic failure it prevents
An order is written, then the process fails before stock is decremented. Without a transaction you have a paid order and stock that says the item is still available, so it sells again.
Where transactions are required
- Order creation with stock movement
- Payment recording with fulfilment
- Webhook idempotency — recording the event id and doing the work must be atomic
- Anything transferring a value from one row to another
The subtlety
Wrapping only the write in a transaction does not prevent a race. The read that decides whether to write must be inside it too, usually with a lock, or two concurrent requests can both read the same stale value.
This is why the bug survives testing. Sequential manual testing never produces two simultaneous reads, so the code appears correct until the first genuinely busy day.
Where this is covered in depth
A definition can only go so far. E-commerce website development: what the whole build actually involves covers this properly — 7 minutes, free, no email required.
Who wrote this
Anas Bin Masud builds e-commerce sites and does technical SEO for businesses in the UK, Canada and Pakistan. These definitions come from client work rather than from a content brief — where an entry describes a mistake, it is usually one found on a real site. More about how I work.