domain platform Commons: 3/5

Optimistic Locking Pattern

Also known as: Optimistic Concurrency Control

1. Overview

The Optimistic Locking pattern, also known as Optimistic Concurrency Control (OCC), is a concurrency control method used in transactional systems to manage simultaneous access to shared data. It assumes that multiple transactions can complete without affecting each other, and that therefore, conflicts are rare. Instead of locking data records when they are read, which can lead to performance bottlenecks, optimistic locking allows transactions to proceed and then checks for conflicts before committing them. If a conflict is detected, the transaction is rolled back and can be retried. This approach is contrasted with pessimistic locking, which locks resources to prevent conflicts from happening in the first place.

The historical origins of optimistic locking can be traced back to the early days of database management systems. It was proposed as an alternative to traditional locking mechanisms, which were seen as too restrictive for certain types of applications, particularly those with high read-to-write ratios and low data contention. The term was coined by H.T. Kung and John T. Robinson in their 1981 paper “On Optimistic Methods for Concurrency Control.” [1]

2. Core Principles

The core principles of the Optimistic Locking pattern are as follows:

  • No Locks During Read: Resources are not locked when they are read for modification. This allows multiple transactions to read the same data concurrently, improving system performance and scalability.
  • Versioning: A version identifier (e.g., a version number, timestamp, or hash) is associated with each data record. This version is read along with the data.
  • Conflict Detection: Before a transaction is committed, the version of the data it read is compared with the current version in the database. If the versions are the same, it means the data has not been modified by another transaction, and the commit can proceed. If the versions differ, a conflict is detected.
  • Rollback on Conflict: If a conflict is detected, the transaction is rolled back, and the changes are not saved. The application can then choose to retry the transaction, which will involve re-reading the data and its new version.

3. Key Practices

In a multi-user environment where multiple transactions can access and modify the same data concurrently, there is a risk of “lost updates.” This occurs when two transactions read the same data, and then one transaction updates it, and the second transaction, unaware of the first update, also updates it. The changes made by the first transaction are overwritten and lost. Pessimistic locking solves this by locking the data, but this can lead to poor performance and deadlocks, especially in systems with a large number of users and long-running transactions.

4. Implementation

The Optimistic Locking pattern provides a solution to the lost update problem without the overhead of pessimistic locking. It works as follows:

  1. Read: A transaction reads a data record, including its current version identifier.
  2. Modify: The transaction modifies the data in memory.
  3. Verify and Write: When the transaction is ready to commit, it issues an update statement with a WHERE clause that checks if the version of the record in the database is still the same as the version it read. If it is, the data is updated, and the version number is incremented. If the version is different, the update fails because another transaction has modified the data in the meantime.

This “read-verify-write” cycle ensures that a transaction only updates data if it has not been changed by another transaction since it was read.

5. 7 Pillars Assessment

Pillar Score (1-5) Rationale
Purpose 3 Serves a clear technical purpose in system design
Governance 3 Can be governed through standard engineering practices
Culture 3 Supports engineering culture of reliability and quality
Incentives 3 Aligns incentives toward system stability
Knowledge 4 Well-documented pattern with extensive community knowledge
Technology 4 Directly applicable to modern technology stacks
Resilience 4 Contributes to overall system resilience
Overall 3.4 A valuable technical pattern that supports commons infrastructure

Pros:

  • High Concurrency: Optimistic locking allows for a high degree of concurrency because it does not hold locks on data, enabling multiple transactions to access the same data simultaneously.
  • Improved Performance: By avoiding the overhead of acquiring and releasing locks, optimistic locking can improve the overall performance of the system, especially in read-heavy applications.
  • No Deadlocks: Since no locks are held, deadlocks are not an issue.

Cons:

  • Rollbacks: In high-contention environments where conflicts are frequent, optimistic locking can lead to a large number of rollbacks and retries, which can degrade performance.
  • Complexity: Implementing optimistic locking can be more complex than pessimistic locking, as it requires the application to handle transaction retries.

6. When to Use

  • Databases: Many modern databases, including Microsoft SQL Server, Oracle, and PostgreSQL, provide support for optimistic locking. For example, in SQL Server, you can use the rowversion data type to implement optimistic locking. [3]
  • Web Applications: Optimistic locking is commonly used in web applications to prevent lost updates when multiple users are editing the same data. For example, in a wiki, optimistic locking can be used to ensure that if two users edit the same page at the same time, one user’s changes are not overwritten by the other’s.
  • Object-Relational Mapping (ORM) Frameworks: ORM frameworks like Hibernate (Java) and Entity Framework (.NET) have built-in support for optimistic locking. [2]

7. Anti-Patterns & Gotchas

In the cognitive era, where AI and machine learning models are increasingly used to automate and enhance business processes, optimistic locking remains a relevant and valuable pattern. For example, in a system where multiple AI agents are concurrently updating a shared knowledge base, optimistic locking can be used to ensure data consistency without sacrificing performance. The pattern can also be applied to the management of machine learning models, where multiple data scientists might be working on different versions of the same model.

8. References

  • Shared Resource: The Optimistic Locking pattern is well-aligned with the principle of a shared resource, as it is designed to manage concurrent access to shared data.
  • Democratic Governance: The pattern does not directly relate to democratic governance.
  • Equitable Access: By allowing for high concurrency, optimistic locking can help to ensure that all users have equitable access to the system’s resources.
  • Sustainability: The improved performance and scalability offered by optimistic locking can contribute to the long-term sustainability of a system.
  • Community Benefit: By enabling the development of more robust and performant applications, the Optimistic Locking pattern can provide a significant benefit to the community of users.

8. References

[1] Kung, H. T., & Robinson, J. T. (1981). On Optimistic Methods for Concurrency Control. ACM Transactions on Database Systems, 6(2), 213–226. [2] Fowler, M. (2002). Patterns of Enterprise Application Architecture. Addison-Wesley. [3] Microsoft. (n.d.). Optimistic Concurrency. Azure Architecture Center. Retrieved February 10, 2026, from https://learn.microsoft.com/en-us/azure/architecture/patterns/optimistic-concurrency