DynamoDB Transactions Exam Tips
DynamoDB Transactions are to support mission-critical applications which need an all or nothing approach to database transactions.
- ACID Transactions: Describes ideal properties of a database transaction
- Atomic - Guarantees that a transaction is treated as a single unit. Basically, all or nothing; can't be partially completed.
- Consistent - Valid transaction; must leave DB in a valid state. Prevents any database corruption or data integrity issues.
- Isolated - No dependency between different transactions. They can be completed in parallel or sequentially; the effect of the transaction is going to be the same.
- Durable - Once a transaction is committed, it stays commited regardless of power failure or the like. Basically, it's fully written to disk and not in-memory.
- Essentially, transactions should be treated as a single operation that's durable i.e. available regardless of a catistrophic event such as a power failure.
- E.g. financial transaction
- Read or write multiple items across multiple tables as an all or nothing operation.
- Check for a pre-requisite condition before writing to a table
For example, imagine you are coding a game and you want to ensure that when a player purchases an item from another player you might perform the following steps:
- Check that the current owner actually owns the item.
- Check that the buyer has enough currency.
- Add the currency to the seller's account.
- Remove the currency from the buy's account.
- Add the item to the buyer's account.
- Remove the item from the seller's account.
As you would imagine, we would need this as a single atomic and durable transaction or we could have bugs, like the currency transferring but not the item.
You can use DynamoDB transactions to achieve this. Think T-SQL Transactions with ROLLBACK and COMMIT