DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Ampere PMU Profiler: A Guide to Microarchitecture Profiling
  • CMDB vs. IT Asset Management: Why Confusing Them Can Break Your IT Operations
  • How Synthetic Data Generation Accelerates the Software Development Lifecycle in the Enterprise
  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts

Trending

  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Navigating Concurrency: Optimistic vs. Pessimistic Control in Software Development

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.

By 
Roopa Kushtagi
·
Dec. 14, 23 · Review
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In 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.

Request


The order placement workflow with OCC is explained below.

OCC

  • 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. 

OCC vs. PCC

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.

HTTPS Optimistic concurrency control Software development Virtual screening Data (computing) Inventory (library)

Published at DZone with permission of Roopa Kushtagi. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ampere PMU Profiler: A Guide to Microarchitecture Profiling
  • CMDB vs. IT Asset Management: Why Confusing Them Can Break Your IT Operations
  • How Synthetic Data Generation Accelerates the Software Development Lifecycle in the Enterprise
  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook