It's the last working day before Eid. A bank branch is packed, the mobile app is busy, and the ATM queue reaches the road. Hundreds of transactions hit the same database every second, some of them on the very same accounts. In the previous chapter we learned what must never go wrong: no lost update, no dirty read, every schedule serializable, every committed transfer safe. We learned what the database promises. This chapter is about how it keeps those promises.
Here's a picture to carry through the chapter. A branch keeps its important registers in one cupboard. Any number of officers may read a register together, but an officer who wants to write in one takes it to her own desk, and nobody else touches it until she brings it back. Sometimes two officers each hold one register and wait for the other's, and the manager has to step in. And every entry is first jotted in a day journal, so that if the power fails mid-entry, the registers can be put right the next morning. Those three ideas, the lock, the deadlock and the log, are most of this chapter.
By the end you'll be able to grant or refuse a lock from the compatibility matrix and trace a schedule under two-phase locking and its strict, rigorous and conservative forms. You'll find a deadlock in a wait-for graph and resolve it by detection or by Wait-Die and Wound-Wait. You'll run timestamp ordering with the Thomas write rule, and explain multiversion and optimistic control and intention locks. You'll recover a database from its log after a crash, with and without checkpoints, explain WAL, shadow paging and the outline of ARIES, and walk a distributed transaction through two-phase commit, including what happens when a site fails.
We'll go in eight parts: locks, two-phase locking, deadlock, the lock-free methods (timestamps, versions and optimism), lock granularity, failures and the log, checkpoints and the recovery algorithms, and finally the commit protocol for a transaction that spans several databases.