Optimistic concurrency control clarification
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Optimistic concurrency control (OCC) is a database management technique designed to handle transactions while minimizing lock-based mechanisms, which can become a bottleneck in highly concurrent systems or systems where read operations dominate. Instead of locking data when a transaction begins, OCC assumes that multiple transactions can complete without conflicting with each other, checking for conflicts only upon transaction commit—a fundamentally optimistic approach.
Principles of Optimistic Concurrency Control
OCC works on three basic principles:
- Begin: The transaction reads and records the state of data, often in the form of a version number or a timestamp, without locking resources.
- Modify: The transaction performs necessary computations and changes without affecting the shared data.
- Validation: On commit, the transaction checks if other transactions have modified the data it used in the meantime. If the data has been changed, the transaction is rolled back and may be retried.
How Optimistic Concurrency Control Works
To understand the implementation of OCC, consider a simple example involving a database of bank accounts:
- Step 1 (Begin): Transaction A wants to transfer
$100 from Account X to Account Y. It reads the balances$x = 500$and $y = 300$`, with version 1 noted for both account records. - Step 2 (Modify): Transaction A calculates new balances locally: and .
- Step 3 (Validation): Before committing, Transaction A checks the current versions of the account balances. If either account has a version number greater than 1, it means another transaction has modified one of the accounts, and thus, Transaction A will be rolled back. If not, Transaction A commits the changes and increments the version numbers for both accounts.
Benefits and Drawbacks of OCC
Benefits:
- High Throughput: Particularly in environments where read operations vastly outnumber writes, OCC can significantly reduce the overhead caused by locking mechanisms.
- Reduced Lock Contention: OCC eliminates most of the locking and blocking issues prevalent in pessimistic concurrency control strategies.
- Scalability: Easier to scale in a predominantly read environment as it reduces the overhead and complexity associated with fine-grained locking.
Drawbacks:
- Commit Overhead: The commit phase can become a bottleneck under high conflict conditions, as the cost of validating and potentially rolling back transactions can be significant.
- Starvation: High contention on the same data might lead to repeated rollbacks, causing transaction starvation.
- Additional Resource Requirements: More memory might be required to manage multiple versions of data objects or additional metadata for validation.
Practical Applications and Further Considerations
OCC is widely used in scenarios like document stores, certain NoSQL databases, and applications where the probability of transaction collisions is low relative to the total amount of requests. For instance, a large-scale e-commerce website might employ OCC to handle user sessions where the chance of the same product's stock being updated simultaneously is relatively slim.
In practice, employing OCC requires careful consideration of the system’s operational environment and workload characteristics. The choice between OCC and other concurrency control methods like pessimistic concurrency control or using a hybrid approach depends significantly on specific application needs.
Summary Table
| Attribute | Optimistic Concurrency Control |
| Locking during operation | No locks are used during the read and modify phase. |
| Validation | Occurs at commit; may lead to rollback if data was modified externally. |
| Ideal usage | Useful in read-intensive applications with infrequent updates. |
| Overhead | Low during transaction, potentially high at commit. |
| Conflict Resolution | Transactions must restart or rollback on conflicts. |
| Scalability | High in environments with low to moderate write conflicts. |
In conclusion, optimistic concurrency control offers a robust alternative for managing data consistency in environments where locking can prove inefficient or harmful. By understanding its mechanisms and limitations, developers and database administrators can better design systems that are both resilient and performant.

