Rahul
Rahul

Reputation: 2607

all transactions fails in dynamodb when condition for one of the transaction fails

Data in products table in dynamoDB.

[
  {
    productId:123,
    quantity:50
  },
  {
    productId:4565,
    quantity:10
  }
  // more items
]

Now, a client can order one or more than one product at once. Now, suppose the client is ordering products 123 & 4565 with quantity 30 & 12 respectively.

The client can purchase product 123, but he can not purchase product 4565 because it has less quantity than the client wants.

I am using AWS docClient and dc.transactWrite() method to achieve this. But, the problem with transactWrite is that, if one of the conditions fails then all transactions will fail.

Implementation of Atomic Transactions in dynamodb

ConditionExpression for transactWrite

// QN - quantity
// :val - entered by client
ConditionExpression: '#QN >= :val'

Basically, I want to update the product which has the available quantity and give some information about the transaction which has not enough quantity.

Is there any way to achieve this, or I have to manually called documentClient.update() for every product.

Upvotes: 0

Views: 1248

Answers (1)

Charles
Charles

Reputation: 23791

The whole point of using a transaction is to ensure if one fails, nothing is changed.

That's the very definition of "transaction" in a DB.

Seems like you should just use BatchWriteItem()

The individual PutItem and DeleteItem operations specified in BatchWriteItem are atomic; however BatchWriteItem as a whole is not. If any requested operations fail because the table's provisioned throughput is exceeded or an internal processing failure occurs, the failed operations are returned in the UnprocessedItems response parameter.

Upvotes: 2

Related Questions