Câu hỏi phỏng vấn Databases
Câu hỏi

What is Optimistic Locking?

Câu trả lời

Optimistic locking is a concurrency control method used in database management systems to handle simultaneous transactions without locking the data during read operations. This approach assumes that conflicts between transactions are rare and allows multiple transactions to proceed without interference until the point of committing changes. Here is a detailed explanation of how optimistic locking works and its key characteristics:

How Optimistic Locking Works

  1. Version Tracking: Each record in the database contains a version field, which could be a timestamp or an incrementing number. This version represents the state of the record at the time it was read.
  2. Read Operation: When a transaction reads a record, it also reads the version number of that record.
  3. Update Attempt: Before updating the record, the transaction checks the current version of the record against the version it initially read.
  4. Version Check:
    • If the versions match: This indicates that no other transaction has modified the record since it was read. The update proceeds, and the version field is incremented.
    • If the versions do not match: This indicates that another transaction has modified the record. The update is aborted, and an optimistic locking exception is thrown, signaling a failed update due to concurrent modifications.

Advantages of Optimistic Locking

  • Reduced Locking Overhead: Since data is not locked during read operations, optimistic locking reduces the overhead associated with managing locks.
  • Improved Concurrency: It allows more transactions to proceed concurrently, which can enhance performance, especially in environments with a high number of read operations and fewer write operations.
  • Avoidance of Deadlocks: By not holding locks during the transaction, optimistic locking avoids the risk of deadlocks, which can occur in pessimistic locking scenarios.

Disadvantages of Optimistic Locking

  • Conflict Handling: Requires additional logic to handle conflicts when they occur. Applications must be designed to gracefully handle optimistic locking exceptions and resolve them, often by retrying the transaction.
  • Increased Complexity: The need to manage version checks and handle potential conflicts can increase the complexity of the application code.
  • Potential for Higher Latency: In cases where conflicts are frequent, the need to retry transactions can lead to increased latency and reduced performance.

Use Cases

Optimistic locking is particularly suitable for applications where:

  • Read operations are more frequent than write operations: This reduces the likelihood of conflicts and makes the benefits of reduced locking overhead more significant.
  • Low contention environments: Where the probability of multiple transactions attempting to modify the same data simultaneously is low.
  • Client-server applications: Especially those employing connection pooling, where maintaining a stateful connection for each user is impractical.

Example Scenario

Consider a scenario where two users are trying to update the same record in a database:

  • User A reads the record and notes the version number.
  • User B reads the same record and notes the same version number.
  • User A attempts to update the record. The application checks the version number, finds it unchanged, and proceeds with the update, incrementing the version number.
  • User B then attempts to update the record. The application checks the version number, finds it has changed, and throws an optimistic locking exception, indicating a conflict.

In summary, optimistic locking is a strategy that enhances performance and concurrency by avoiding locks during read operations and handling conflicts at the time of committing changes. It is best suited for scenarios with low data contention and a high volume of read operations[1][2][3][4][5][6][7][8].

Citations:
[1] https://systemdesignschool.io/blog/optimistic-locking
[2] https://www.linkedin.com/advice/1/how-do-you-handle-conflicts-errors-when-using-pessimistic
[3] https://en.wikipedia.org/wiki/Optimistic_concurrency_control
[4] https://dev.to/jszutkowski/pessimistic-vs-optimistic-locking-in-mysql-3hgc
[5] https://docs.oracle.com/cd/B14099_19/web.1012/b15901/dataaccs008.htm
[6] https://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking
[7] https://viblo.asia/p/009-optimistic-lock-va-pessimistic-lock-L4x5xr7aZBM
[8] https://www.ibm.com/docs/en/db2/11.5?topic=overview-optimistic-locking

middle

middle

Gợi ý câu hỏi phỏng vấn

expert

What is Index Cardinality and why does it matter?

expert

What is Optimistic Locking and Pessimistic Locking?

junior

What is Normalisation?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào