Navigating Concurrency: Optimistic vs. Pessimistic Control in Software Development
Discover how Concurrency Control prevents underselling and overselling during peak traffic on e-commerce sites like Amazon and Flipkart.
Join the DZone community and get the full member experience.
Join For FreeIn software development, concurrency refers to the ability of a system to execute multiple tasks simultaneously, enhancing efficiency and responsiveness.
There are two main mechanisms for concurrency control.
1. Optimistic Concurrency Control (OCC)
OCC is a concurrency control mechanism that allows concurrent execution of transactions without acquiring locks upfront. It assumes that conflicts between transactions are infrequent and transactions proceed optimistically. During the commit phase, conflicts are detected, and if conflicts occur, appropriate actions such as aborting and retrying the transaction are taken.
In a distributed system, OCC can be implemented by maintaining version information for each data item. Each transaction reads a consistent snapshot of the database at the beginning, and during the commit phase, it checks if any other transaction has modified the same data items it has read. If conflicts are detected, the transaction is rolled back and retried with a new snapshot.
2. Pessimistic Concurrency Control (PCC)
PCC is a concurrency control mechanism that assumes conflicts are likely to occur and takes a pessimistic approach by acquiring locks on resources upfront to prevent conflicts. It ensures that transactions acquire exclusive access to resources, preventing other transactions from modifying or accessing them until the locks are released.
In a distributed system, PCC can be implemented by using distributed locks or lock managers. When a transaction wants to access a resource, it requests a lock on that resource from the lock manager. If the lock is available, it is granted, and the transaction proceeds. If the lock is unavailable, the transaction waits until the lock is released.
In an e-commerce website like Amazon or Flipkart, a common approach to handle inventory availability during order placement without blocking other users is through Optimistic Concurrency Control (OCC).
As there are multiple concurrent HTTP requests, it’s important to ensure a seamless experience for the users.
The order placement workflow with OCC is explained below.
- Check Inventory Availability: The system checks if the desired items are in stock.
- Allocate Inventory: If available, the system marks the items as allocated for the specific order without immediately decrementing the stock quantity.
- Set Expiration Time: A temporary expiration time is set for the reservation. If the order isn't completed within this time, the allocated inventory is released back to the available stock.
- Pay and Confirm the Order: Once allocated, the order processing continues, including payment and confirmation.
- Update Inventory Quantity: After payment, the reserved inventory is decremented from the available stock, ensuring accurate tracking.
- Release Expired Reservations: If an order isn't completed within the specified time frame, the allocated inventory is automatically released for others to purchase.
Here's the beauty of optimistic concurrency control – there's no explicit lock during the order placement. Instead of blocking others, the approach relies on allocating inventory and setting an expiration time.
The inventory isn't exclusively locked until paid for and confirmed. Others can still view and attempt to purchase the items, but the reserved inventory prevents overselling by marking them as reserved.
By setting an expiration time, if an order isn't completed within that timeframe, the allocated inventory is released, making it available for others to purchase.
Optimistic Concurrency Control strikes a balance, ensuring inventory availability for a specific order while allowing concurrent access without explicit locks.
Concurrency Control Techniques: There are various OCC and PCC techniques available.
For a video version of this article:
The choice between OCC and PCC depends on factors such as the workload characteristics, contention level, and desired level of concurrency and performance. OCC is often favored when conflicts are expected to be infrequent, allowing for greater concurrency, while PCC is preferred when conflicts are anticipated to be frequent, at the cost of potentially more locking and blocking.
For instance, an e-commerce solution may opt for OCC under normal conditions and choose to use PCC when there is a burst in demand for an item on sale, i.e., a hot sku, or use PCC only when inventory for an item reaches a certain low threshold.
Published at DZone with permission of Roopa Kushtagi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments