Difference between 2PC (2 phase commit) and 2 PL (2 phase locking)

3.1k views Asked by At

What is the difference between the two? The protocol on the surface looks different, but I would like to understand what is really different between the two and why they are not equivalent.

1

There are 1 answers

0
Shashank V On

2 Phase locking is a mechanism implemented within a single database instance to achieve serializeable isolation level. Serializeable transaction level is the strongest isolation where even with parallely executing transactions, the end result is same as if the transactions where executed serially. It works as follows:

Whenever the transaction wants to update an object/row, it must acquire a write/exclusive lock. When transactions wants to read an object/row, it must acquire a read/shared lock. Instead of releasing the lock immediately after each query, the locks must be held till the end of the transaction(commit or abort). So while the transaction is being executed, the number of locks held by the transaction expand/grow. (Read/write lock behavior is similar to any other reader/writer locking mechanisms, so not discussing here)

At the end of the transaction, the locks are released and number of locks held by the transactions shrinks.

Since the locks are acquired in one phase and released in another phase i.e., there are no lock releases in acquire phase and no new lock acquire in release phase, this is called 2 phase locking.


2 phase commit is an algorithm for implementing distributed transaction across multiple database instances to ensure all nodes either commit or abort the transaction.

It works by having coordinator(could be a separate service or library within the application initiating the transaction) issue two requests - PREPARE to all nodes in phase 1 and COMMIT(if all nodes returned OK in PREPARE phase) or ABORT(if any node returned NOT OK in PREPARE PHASE) to all nodes in phase 2.

TLDR:

2 phase locking - for serializable isolation within a single database instance

2 phase commit - atomic commit across multiple nodes of a distributed database/datastores