pessimistic
optimistic
locking

Pessimistic Locking vs. Optimistic Locking

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Pessimistic Locking vs. Optimistic Locking

When dealing with concurrency in databases, two common strategies to manage data consistency are pessimistic locking and optimistic locking. Both approaches aim to handle scenarios where multiple transactions or operations might attempt to read or modify the same data concurrently, but they do so in different ways.

Pessimistic Locking

Pessimistic locking assumes that conflicts will occur when multiple transactions attempt to access the same data. To prevent these conflicts, it locks the data at the moment it is accessed, ensuring that no other transaction can modify it until the lock is released.

  • How It Works:
    • When a transaction reads or writes data, it immediately places a lock on that data.
    • Other transactions that attempt to access the locked data will either be blocked until the lock is released or will fail.
    • Once the transaction completes, the lock is released, allowing other transactions to access the data.
  • Pros:
    • Ensures strong consistency by preventing other transactions from accessing the locked data.
    • Avoids conflicts and rollbacks since no two transactions can modify the data simultaneously.
  • Cons:
    • Can lead to reduced concurrency, as transactions have to wait for locks to be released.
    • Potential for deadlocks, where two or more transactions are waiting indefinitely for each other’s locks to be released.
    • Can be inefficient if conflicts are rare, leading to unnecessary locking.
  • Use Cases:
    • Suitable for environments where data contention is high, and the cost of handling conflicts is significant.
    • Often used in legacy systems or scenarios where strict data consistency is critical.

Optimistic Locking

Optimistic locking assumes that conflicts are rare and that transactions can generally proceed without interference from others. Instead of locking the data when it is accessed, optimistic locking checks for conflicts only when a transaction is about to commit its changes.

  • How It Works:
    • When a transaction reads data, it also reads a version number or timestamp associated with that data.
    • The transaction proceeds without locking the data.
    • Before committing, the transaction checks if the data’s version number or timestamp has changed since it was read.
    • If the data has been modified by another transaction (i.e., the version number or timestamp has changed), the transaction fails or retries. Otherwise, it commits successfully.
  • Pros:
    • Allows for higher concurrency, as transactions are not blocked by locks.
    • More efficient in scenarios where conflicts are infrequent, as transactions proceed without waiting.
    • Reduces the risk of deadlocks, as no locks are held during most of the transaction’s execution.
  • Cons:
    • Requires conflict detection and resolution logic, which can complicate the implementation.
    • Transactions might need to be retried if a conflict is detected at the commit stage.
    • May lead to increased overhead in checking version numbers or timestamps.
  • Use Cases:
    • Suitable for environments with low data contention, where transactions are unlikely to interfere with each other.
    • Common in modern web applications and systems with high levels of concurrent read operations.

Summary

  • Pessimistic Locking: Locks data as soon as it is accessed to prevent conflicts. It ensures data consistency at the cost of reduced concurrency and the potential for deadlocks.
  • Optimistic Locking: Assumes minimal conflicts and only checks for them at the time of committing the transaction. It allows higher concurrency but requires mechanisms to detect and handle conflicts.

Example Scenarios

  • Pessimistic Locking:
    • Banking Systems: Where two transactions updating the same account balance should never happen concurrently.
  • Optimistic Locking:
    • E-commerce Platforms: Where product information is frequently read by users, but the chances of simultaneous updates (like changing product descriptions) are relatively low.

Each strategy has its advantages and trade-offs, and the choice between pessimistic and optimistic locking often depends on the specific requirements of the application and the expected frequency of data conflicts.


Course illustration
Course illustration

All Rights Reserved.