Alex pays twenty dollars for his pizza. The bank takes the money out of his account, and then the power goes out, before the money reaches the pizza shop. When the power comes back, Alex has his twenty dollars again. The payment was a transaction, and a transaction is all or nothing. This article shows how the database keeps that promise.
The video tells the same story in five minutes. This is the written version, with the SQL.
One payment, two changes
Both accounts are at the same bank, in one MySQL database:
CREATE TABLE accounts (
owner VARCHAR(64) NOT NULL,
balance INT NOT NULL,
PRIMARY KEY (owner)
) ENGINE=InnoDB;
INSERT INTO accounts VALUES ('Alex', 100), ('Pizza Shop', 500);
A payment looks like one step, but it is two changes:
UPDATE accounts SET balance = balance - 20 WHERE owner = 'Alex';
UPDATE accounts SET balance = balance + 20 WHERE owner = 'Pizza Shop';
Each change alone
If the app just sends those two statements, MySQL treats each one as its own piece of work. That is autocommit, and it is on by default:
SELECT @@autocommit;
-- 1
The manual says that with autocommit on, "each statement is atomic, as if it were surrounded by START TRANSACTION and COMMIT." So the first change is committed and saved before the second one is even sent. If the power goes out between them, Alex has 80 and the shop still has 500. The total was 600 and now it is 580. Twenty dollars has vanished, because nobody told MySQL that the two changes belong together.
Start a transaction, then commit
So the app has to tell MySQL:
START TRANSACTION;
UPDATE accounts SET balance = balance - 20 WHERE owner = 'Alex';
UPDATE accounts SET balance = balance + 20 WHERE owner = 'Pizza Shop';
COMMIT;
Only the app knows when the work is complete, so the app says it. COMMIT means: I'm finished, keep all of it. BEGIN works too; the manual calls it an alias of START TRANSACTION.
The commit note, and done
MySQL writes every change into its log as a small note, the write-ahead log from part 2 of this series. The commit gets a record in the log too. With the default setting, InnoDB flushes the log to disk at every commit, and only then does MySQL say done.
So "all" means both changes and the commit, safely on disk.
Rollback, and the old value
Sometimes the app can't finish. MySQL has already taken twenty dollars from Alex, and then the app finds the shop's account closed. So the app says:
ROLLBACK;
ROLLBACK means: cancel this transaction and put everything back.
You might guess that MySQL adds the twenty back. It doesn't, because that would not work for every kind of change. Instead, before MySQL changes a row, it keeps the old value. That record is the undo log. The manual says an undo log record "contains information about how to undo the latest change by a transaction to a clustered index record", and every row points to its undo record through a roll pointer.
Alex's balance was 100, so MySQL keeps 100. On a rollback, it puts the old value back, and Alex has 100 again, as if the payment never started. So "nothing" means every old value is back.
The power cut, again
Now go back to the power cut, this time inside a transaction. MySQL took twenty dollars from Alex, and the power went out before the commit.
When the power comes back, MySQL first repeats every change it finds in its redo log. Alex's change is in the log, so for a moment Alex is at 80. Then MySQL looks for transactions that never committed. Alex's payment is one of them. The old value survived the power cut, because the undo log is protected by the redo log like everything else. So MySQL rolls the payment back, and Alex has 100 again.
One detail from the manual: after the redo log is applied, MySQL takes new connections as early as it can, and "the rollback is performed by a background thread, executed in parallel with transactions from new connections."
The commit is the line
If the power goes out before the commit is on disk, the payment never happened. If it goes out after, the payment stays, even if the app never heard done.
So if your app never hears done, ask the database before you pay again. Look the payment up by its own id, or send the retry with the same request id as the first try, so the bank can see it is the same payment.
What this does not cover
- If the connection drops before COMMIT, MySQL rolls the transaction back.
- If one statement fails, MySQL usually undoes only that statement, and the app decides whether to commit or roll back the rest. A deadlock is different: InnoDB rolls back the whole transaction.
- Two banks mean two databases, and one transaction lives inside one database. Moving money between them takes more than a transaction, and that comes later in the series.
The name: atomicity
All or nothing has a formal name, atomicity. It is the A in ACID. The other three, in plain words: C, the rules you set still hold. I, each transaction runs as if it were alone. D, done stays done.
The law
A transaction is all or nothing. The commit decides which.
Go deeper
- START TRANSACTION, COMMIT and ROLLBACK (MySQL 8.4 Reference Manual)
- autocommit, commit and rollback (MySQL 8.4 Reference Manual)
- Undo logs and InnoDB multi-versioning (MySQL 8.4 Reference Manual)
- InnoDB recovery (MySQL 8.4 Reference Manual)
- InnoDB and the ACID model (MySQL 8.4 Reference Manual)
- Transactions (PostgreSQL 18 tutorial): "it bundles multiple steps into a single, all-or-nothing operation"
Next time
This is part 3 of Database Internals. Next time, Alex pays two shops at the same moment. One payment rolls back while the other commits. We'll find out what's left in his account.
Alex, the pizza shop, the bank and the amounts are an invented example. Have you ever found data half changed after a crash or a failed request? What was missing?
Top comments (0)