TransactWriteItems lets you make multiple changes to your database at once. It makes sure all changes happen together or none at all, so your data stays correct.
TransactWriteItems in DynamoDB
Introduction
Syntax
DynamoDB
TransactWriteItems {
TransactItems: [
{
Put: { TableName, Item },
Update: { TableName, Key, UpdateExpression, ExpressionAttributeValues },
Delete: { TableName, Key },
ConditionCheck: { TableName, Key, ConditionExpression }
},
...
]
}You include an array of actions inside TransactItems.
Each action can be a Put, Update, Delete, or ConditionCheck.
Examples
DynamoDB
TransactWriteItems {
TransactItems: [
{ Put: { TableName: "Users", Item: { UserId: "123", Name: "Alice" } } },
{ Update: { TableName: "Inventory", Key: { ProductId: "A1" }, UpdateExpression: "SET Stock = Stock - :dec", ExpressionAttributeValues: { ":dec": { "N": "1" } } } }
]
}DynamoDB
TransactWriteItems {
TransactItems: [
{ Delete: { TableName: "Sessions", Key: { SessionId: "abc" } } },
{ ConditionCheck: { TableName: "Users", Key: { UserId: "123" }, ConditionExpression: "attribute_exists(UserId)" } }
]
}Sample Program
This transaction adds a new order and reduces the stock of the product by 2. Both happen together or not at all.
DynamoDB
TransactWriteItems {
TransactItems: [
{
Put: {
TableName: "Orders",
Item: { OrderId: "order123", ProductId: "prod456", Quantity: { "N": "2" } }
}
},
{
Update: {
TableName: "Inventory",
Key: { ProductId: "prod456" },
UpdateExpression: "SET Stock = Stock - :qty",
ExpressionAttributeValues: { ":qty": { "N": "2" } }
}
}
]
}Important Notes
All actions in TransactWriteItems succeed or fail together.
Use ConditionCheck to prevent unwanted changes.
Maximum 100 actions per transaction.
Summary
TransactWriteItems lets you group multiple writes into one safe transaction.
It helps keep your data consistent by applying all changes together.
You can use Put, Update, Delete, and ConditionCheck actions inside it.
Practice
1. What is the main purpose of
TransactWriteItems in DynamoDB?easy
Solution
Step 1: Understand the role of TransactWriteItems
TransactWriteItems groups multiple write actions (Put, Update, Delete) into one atomic transaction.Step 2: Compare with other options
It does not perform reads, backups, or unconditional batch deletes.Final Answer:
To group multiple write operations into a single atomic transaction -> Option AQuick Check:
Atomic transaction = group writes together [OK]
Hint: Remember: TransactWriteItems groups writes atomically [OK]
Common Mistakes:
- Confusing TransactWriteItems with batch read operations
- Thinking it automatically backs up data
- Assuming it only deletes items
2. Which of the following is the correct syntax structure for a
TransactWriteItems request in DynamoDB?easy
Solution
Step 1: Identify correct TransactWriteItems syntax
The correct syntax uses the key 'TransactItems' with an array of actions like Put and Update.Step 2: Eliminate incorrect options
BatchWriteItems is a different API; Get is not a write action; WriteItems is not a valid key.Final Answer:
{ TransactItems: [{ Put: { TableName: 'MyTable', Item: {...} } }, { Update: {...} }] } -> Option CQuick Check:
TransactItems with Put/Update = correct syntax [OK]
Hint: Look for 'TransactItems' key with write actions inside [OK]
Common Mistakes:
- Using BatchWriteItems instead of TransactWriteItems
- Including Get operations inside TransactWriteItems
- Using incorrect key names like WriteItems
3. Given the following TransactWriteItems request, what will happen if the condition in the Update action fails?
{
TransactItems: [
{ Put: { TableName: 'Users', Item: { UserId: '123', Name: 'Alice' } } },
{ Update: { TableName: 'Users', Key: { UserId: '123' }, UpdateExpression: 'SET Age = :age', ConditionExpression: 'attribute_exists(UserId)', ExpressionAttributeValues: { ':age': 30 } } }
]
}medium
Solution
Step 1: Understand TransactWriteItems atomicity
All actions in TransactWriteItems succeed or all fail together as one transaction.Step 2: Analyze condition failure effect
If the Update's condition fails, the entire transaction rolls back, including the Put.Final Answer:
Both Put and Update actions are rolled back; no changes are applied -> Option DQuick Check:
Condition failure rolls back entire transaction [OK]
Hint: If any condition fails, whole TransactWriteItems rolls back [OK]
Common Mistakes:
- Assuming partial success when condition fails
- Thinking Put action always succeeds
- Ignoring atomicity of TransactWriteItems
4. You wrote this TransactWriteItems request but it fails with a syntax error:
What is the likely cause of the error?
{
TransactItems: [
{ Put: { TableName: 'Orders', Item: { OrderId: '001', Status: 'Pending' } } },
{ Delete: { TableName: 'Orders', Key: { OrderId: '002' } } }
],
ReturnConsumedCapacity: 'TOTAL'
}What is the likely cause of the error?
medium
Solution
Step 1: Check JSON syntax in the request
ReturnConsumedCapacity is a valid parameter, but the JSON is invalid due to a missing comma after the closing bracket (]) of the TransactItems array.Step 2: Validate other parts
Delete and Put actions are correctly structured; TransactItems must be an array; TableName is required inside actions.Final Answer:
Missing comma after the TransactItems array -> Option BQuick Check:
JSON requires commas between object properties [OK]
Hint: Validate JSON syntax: add commas between top-level properties [OK]
Common Mistakes:
- Omitting commas in JSON request objects
- Forgetting TableName inside each action
- Misusing array vs object for TransactItems
5. You want to atomically update a user's email and delete a related temporary token in DynamoDB using
TransactWriteItems. Which of the following request structures correctly achieves this?hard
Solution
Step 1: Identify correct atomic update and delete
{ TransactItems: [ { Update: { TableName: 'Users', Key: { UserId: 'u1' }, UpdateExpression: 'SET Email = :e', ExpressionAttributeValues: { ':e': 'new@example.com' } } }, { Delete: { TableName: 'Tokens', Key: { TokenId: 't1' } } } ] } uses TransactWriteItems with Update and Delete actions correctly structured.Step 2: Eliminate incorrect options
{ BatchWriteItems: [ { UpdateRequest: { Key: { UserId: 'u1' }, UpdateExpression: 'SET Email = :e' } }, { DeleteRequest: { Key: { TokenId: 't1' } } } ] } uses BatchWriteItems which does not support Update; { TransactItems: [ { Put: { TableName: 'Users', Item: { UserId: 'u1', Email: 'new@example.com' } } }, { Delete: { TableName: 'Tokens', Key: { TokenId: 't1' } } } ] } uses Put which replaces entire item, not update; { TransactItems: [ { Update: { TableName: 'Users', Key: { UserId: 'u1' }, UpdateExpression: 'SET Email = :e' } }, { Delete: { TableName: 'Tokens', Key: { TokenId: 't1' } } } ], ReturnValuesOnConditionCheckFailure: 'ALL_OLD' } includes unsupported parameter ReturnValuesOnConditionCheckFailure.Final Answer:
{ TransactItems: [ { Update: { TableName: 'Users', Key: { UserId: 'u1' }, UpdateExpression: 'SET Email = :e', ExpressionAttributeValues: { ':e': 'new@example.com' } } }, { Delete: { TableName: 'Tokens', Key: { TokenId: 't1' } } } ] } -> Option AQuick Check:
Use Update and Delete inside TransactItems for atomic changes [OK]
Hint: Use Update and Delete inside TransactItems for atomic writes [OK]
Common Mistakes:
- Using BatchWriteItems for updates
- Using Put instead of Update to change attributes
- Adding unsupported parameters in TransactWriteItems
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
