Transactions in the DynamoDB node.js DocumentClient with transactWrite and transactGet

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet

Transactions allow us to guarantee that a set of operations will succeed or fail as a unit. This is different than the behavior of batchWrite and batchGet. Transactions also enable us to use more of the DynamoDB API, such as Update and ConditionCheck.

Instructor: [0:00] TransactGet is a lot like BatchGet. It is a slightly different syntax where we use transact items instead of request items. We have to specify each get separately with the table name.

[0:11] TransactItems cannot use the GSI or a secondary index, so there is no way to specify an index instead of a table name. If we run TransactGet from this script on this table with three items in it, we get back our responses key each of the items.

[0:26] Note that the main difference between TransactGet and BatchGet is that TransactGet will completely fail if anything fails, such as going over the provision to capacity for a table, while BatchGet will get you as much as it can and then give you the unprocessed keys as well.

[0:41] Note that the main difference between TransactWrite and BatchWrite is not only that the entire request will fail or succeed in total, but that we have access to more of the API like update which we didn't have access to before. In this case, we're deleting the first item, putting todo4 for user1 and updating todo2 with a new text value.

[1:00] If we refresh the DynamoDB console and take a look at what happened, we can see that the todo1 is gone because we deleted it successfully. Todo2 has updated its text value inside of the data attribute. We've also put todo4 with an additional value.

[1:15] Note that in addition to put, delete, and update, TransactWrite allows us to specify condition check objects. This allows us to specify a condition expression that is very similar to any of the other condition expressions that we've written. If the condition check fails, the entire write transaction will fail as well.