I'm working on a project where I need to update multiple items in a DynamoDB table. I'm considering different approaches and would like some advice on the tradeoffs between using batch operations like BatchGetItem and then BatchWriteItem versus using individual UpdateItem operations on each items.
I have a scenario where I need to update specific attributes of multiple items in a DynamoDB table. I'm aware of BatchGetItem and BatchWriteItem for retrieving and writing multiple items in a batch.
Should I consider using BatchGetItem followed by BatchWriteItem or perform individual UpdateItem operations on each item for granular attribute updates? What are the tradeoffs in terms of performance, cost, and complexity between these approaches?
Any insights, best practices, or experiences with these methods in DynamoDB would be greatly appreciated.
In DynamoDB there is no option to BatchUpdate, you can only BatchPut/BatchDelete.
What that means is that each operation is an overwrite. So if you have other processes modifying the data at the same time you're making BatchWrites then there's a very good chance your items will end up in an inconsistent state.
In terms of monetary cost, both UpdateItem and BatchWriteItem cost the same.
Performance improvements can be gained from BatchingItems, which allows you to send 25 items in a single request, this will be less resource intensive than firing 25 individual UpdateItems in parallel.
However, if your host machine is capable then with multi threading there is no real noticeable difference in performance, DynamoDB ends up doing parallel operations no matter which method you choose.
While BatchWriteItems does not provide the ability to use Update, you can leverage PartiQL API BatchExecuteStatement which allows you to define up to 25 update operations, in which you write it in SQL like syntax. This is useful should you be limited by your host machine.