Hone logo
Hone
Problems

Concurrent Inventory Update Challenge

A common scenario in e-commerce and inventory management systems is handling multiple concurrent updates to the same item. Without proper concurrency control, race conditions can lead to incorrect inventory levels. This challenge focuses on implementing robust locking strategies to ensure data integrity during high-volume concurrent updates.

Problem Description

You are tasked with building a system that allows multiple users to simultaneously update the quantity of a specific product in an inventory database. The system needs to prevent lost updates and ensure that the final inventory count accurately reflects all successful transactions.

What needs to be achieved: Implement a mechanism that allows multiple transactions to update a product's quantity while guaranteeing that:

  1. No updates are lost due to concurrent access.
  2. The final quantity reflects the sum of all intended additions/subtractions.
  3. The system remains responsive and avoids indefinite deadlocks.

Key requirements:

  • The system must support ADD and SUBTRACT operations on a product's quantity.
  • These operations must be atomic with respect to other concurrent operations on the same product. Operations on different products can proceed concurrently without blocking each other.
  • A mechanism to handle potential deadlocks or long lock durations should be considered.

Expected behavior: When multiple transactions attempt to update the same product's inventory concurrently, they should be serialized in their access to that specific product's record. Each transaction should read the current quantity, perform its calculation, and then write the new quantity back. The order of these read-modify-write cycles matters.

Important edge cases:

  • Insufficient stock for subtraction: If a subtraction operation would result in a negative quantity, it should be rejected (or handled according to business rules, for this challenge, rejection is sufficient).
  • High concurrency: The solution should perform well under a high load of concurrent requests targeting the same product.
  • Stale reads: Ensure that subsequent reads within the same transaction see a consistent state.

Examples

Example 1:

Initial State: Products table:

product_idquantity
10150

Transactions:

  • Transaction A: UPDATE Product 101: ADD 10
  • Transaction B: UPDATE Product 101: SUBTRACT 5

Possible Execution Scenario 1 (A completes before B reads):

  1. Transaction A reads quantity = 50.
  2. Transaction A calculates new quantity: 50 + 10 = 60.
  3. Transaction A writes quantity = 60.
  4. Transaction B reads quantity = 60.
  5. Transaction B calculates new quantity: 60 - 5 = 55.
  6. Transaction B writes quantity = 55.

Output: Products table:

product_idquantity
10155

Explanation: Transaction A successfully updated the quantity, and Transaction B then operated on the updated value. The final quantity is correct.

Example 2:

Initial State: Products table:

product_idquantity
10150

Transactions:

  • Transaction A: UPDATE Product 101: ADD 10
  • Transaction B: UPDATE Product 101: SUBTRACT 5

Possible Execution Scenario 2 (B completes before A writes):

  1. Transaction B reads quantity = 50.
  2. Transaction B calculates new quantity: 50 - 5 = 45.
  3. Transaction B writes quantity = 45.
  4. Transaction A reads quantity = 45.
  5. Transaction A calculates new quantity: 45 + 10 = 55.
  6. Transaction A writes quantity = 55.

Output: Products table:

product_idquantity
10155

Explanation: Even though Transaction B wrote its result first, Transaction A still read the stale value, performed its operation, and then wrote its result. The final quantity is still correct because the read-modify-write cycle for each transaction was atomic relative to the other.

Example 3: Insufficient Stock

Initial State: Products table:

product_idquantity
10110

Transactions:

  • Transaction A: UPDATE Product 101: SUBTRACT 5
  • Transaction B: UPDATE Product 101: SUBTRACT 8

Possible Execution Scenario (A reads first, B also reads):

  1. Transaction A reads quantity = 10.
  2. Transaction B reads quantity = 10.
  3. Transaction A calculates new quantity: 10 - 5 = 5.
  4. Transaction A attempts to write quantity = 5.
  5. Transaction B calculates new quantity: 10 - 8 = 2.
  6. Transaction B attempts to write quantity = 2.

Output: The system should reject Transaction B's update, leaving the quantity at 5.

Products table:

product_idquantity
1015

Explanation: Transaction A successfully subtracted 5, resulting in a quantity of 5. When Transaction B attempts to subtract 8 from the original read value of 10, it would result in a negative quantity. The system, recognizing this as an invalid operation (or an attempted modification from a stale read where stock is now insufficient), rejects Transaction B's update.

Constraints

  • The Products table contains at least two columns: product_id (unique identifier) and quantity (integer representing stock count).
  • product_id will be an integer between 1 and 1,000,000.
  • quantity will be an integer between 0 and 10,000,000 initially.
  • The total number of concurrent update operations targeting the same product_id can reach 1,000 per second.
  • Operations must complete within 500 milliseconds.
  • The database can support at least row-level and table-level locks.

Notes

Consider different locking granularities. Row-level locks on the specific product_id are ideal for this scenario as they minimize contention. However, explore how other locking strategies (like table locks or advisory locks) might be used, and discuss their trade-offs.

Think about how to implement the "reject subtraction if insufficient stock" rule in conjunction with locking. This often involves a conditional update or checking the quantity after acquiring the lock but before committing the write.

Your solution should be expressed in pseudocode, focusing on the transactional logic and locking mechanisms. You do not need to provide the full SQL DDL/DML, but rather the conceptual steps within a transaction.

Success will be measured by the ability to handle high concurrency for individual product updates without data corruption or lost updates, while correctly enforcing business rules like stock limitations.

Loading editor...
plaintext