--- created: 2022-03-09 aliases: tags: --- Question: **is using `TransactWriteItems` a better *default* approach for writing a bunch of items to [[DynamoDB]] than using `BatchWriteItem`, even if you don't need the atomicity that the former provides?** I'm assuming here that your use case is ok with allowing dirty reads if only some of the items have been written and write isolation isn't a concern either. The major benefit I see with `TransactWriteItems` (and why I'm considering this question) is that you don't need to handle partial errors with `TransactWriteItems` in your code, i.e. when some writes succeed and others fail. Your code is then responsible for retrying these failures. This adds significant overhead to your code, e.g. you may need to push the partial failed items to a queue to have them processed async. However, we also need to consider that `TransactWriteItems` has 2x cost per write and can throw conflict errors if an item is acted upon while an ongoing transaction for that item is in progress (instigated by a different client). ## In favour of `TransactWriteItems`: - It allows specifying of a top-level `ConditionCheck` and item-level `ConditionExpression`s before doing any of the writes - `BatchWriteItem` doesn't allow Update operations, only Puts and Deletes - `TransactWriteItems` allows for idempotent requests (w/in a 10 minute window) if you need it - `BatchWriteItem` is annoyingly named without an "s" at the end! 😛 ## In favour of `BatchWriteItem`: - `TransactWriteItems` is [double the cost](https://aws.amazon.com/dynamodb/pricing/on-demand/): Transactional writes consume 2 Write Units vs the typical 1 Write Unit. If writes are infrequent, this may not matter much, but will add up at scale. - Will complete quicker - `TransactWriteItems` can block other operations on the items during the transaction, even other single-item actions - Supports a larger payload size: - `BatchWriteItem`: "A single call can transmit up to 16MB of data over the network" - `TransactWriteItems`: "The aggregate size of the items in the transaction cannot exceed 4 MB." ## Other considerations: - Both operations allow 25 items to be written in one go. - Both support writing to multiple tables in a single request --- ## References - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html - https://icircuit.net/dynamodb-batch-write-vs-transactional-write/2915