Lesson 0005 · Internals & Performance
Why two transactions running at once don't corrupt each other — and what "REPEATABLE READ" actually promises, built on a snapshot you can't see.
Lessons 0001–0004 were about a single query reaching rows quickly. Now we turn to the other half of the mission: what happens when many transactions touch the same rows at the same time. The tool MySQL gives you is the isolation level, and the machine underneath it is MVCC. Understand the snapshot and the whole topic — including the notorious "REPEATABLE READ vs READ COMMITTED" interview question — falls into place.
Concurrency can corrupt a read in three escalating ways. The isolation level is just a dial for which of these you tolerate:
| dirty read | You see another transaction's uncommitted change — which may be rolled back. |
| non-repeatable read | You read a row twice in one transaction and get different values (someone committed an UPDATE between). |
| phantom read | You run the same WHERE twice and new rows appear (someone committed an INSERT between). |
A plain SELECT in InnoDB is a consistent nonlocking read: it
reads a snapshot of the database at a point in time via multi-version
concurrency control (MVCC) — not the live rows, and it sets no locks.
So readers never block writers and writers never block readers. The isolation level only
decides when that snapshot is taken.
— MySQL Manual: "InnoDB uses multi-versioning to present to a query a snapshot of the database… A consistent read does not set any locks."
| Level | Snapshot rule | Allows |
|---|---|---|
| READ UNCOMMITTED | Reads even uncommitted versions. | dirty, non-repeatable, phantom |
| READ COMMITTED | Fresh snapshot per statement. | non-repeatable, phantom |
| REPEATABLE READ (default) | Snapshot fixed at the first read. | phantom (largely prevented in InnoDB) |
| SERIALIZABLE | Like RR, but plain SELECT becomes locking. | nothing — full isolation |
Both REPEATABLE READ and READ COMMITTED use MVCC snapshots. The
only difference is snapshot timing:
In the SQL standard, REPEATABLE READ still permits phantom rows. InnoDB is stronger: because
a plain SELECT reads the first-read snapshot, newly inserted rows simply
aren't in that snapshot — so they don't appear. (For locking reads like
SELECT … FOR UPDATE, InnoDB adds next-key locks to block the inserts too — that's
Lesson 0006.)
Answer from memory — effortful recall is what builds retention. Feedback is immediate.
InnoDB's default transaction isolation level is:
A plain SELECT in InnoDB reads from:
Seeing another transaction's uncommitted change is a:
Under REPEATABLE READ, the read snapshot is fixed at:
Under READ COMMITTED, each consistent read:
A consistent (nonlocking) read in InnoDB:
Open two mysql sessions (call them A and B) against the same table
— reuse people from earlier lessons.
-- Session A
START TRANSACTION;
SELECT first_name FROM people WHERE id = 1; -- e.g. 'Ann' — snapshot taken here
-- Session B (separate connection)
UPDATE people SET first_name = 'CHANGED' WHERE id = 1; -- autocommits
-- Back in Session A — same transaction
SELECT first_name FROM people WHERE id = 1; -- STILL 'Ann' — reads the frozen snapshot
COMMIT;
SELECT first_name FROM people WHERE id = 1; -- now 'CHANGED' — new transaction, new snapshot
-- Session A
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT first_name FROM people WHERE id = 1; -- 'CHANGED'
-- Session B: UPDATE people SET first_name='AGAIN' WHERE id=1;
SELECT first_name FROM people WHERE id = 1; -- 'AGAIN' — fresh snapshot each statement
COMMIT;
Under REPEATABLE READ, A's second SELECT ignores B's committed
update — the snapshot was frozen at A's first read. Only after A commits does a new snapshot
reveal it. Under READ COMMITTED, A's second SELECT sees B's change
immediately — a non-repeatable read, by design. That single behavioral difference is
the two levels. Reset with SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;.
MySQL 8.0 Reference Manual — Transaction Isolation Levels, then Consistent Nonlocking Reads for the MVCC snapshot mechanism in the authors' own words.