A developer is designing a peer-to-peer mobile payment application. When a user initiates a funds transfer to another user, the application must perform three operations: deduct the transfer amount from the sender's account balance, add the transfer amount to the recipient's account balance, and record the transaction history in a log table. These operations must execute atomically so that either all of them succeed or all of them fail. Under high traffic, the system must maintain strict consistency and prevent throttling issues. Which implementation strategy should the developer use to meet these requirements?
- AInitialize the DynamoDB client by passing hardcoded IAM access keys with administrative access directly in the application code, and use the BatchWriteItem API to execute the updates and transaction logging in a single batch request.
- BPerform a Scan operation on the Accounts table to retrieve both the sender and recipient records, verify the sender's balance within the application logic, and then apply the updates using individual UpdateItem calls.
- Use the TransactWriteItems API to perform conditional Update operations on the sender and recipient records in the Accounts table, checking that the sender's balance is sufficient, while simultaneously performing a Put operation in the Transactions table.Answer
- DUse a partition key of AccountStatus to query active accounts, and increase the table's provisioned write capacity units (WCUs) to resolve ProvisionedThroughputExceededException errors caused by highly concurrent balance updates.
Answer
Use the TransactWriteItems API to perform conditional Update operations on the sender and recipient records in the Accounts table, checking that the sender's balance is sufficient, while simultaneously performing a Put operation in the Transactions table.
Using the TransactWriteItems API provides atomicity and consistency guarantees, ensuring that all operations (the balance updates and the transaction log) either succeed together or fail together. The conditional check verifies that the sender has sufficient funds prior to completing the write.
Step-by-Step Solution
Key Concept
Using DynamoDB Transactions (TransactWriteItems) for atomic, multi-table write operations with conditional checks.