What Is The Difference Between Batch Writes And Transaction Writes In DynamoDB?
A super simple and brief explanation of their differences and when to use which
Are you using loops to insert or delete multiple items in DynamoDB?
If you are, that’s probably the bad way of writing or deleting items from your table.
Even if you’re calling the DynamoDB API once for a put method and again right after for a subsequent put, you’re using it inefficiently.
There is a more efficient way of doing this than using loops or calling the API multiple times.
The most efficient manner is by using Batch Writes.
Batch writes allow you to write and/or delete multiple items to and from a DynamoDB table with a single API call.
However, batch writes are frequently confused with Transaction writes.
What are the differences and when should you use which?
Let’s take a look at their purposes and see some use cases that call for the best fit.
Batch Writes
Batch writes are used to insert or delete multiple items to a DynamoDB with a single API call.
You do not need to use any loops or additional logic.
Here’s what the API call looks like in Node JS:
const params = {
RequestItems: {
"users": [
{
PutRequest: {
Item: {
PK: { S: "user-101" },
SK: { S: "user1@gmail.com" },
address: { S: "31 Sunset St." }
}
}
},
{
PutRequest: {
Item: {
PK: { S: "user-102" },
SK: { S: "user2@gmail.com" },
address: { S: "36 Sunset St." }
}
}
},
{
DeleteRequest: {
Key: {
PK: { S: "user-103" },
SK: { S: "user3@gmail.com" }
}
}
}
]
}
};
const command = new BatchWriteItemCommand(params);
const result = await client.send(command);
As you can see, put and delete operations are supported in a single batch write.
But here is the distinction with Transactions:
Batch writes are NOT atomic.
What this means is that if one of the puts or deletes fails — perhaps because of capacity limits — the rest of the operations can still succeed independently.
To manage the consistency of data in this case, you need to have proper logging in place and retry operations that failed.
Transaction Writes
Transaction writes on the other hand complement the very problem that can occur with batch writes as I explained above.
Sometimes, we need our data to be written atomically.
All individual put or delete operations should happen together or not at all.
In this case, if one operation fails, all operations that took place before the failed one will be rolled back, and all operations that took place after the failed operation will not be executed.
In a transaction write, all operations either succeed or fail as a single unit.
This guarantees the consistency of written data to your database table.
Here’s a typical example of a Transaction write:
const params = {
TransactItems: [
{
Put: {
TableName: "users",
Item: {
PK: { S: "user-101" },
SK: { S: "user1@gmail.com" },
name: { S: "John Doe" }
}
}
},
{
Update: {
TableName: "products",
Key: {
PK: { S: "prod-101" },
SK: { S: "prod-101" }
},
UpdateExpression: "SET name = :nameValue",
ExpressionAttributeValues: {
":nameValue": { S: "Airpods Pro 3" }
}
}
},
]
}
const command = new TransactWriteItemsCommand(params);
const result = await client.send(command);
In essence, a transaction is simply multiple write commands grouped together as one atomic command, each with its individual expressions.
Limitations Of Batch And Transactions
Some limitations of batch writes are:
A batch write can write a maximum of 25 items OR 16MBs of data — whichever comes first.
Batch writes only allow you to perform a put or a delete operation, not an update item operation.
A batch write can only be run on a single table.
Some limitations of transaction writes are:
a single transaction can write a maximum of 25 operations OR 4MBs of data — whichever comes first.
A transaction can span multiple DynamoDB tables (although within one region only)
Use Cases For Batch Writes
Some typical use cases for batches can be:
Allow a user to upload multiple images to a photo album.
Allow an admin to mass delete old data such as expired logs.
Allow users to send out promotional offers to multiple recipients in a list.
Use Cases For Transaction Writes
Some typical example use cases for transactions are:
In an e-commerce app, an admin might want to update their inventory when a product has been purchased. Here you can create a transaction for a new order as well as update your product inventory table.
A social media app can benefit from transaction writes where adding a like to a post, should increment a like counter attribute on that post.
In a banking app, if a user transfers money from one account to another, you should process that as a single transaction.
Essentially, any group of operation that should always be executed as single unit should make use of transactions.
Conclusion
Understanding the difference between batches and transactions is important for managing multiple-item writes in DynamoDB.
While they may seem to be quite similar, they differ in their purpose. Ultimately, transactions provide atomicity while batches do not.
Additionally, understanding your use case is also crucial for picking either method for writing multiple items simultaneously.
I am creating an email course to help you master DynamoDB in ways no other course offers, if you want in, you can sign up here:
https://www.buildawaitlist.com/waitlist/dynamodb-course
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!