If every read took a lock, a busy database would spend most of its time waiting. Multi-version concurrency control, MVCC, avoids that by keeping more than one version of a row. Readers look at an old, consistent version while writers create a new one, and neither waits for the other. The behaviour has a subtlety that causes real bugs, so it is worth understanding properly.
How versions are kept
Every InnoDB row carries hidden fields, including the id of the transaction that last changed it and a pointer into the undo log. When a transaction updates a row, InnoDB writes the new values in place and links the row to the old version stored in the undo log. A chain of such links lets the engine rebuild any recent version of the row.
When a transaction needs to read, InnoDB builds a read view: a description of which transactions had committed at that moment. For each row, the engine follows the version chain until it finds a version that this read view is allowed to see.
When the snapshot is taken
- At REPEATABLE READ the read view is created at the first consistent read in the transaction and reused for the rest of it. Every later read sees the same data, no matter what other sessions commit.
- At READ COMMITTED a fresh read view is created for each statement, so each statement sees the latest committed data.
This is why a long transaction at REPEATABLE READ gives stable reports, and also why it is expensive: InnoDB cannot purge old row versions that an open read view might still need, so undo history grows. Idle transactions that stay open for hours cause real problems.
Two kinds of read
This is the key distinction, and interviewers love it.
- A snapshot read (consistent read) is an ordinary
SELECT. It reads the version from the read view and takes no row locks. - A current read (locking read) reads the latest committed version and locks what it reads.
SELECT ... FOR UPDATE,SELECT ... FOR SHARE,UPDATE,DELETEandINSERTare all current reads.
Inside one transaction you can mix them, and they can disagree. A snapshot read may show a balance of 100 while a current read in the same transaction returns 70 because another session committed in between.
START TRANSACTION;
SELECT balance FROM wallets WHERE id = 1; -- snapshot read: 100
-- another session commits: balance becomes 70
SELECT balance FROM wallets WHERE id = 1; -- still 100 (same snapshot)
SELECT balance FROM wallets WHERE id = 1 FOR UPDATE; -- current read: 70
COMMIT;Why REPEATABLE READ does not stop a double-spend
Suppose the application checks the balance with a plain SELECT and then updates it. Two requests run at the same time. Both snapshot reads return 100, so both decide that a withdrawal of 80 is allowed. Both then write. The first sets the balance to 20, and the second, computing from its stale 100, also sets 20, or subtracts 80 and drives the balance negative. The isolation level did exactly what it promised, a stable view for reads, yet the business rule was broken.
The fix is to make the decision on the latest data, under a lock, in one of two ways.
-- 1) lock the row, then decide in the application
START TRANSACTION;
SELECT balance FROM wallets WHERE id = 1 FOR UPDATE;
UPDATE wallets SET balance = balance - 80 WHERE id = 1;
COMMIT;
-- 2) let one atomic statement check and change
UPDATE wallets SET balance = balance - 80
WHERE id = 1 AND balance >= 80;With the second form the application reads the number of affected rows. One means success, zero means the balance was too low. There is no gap between checking and acting, which removes the whole class of time-of-check to time-of-use bugs.
Practical consequences
- Keep transactions short. Long ones hold read views open and bloat the undo history.
- Use a locking read or an atomic conditional update whenever a decision depends on a value you are about to change.
- Do not mix snapshot reads and writes in the same logic and expect them to agree. A write sees newer data than your earlier
SELECTdid. - If you need a fresh value from a snapshot-based transaction, use a locking read or start a new transaction.
Takeaway
MVCC lets readers and writers run side by side by keeping row versions. A snapshot read shows the version from your read view, a current read shows and locks the latest one. Isolation levels govern the first kind. For money and counters, use the second kind or an atomic update.
Comments