8. Databases2PL vs MVCC
2PL vs MVCC
Two-phase locking and MVCC are the two main ways databases stop concurrent transactions from tripping over each other. With two-phase locking, a transaction takes locks on the data it reads and writes and holds them until it finishes, so anyone wanting conflicting access has to wait, which is safe but can mean a lot of queuing. MVCC, multi-version concurrency control, keeps several versions of each row instead: a writer creates a new version, and readers keep seeing the version that was current when their transaction began, so reads and writes don’t block each other. The cost is that old versions pile up and need cleaning up, which is what PostgreSQL’s vacuum does.
In this lesson: A version-chain visual next to a lock-wait visual.