jdsr4c
jdsr4c

Reputation: 21

Will DynamoDB API BatchWriteItem always write to the table in the order of the PutRequests in RequestItems?

Simple example for clarification: The JSON below is sent to DynamoDB using BatchWriteItem. Will it always be processed in the exact same order? I must have "Message one" written to the table first, then "Message Two" second, and "Message Three" third.

"RequestItems": {
  "TABLE": [
    {
      "PutRequest": {
        "Item": {
          "Message": {
            "S": "Message one."
          },
          "ID": {
            "S": "S103"
          }
        }
      }
    },
    {
      "PutRequest": {
        "Item": {
          "Message": {
            "S": "Message two."
          },
          "ID": {
            "S": "S104"
          }
        }
      }
    },
    {
      "PutRequest": {
        "Item": {
          "Message": {
            "S": "Message three."
          },
          "ID": {
            "S": "S105"
          }
        }
      }
    },
  ]
}

Upvotes: 1

Views: 508

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

I don't see anything in the DynamoDB docs that suggest there is an ordering guarantee when using BatchWriteItem. However, the docs do say

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. You can investigate and optionally resend the requests. Typically, you would call BatchWriteItem in a loop. Each iteration would check for unprocessed items and submit a new BatchWriteItem request with those unprocessed items until all items have been processed.

The very idea that some of the requests may end up as unprocessed suggests that order is not guaranteed.

I don't have specific knowledge about how BatchWriteItem ordering is or isn't handled, but wanted to point this out in the event your use case can't handle that uncertainty.

If order is important, you could just fire off multiple requests in-order.

If you're able to share more about the details of your use-case, I'm sure the community could make recommendations that directly address the problem you're trying to solve.

Upvotes: 2

Related Questions