Amazon DynamoDB Distributed Transactions at Scale
05 Feb 2024 (9 months ago)
Benefits of Cloud Databases
- Cloud databases offer a fully managed experience, elasticity, and a pay-as-you-go model, making them agile and suitable for modern applications.
Features of DynamoDB
- DynamoDB provides a simple API, flexible schema, high availability, and unbounded growth.
- DynamoDB offers predictable performance, ensuring consistent single-digit millisecond latency even as the table size or request rate increases.
Importance of Transactions
- Transactions are essential for ensuring atomicity, consistency, isolation, and durability of database operations.
- Transactions facilitate the construction of correct and reliable applications that wish to maintain multi-item invariants.
- Transactions help maintain invariants when multiple instances of an application are running in parallel and accessing the same data concurrently.
- Transactions ensure that invariants hold true even if the application crashes in the middle of an operation.
Challenges of Implementing Transactions in NoSQL Databases
- NoSQL databases traditionally do not support transactions due to concerns about performance, complexity, and potential impact on non-transactional workloads.
- Implementing transactions on the client side can be complex and requires handling various failure modes to avoid inconsistent states.
- Building cleanup logic for unfinished transactions adds additional complexity for application developers.
- In a multi-user environment, additional mechanisms like locks are needed to guarantee isolation and prevent interference between operations.
- Introducing a new layer and locks can impact scalability and performance, making it harder to achieve unbounded growth and predictable performance.
DynamoDB's Approach to Transactions
- To add transactions to DynamoDB, Amazon aimed to enable atomic and serializable execution of operations across multiple partitions or tables, with predictable performance and without impacting non-transactional workloads.
- DynamoDB introduced two new operations, TransactGetItems and TransactWriteItems, which allow for single-request transactions that either succeed or fail immediately without blocking.
- TransactGetItems enables consistent snapshot reads of multiple items from arbitrary DynamoDB tables, while TransactWriteItems allows atomic creation, deletion, or update of multiple items across multiple tables.
- Preconditions can be optionally included in transactions to check specific item conditions before executing operations.
- DynamoDB's approach to transactions mimics how distributed transactions are implemented in other systems, using resets to record item values at the time of transaction execution and buffering writes until commit time.
Transaction Coordination in DynamoDB
- Transaction coordinators pick up the items involved in the transaction and send requests to the storage nodes.
- Transaction coordinators use a two-phase protocol to ensure that transactions are executed in their entirety exactly once.
- Recovery from transaction coordinator failures is handled by a recovery manager that periodically scans a ledger of transactional requests.
Serializability in DynamoDB
- Serializability is achieved using timestamp ordering, where each transaction is assigned a timestamp that defines its serial order.
- Storage nodes can independently validate that transactions are executed in the correct order and reject conflicting transactions.
- AWS provides a Time sync service to keep the clocks in coordinator fleets closely in sync, but even with perfectly synchronized clocks, transactions can arrive at the storage nodes out of order due to network delays and failures.