DEV Community

Hiren Dhaduk
Hiren Dhaduk

Posted on

DynamoDB ACID Transactions- What, Why, & How?

Amazon DynamoDB is a fast, flexible, fully managed NoSQL database service designed for single-digit millisecond performance at any scale. And many leading companies, including Disney, Salesforce, Snap Inc, Dropbox, Capital One, and more, use DynamoDB for various use cases.

But if you have worked with DynamoDB, you may have experienced challenges when implementing business logic that requires multiple, all-or-nothing operations within and across one or more tables. To eliminate such complexities and expand use cases, AWS introduced native support for DynamoDB transactions. And yes, DynamoDB also supports ACID transactions! This article discusses what ACID transactions are and why and how you should use them in DynamoDB.

What are ACID transactions?

The term ACID transactions has been around for decades. It describes the ideal properties of database transactions such as below:

  • Atomic: A quality that ensures each transaction is fully committed or not at all. It treats each transaction as a single unit of change to the database and cannot be partially completed. The transaction is either committed or rolled back.
  • Consistent: A quality that ensures the database is in a consistent and valid state after a transaction is committed. It prevents the database from being corrupted or causing data integrity issues.
  • Isolated: It guarantees that transactions are not dependent on each other. A transaction is executed in such a way that it does not affect other transactions.
  • Durable: It guarantees that a transaction is committed even if the system fails and there is no breakdown in the event of failures (such as power loss).

DynamoDB is designed to provide ACID transactions along with scalability. The transactions simplify code by executing multiple, all-or-nothing actions within and across tables with a single API call. They also provide atomicity, consistency, isolation, and durability (ACID) in DynamoDB, enabling developers to maintain data correctness in applications more quickly.

DynamoDB provides ACID transactions across one or more tables within a single AWS account and region. And they are available globally in all standard AWS Regions. As for pricing, there is no additional cost to enable transactions for DynamoDB tables. You only pay for reads and writes that are part of your transaction. However, DynamoDB performs two underlying reads or writes of every item in the transaction - one to prepare the transaction and one to commit the transaction. The read/write operations are also visible in Amazon CloudWatch metrics.

Why use DynamoDB ACID transactions?

DynamoDB’s native, server-side support for transactions simplifies the developer experience. They can extend the scale, performance, and enterprise benefits of DynamoDB to a broader range of mission-critical workloads. Many use cases become easier and faster to implement with transactions, such as:

  • Executing financial transactions
  • Managing and fulfilling orders
  • Building multiplayer game engines
  • Coordinating actions across distributed services and components

You can support more sophisticated workloads and business logic that require updating, adding, and deleting multiple items as a single, all-or-nothing operation. For example, with DynamoDB transactions, you can group multiple actions together and submit them as a single atomic transaction.

For more on how DynamoDB transactions work and best practices for using them, you can refer to this official developer guide. And to achieve maximum performance and cost-efficiency in DynamoDB, check out this detailed blog on Amazon DynamoDB best practices.

DynamoDB transactions in action

Let’s take an example of an online financial transaction to understand DynamoDB transactions in action. You want to order a pizza, so you order it online and pay via the food delivery app. But you do not receive the notification of a successful payment seconds later. Now what?

In this case, there is no need to write or update anything in the database unless all parts of the whole operation are complete. Here, DynamoDB ACID transactions come handy. In the above operation, there are multiple steps that happen almost simultaneously and require all-or-nothing execution as below–

Receive request to make a payment → Verify there are sufficient funds in the originating account → Debit the originating account → Credit the receiving account

Thus, ACID transactions are essential for implementing critical use cases successfully in the cloud. To get started with and enable Amazon DynamoDB transactions, download the latest AWS SDK or the AWS CLI (Command Line Interface). For a better understanding, you can follow these DynamoDB transactions examples.

Oldest comments (0)