Amazon announced that its DynamoDB NoSQL database service now supports Transactions, offering full atomicity, consistency, isolation, and durability (ACID) guarantees. With this capability, developers can build transaction guarantees for multi-item updates, making it easier to avoid conflicts and errors when developing highly scalable business-critical applications.
Amazon’s DynamoDB is a fully-managed key-value and document database available in AWS, providing developers with a NoSQL database service for "any scale". This database service is used by AWS customers for various use cases, ranging from building microservices and mobile backends to implementing gaming and Internet of Things (IoT) solutions. Now Amazon aims to makes life simpler for developers building logic that requires multiple, all-or-nothing operations across one or more tables with the native support for transactions.
With Transactions Amazon introduces two new DynamoDB operations for handling transactions:
- TransactWriteItems: a batch operation that contains a write set, with one or more PutItem, UpdateItem, and DeleteItem operations.
- TransactGetItems: a batch operation that contains a read set, with one or more GetItem operations.
Each transaction can include up to 10 unique items or up to 4 MB of data, including conditions. On hacker news in a thread discussing Amazon DynamoDB Transactions, one respondent stated:
Max 10 items per transaction, that's quite a restriction! I guess you have to plan all the transactions you would perform and make sure they meet the bounds.
DynamoDB Transactions offer multiple read and write options:
- Three options for reads—eventual consistency, strong consistency, and transactional.
- Two for writes—standard and transactional.
In the blog post on Transaction Danilo Poccia, evangelist at Amazon Web Services, provides an example - implementing a single atomic transaction when purchasing an item:
- First, check that the item is available and the player has the necessary coins.
- If those conditions are satisfied, the item is marked as not available and owned by the player.
- The purchased item is then added to the player inventory list.
data = await dynamoDb.transactWriteItems({
TransactItems: [
{
Update: {
TableName: 'items',
Key: { id: { S: itemId } },
ConditionExpression: 'available = :true',
UpdateExpression: 'set available = :false, ' +
'ownedBy = :player',
ExpressionAttributeValues: {
':true': { BOOL: true },
':false': { BOOL: false },
':player': { S: playerId }
}
}
},
{
Update: {
TableName: 'players',
Key: { id: { S: playerId } },
ConditionExpression: 'coins >= :price',
UpdateExpression: 'set coins = coins - :price, ' +
'inventory = list_append(inventory, :items)',
ExpressionAttributeValues: {
':items': { L: [{ S: itemId }] },
':price': { N: itemPrice.toString() }
}
}
}
]
}).promise();
With the support for transactions, Poccia also states in the blog post that transactions will bring the scale, performance, and enterprise benefits of DynamoDB to a broader set of workloads. Many use cases are easier and faster to implement using transactions, for example:
- Processing financial transactions
- Fulfilling and managing orders
- Building multiplayer game engines
- Coordinating actions across distributed components and services
Dennis Vriend, cloud consultant at binx.io, concluded in his blog post:
By having ACID guarantees, DynamoDB can be used to keep two or more region-tables consistent and opens the way for new cloud scale data architectures using DynamoDB.
Lastly, the DynamoDB Transactions are available globally in all commercial regions. Furthermore, there will be no additional cost to enable transactions for DynamoDB tables - customers will only pay for the reads or writes that are part of their transaction.