Keep the Old Row and Nobody Has to Wait
Last timeDoing It With Locks
Writers make a new version rather than overwriting, so every reader sees the database as it was when it started and never waits for anybody. One gap survives.
The mechanism in the previous lesson makes transactions wait, and the waiting
is where both the latency and the deadlocks come from. This lesson is the other
mechanism, which asks a different question: what if a reader never needed a
lock at all?
Several versions of one row
The idea is one change to what a write does. Instead of overwriting the row, it
writes a new copy of it and leaves the old one where it is, stamped with which
transaction made it and when.
| step | version | balance | written by | committed at | who can see it | what happened |
|---|---|---|---|---|---|---|
| 1 | 1 | 500 | transaction 40 | 100 | anybody who started after 100 | The original value, and still the right answer for any transaction that began before the next commit landed. |
| 2 | 2 | 460 | transaction 51 | 140 | anybody who started after 140 | A withdrawal. Version 1 is not deleted, only marked as ending at 140, because somebody may still be reading it. |
| 3 | 3 | 760 | transaction 55 | 180 | anybody who started after 180 | A deposit. Three versions of one row now exist on disk and all three are correct answers to somebody. |
| 4 | 4 | 700 | transaction 61 | 0 | nobody yet | Uncommitted. Visible only to transaction 61 itself, and if 61 aborts this version is simply never shown to anybody and is removed later. |
What a snapshot is
The reader's rule is one sentence: read the newest version of this row that was
committed before I started, and pretend nothing newer exists.
The lesson stops here
3 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents