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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Microservices With .NET Core: Building Scalable and Resilient Applications
  • The Role of Data Brokers in Software Development: Navigating Ethics and Privacy Concerns
  • Enhancing Performance: Optimizing Complex MySQL Queries for Large Datasets
  • Stifling Creativity in the Name of Data

Trending

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  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 user avatar
Roopa Kushtagi
·
Dec. 14, 23 · Review
Likes (2)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Microservices With .NET Core: Building Scalable and Resilient Applications
  • The Role of Data Brokers in Software Development: Navigating Ethics and Privacy Concerns
  • Enhancing Performance: Optimizing Complex MySQL Queries for Large Datasets
  • Stifling Creativity in the Name of Data

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!