amazon-web-servicestransactionsamazon-dynamodbaws-sdk

Mysterious TransactionConflict in TransactionCanceledException


Using transactWriteItems in the aws-sdk (js) we get a TransactionCanceledException. The reason within that Exception is given as TransactionConflict. Sometimes all actions in the transaction fail, sometimes only a few or only one. We do run multiple transactions in parallel that can operate on the same items. The documentation doesn't mention this particular error. Possible reason excerpt:

  1. A condition in one of the condition expressions is not met.
  2. A table in the TransactWriteItems request is in a different account or region.
  3. More than one action in the TransactWriteItems operation targets the same item.
  4. There is insufficient provisioned capacity for the transaction to be completed.
  5. An item size becomes too large (larger than 400 KB), or a local secondary index (LSI) becomes too large, or a similar validation error occurs because of changes made by the transaction.
  6. There is a user error, such as an invalid data format.

None of these apply and when retrying the transaction it seems to eventually work. Anyone knows about this exception? I can't find anything documented.


Solution

  • What you are experiencing is not a bug—it’s actually part of the feature, and it was mentioned in the launch announcement .

    Items are not locked during a transaction. DynamoDB transactions provide serializable isolation. If an item is modified outside of a transaction while the transaction is in progress, the transaction is canceled and an exception is thrown with details about which item or items caused the exception.

    As an aside, instead of locking, DynamoDB uses something called optimistic concurrency control (which is also (confusingly) called optimistic locking). If you’re interested in learning more about that, the Wikipedia article on Optimistic Concurrency Control is pretty good.

    Back to the matter at hand, the AWS documentation for transactions says:

    Multiple transactions updating the same items simultaneously can cause conflicts that cancel the transactions. We recommend following DynamoDB best practices for data modeling to minimize such conflicts.

    Specifically for TransactWriteItems, they say:

    Write transactions don't succeed under the following circumstances:

    When an ongoing TransactWriteItems operation conflicts with a concurrent TransactWriteItems request on one or more items in the TransactWriteItems operation. In this case, the concurrent request fails with a TransactionCancelledException

    Similarly for TransactGetItems:

    Read transactions don't succeed under the following circumstances:

    When there is an ongoing TransactGetItems operation that conflicts with a concurrent PutItem, UpdateItem, DeleteItem or TransactWriteItems request. In this case the TransactGetItems operation fails with a TransactionCancelledException