What you need to know
A transaction groups database changes so they succeed or fail as a unit. That matters for operations such as moving money between accounts or creating a parent record with dependent rows.
How to apply it
Keep the transaction as short as possible, perform the checks before writing where feasible, and handle rollback errors. Add constraints to the database so invalid relationships remain impossible.
Common mistake to avoid
Do not keep a transaction open while waiting on a slow external API. Long transactions can hold locks and affect other users.
