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

Software Engineering · Advanced · question 42 of 100

Describe the main differences between optimistic and pessimistic concurrency control techniques.?

📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

Concurrency control is a fundamental aspect of database management systems that ensures transactions accessing the database do not interfere with each other. Optimistic and pessimistic concurrency control techniques are two common approaches to manage concurrent access.

Pessimistic concurrency control uses locking mechanisms to prevent multiple transactions from accessing the same data simultaneously. When a transaction wants to access a data item, it must first acquire a lock on it. This lock prevents other transactions from accessing the data item until the lock is released. This approach is called pessimistic because it assumes conflicts between transactions are inevitable, and therefore, locks should be acquired before accessing data to prevent potential problems. Pessimistic concurrency control ensures data consistency, but it can also lead to lock contention and lower performance, especially in environments with high concurrency.

Optimistic concurrency control, on the other hand, assumes that conflicts between concurrent transactions are rare. Instead of acquiring locks before accessing data, optimistic concurrency control allows multiple transactions to access the data concurrently, detecting conflicts only when a transaction tries to commit its changes. When conflicts are detected, the system aborts one of the transactions and restarts it. Optimistic concurrency control avoids lock contention, leading to better performance. However, it requires more complex mechanisms to detect conflicts and handle aborts, which can also lead to higher overhead.

To illustrate the differences between the two techniques, consider the following example, where two transactions want to update the balance of a bank account concurrently:

Transaction 1:
read account balance A;
A = A - 100;
write account balance A;

Transaction 2:
read account balance A;
A = A - 50;
write account balance A;

In a pessimistic concurrency control system, before a transaction can access the account balance, it must acquire an exclusive lock on it. Therefore, Transaction 1 will acquire a lock on the account balance before it can read it, preventing Transaction 2 from reading the same data item. Once Transaction 1 finishes updating the balance and releases the lock, Transaction 2 can acquire a lock and update the balance as well. This approach ensures consistency, but it can lead to lock contention and lower performance.

In an optimistic concurrency control system, both transactions can access the balance concurrently, modifying their local copies without locks. When both transactions try to commit their changes, the system checks for conflicts. In this case, it detects that both transactions modified the same data item, leading to a conflict. The system then aborts one of the transactions (e.g., Transaction 2) and restarts it with the updated data. This approach allows for higher concurrency and performance but requires more complex mechanisms to detect conflicts and handle aborts.

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

All 100 Software Engineering questions · All topics