WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Data Structures & Algorithms · Advanced · question 59 of 100

Describe the difference between the Concurrency Control algorithms: Two-Phase Locking (2PL) and Timestamp Ordering (TO).?

📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

Concurrency control algorithms are used to ensure the consistency of a database system while multiple transactions are executed concurrently. Two common concurrency control algorithms are Two-Phase Locking (2PL) and Timestamp Ordering (TO).

The Two-Phase Locking (2PL) algorithm is a locking-based concurrency control algorithm that operates in two phases: the growing phase and the shrinking phase. During the growing phase, a transaction acquires locks on all the data items it needs for its execution. Once a lock has been acquired, it cannot be released until the shrinking phase, which begins after the transaction has executed all its operations. During the shrinking phase, the transaction releases all its locks, allowing other transactions to access the data items. The 2PL algorithm ensures that a transaction’s updates are serialized and that the database remains in a consistent state.

The Timestamp Ordering (TO) algorithm is a timestamp-based concurrency control algorithm that assigns a unique timestamp to each transaction that enters the system. The timestamp represents the order in which the transaction entered the system. When a transaction requests access to a data item, the system checks its timestamp against the timestamps of other transactions that have accessed the data item. If the requesting transaction has a higher timestamp than the other transactions, it is allowed to access the data item. Otherwise, the transaction is forced to wait until all transactions with lower timestamps have completed their operations. The TO algorithm ensures that transactions are executed in a serializable order, but it may result in more aborts and rollbacks compared to the 2PL algorithm.

In summary, the 2PL algorithm uses locks to ensure that transactions are executed serially and that the database remains in a consistent state, while the TO algorithm uses timestamps to enforce a serializable order of transactions. The choice of algorithm depends on the specific requirements of the system and the expected workload.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Data Structures & Algorithms interview — then scores it.
📞 Practice Data Structures & Algorithms — free 15 min
📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

All 100 Data Structures & Algorithms questions · All topics