Concurrency Control: MVCC vs. Pessimistic Locking 00:00:01 One cash drawer and two withdrawals Two tellers share a drawer. A pessimistic design reserves the right to change its balance before acting. Watch who owns the row and who must wait before determining the second withdrawal. BEGIN. The row is free. B begins its own transaction. The first teller's committed balance becomes visible and its row lock is released. B writes 800 and commits. Both withdrawals count. The database protects this interval until the transaction ends. A caller still has to check available funds and handle a timeout or rejected transaction. 00:00:39 A reader can borrow yesterday's edition A reporter wants a consistent view while cashiers continue updating balances. Multi-version concurrency control preserves versions with visibility information so compatible readers can use their snapshots without waiting for every writer. The row holds 100, committed at time 100. Reader B also takes a snapshot. Both reports can now read the versions visible to them. Version 2 is committed at time 150. Anyone who starts now sees 150. When nobody needs version 1 any more, it can be cleaned up. Every retained snapshot has a storage cost. An old reader can keep old versions alive well after the cashier has finished, which is why long-lived transactions deserve monitoring. 00:01:25 Write only if the price tag still matches An optimistic design lets both clerks read the same quantity and version. Each submits the proposed change with the version it observed. The check and the write must happen atomically. Quantity 10, version 5. The version is still 5, so one row changes: 9, version 6. B asks for version 5. The row has 6, so no row changes. 8, version 7. Nothing was lost. A zero-row result instructs the losing clerk to reload the new state and reconsider. With frequent collisions, the repeated work can cost more than briefly waiting for a lock. 00:02:08 When the waiting arrows form a circle Cashier A owns account one and needs account two. Cashier B owns account two and needs account one. More patience cannot break that cycle on its own. Both rows are free. B will move money from 2 to 1. A holds it, and A is waiting for B. The database aborts one, here A. B gets row 1 and finishes. The application retries A. A database can detect the cycle and abort one participant. A consistent lock order removes this particular cycle. Keep the retry outside the transaction and avoid repeating external side effects. 00:02:50 Three workers pick different envelopes A dispatcher wants independent workers to claim available jobs. Skipping an already locked row can keep work moving. It also deliberately changes which row a worker may see at that moment. All are free. Job 1 is locked, so it is skipped. Worker 2 takes job 2. Its transaction rolls back and the lock goes. Each worker marks its job and commits. Locking, versions and optimistic conditions buy different behaviors. Choose by asking who may wait, which state the reader must see and where a failed claimant resumes its work.