Transaction
ELI5 — The Vibe Check
A transaction groups multiple database operations into one all-or-nothing bundle. Either ALL of them succeed, or NONE of them happen. Transferring money: subtract from one account AND add to another. If either fails, both are rolled back. No partial disasters.
Real Talk
A transaction is a unit of work that groups one or more SQL operations into an atomic sequence. Transactions follow ACID properties. They begin with BEGIN (or START TRANSACTION), and end with COMMIT (save changes) or ROLLBACK (undo changes). Savepoints allow partial rollbacks within a transaction.
Show Me The Code
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- or ROLLBACK if something goes wrong
When You'll Hear This
"Wrap the payment logic in a transaction to prevent partial updates." / "The transaction was rolled back due to a constraint violation."
Related Terms
ACID (Atomicity, Consistency, Isolation, Durability)
ACID is the four guarantees a reliable database makes about transactions. It is the reason you trust a bank's database with your money.
Atomicity
Atomicity means a transaction is all-or-nothing — like an atom that cannot be split.
Deadlock
A deadlock is when two things are each waiting for the other to go first, and neither ever does. Thread A holds Lock 1 and wants Lock 2.
Locking
Locking prevents two transactions from modifying the same data at the same time. It is how databases coordinate concurrent access.
Rollback
A rollback is the panic button. When you deploy something and it breaks production, you hit rollback and the system reverts to the last working version — l...