Two-phase Locking does not scale well


Last Updated on Mar 01, 2021

Two-phase locking was the only widely used algorithm for serializability in databases over the last few decades. But it hasn't been used by everybody because of its performance: transaction throughput and query response times are significantly worse under two-phase locking than under weak isolation. As the system scales under load, the performance takes a hit.

This is partly due to the processing overhead of acquiring and releasing locks whenever transactions want to read or write data. But a more significant reason for the performance hit is the reduced concurrency. By design, if two concurrent transactions try to do anything that may in any way result in a race condition, one has to wait for the other to complete.

Even if applications keep transactions short, which is the norm in most OLTP applications, a queue may form if several transactions want to access the same object. Each transaction has to wait for other transactions to complete, impacting response time.

This has an extended adverse effect when the application has long-running slow queries. A single slow transaction, or one transaction that accesses a lot of data and acquires many locks, can paralyze the rest of the system.

Also, because Deadlocks occur more frequently under 2PL Serializable Isolation, there can be significant wasted effort. When transactions are aborted and retried, the database needs to do its work all over again.


© 2022 Ambitious Systems. All Rights Reserved.