Module 01 · Database Internals · One Database, to the Bolt · about 20 min to read
Continue where you left off ×
Locks, Latches & Transactions
Locks, Latches & Transactions · Open board
The board that draws latches and locks side by side, the pair every “why is my database stuck” ticket confuses.
Before you start. The last lesson watched the database plan how to answer a query, the question your app sends it, before reading a single row (one record, such as one account). This lesson is about what happens once that plan runs and meets every other query touching the same rows at the same time.
The idea in 30 seconds
A lock guards your data, one record, such as an account, for as long as your whole transfer runs. A latch guards the engine’s own bookkeeping for microseconds. Different animal, different lifetime, and only one of them is ever what “the database is stuck” means.
Two transfers can each hold what the other one is waiting for, forever. That’s a deadlock, and nobody has to make a mistake for one to form.
The database doesn’t prevent deadlocks, it watches for transfers stuck waiting on each other, kills one side, and hands your app a retry. That’s the whole fix.
A transfer can read truthfully, write only its own rows, take no lock anyone else needed, and still leave a broken rule behind. That’s write skew, and the part of the database that watches for deadlocks never sees it.
How much of that a database blocks is one dial, the isolation level, and turning it all the way up used to leave too many transfers waiting on each other until a 2008 paper made it cheap.
Read the story
Lesson 14 of 26 in Database Internals
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1976: a bank that was never allowed to go down The fix that clicked: reserve the page, not the ledger The animal nobody at the counter ever sees The knot two transfers tie by themselves The freeze that isn’t a freeze at all Terms it explains: lock, latch, deadlock, Write skew, isolation level, Two-phase locking (2PL), Granularity, Escalation, Timestamp ordering, Optimistic concurrency control, microseconds, none, Strict 2PL, cycle, 1 second, victim, retry, Andy Pavlo, CMU 15-445, The Critique, discussed.
Review Database Internals
Mark as read Saved in this browser, and to your account when you sign in.