AWS
DynamoDBTransactionConflictException - DynamoDB Transaction Conflict
Getting a **DynamoDBTransactionConflictException** means your DynamoDB transaction conflicted with another concurrent transaction on the same items—DynamoDB transactions are atomic and cannot be modified while in progress. This client-side error (4xx) happens when AWS detects transaction conflicts. Most common when concurrent transactions operate on the same items, but also appears when transactions are already in progress, optimistic locking fails, or transaction timeouts occur.
#Common Causes
- →Identity: IAM policy allows DynamoDB transactions but conflict occurs. Service Control Policy (SCP) enforces transaction limits.
- →Network: VPC endpoint DynamoDB transaction restrictions. Concurrent transaction conflicts.
- →Limits: Concurrent transaction on same items. Transaction already in progress. Transaction conflict with another operation. Optimistic locking failure. Transaction timeout.
✓Solutions
- 1Step 1: Diagnose - Check if transaction conflict is transient: Retry transaction after short delay. Verify if conflict is due to concurrent operations. Check transaction timeout settings.
- 2Step 2: Diagnose - Review transaction scope: Check how many items are in transaction. Verify if transaction includes hot partitions. Review transaction complexity.
- 3Step 3: Diagnose - Check for concurrent transactions: Review application logs for concurrent transaction attempts. Check if multiple processes are accessing same items.
- 4Step 4: Fix - Implement exponential backoff: Retry with delays: 10ms, 20ms, 40ms, 80ms, 160ms. Add jitter to prevent thundering herd. Max retries: 5-10 attempts.
- 5Step 5: Fix - Reduce transaction scope or use conditional expressions: Split large transactions into smaller ones. Use conditional expressions to prevent conflicts. Check for ongoing transactions before starting new ones.
</>Code Examples
Retry DynamoDB Transaction with Exponential Backoff
1#!/bin/bash
2TABLE_NAME="my-table"
3MAX_RETRIES=5
4
5echo "=== DynamoDB Transaction with Retry Logic ==="
6
7# Example transaction items (JSON format for AWS CLI)
8TRANSACT_ITEMS='[
9 {
10 "Put": {
11 "TableName": "'${TABLE_NAME}'",
12 "Item": {
13 "id": {"S": "123"},
14 "status": {"S": "active"}
15 },
16 "ConditionExpression": "attribute_not_exists(id)"
17 }
18 }
19]'
20
21for ATTEMPT in $(seq 1 ${MAX_RETRIES}); do
22 echo "\nAttempt ${ATTEMPT} of ${MAX_RETRIES}"
23
24 # Execute transaction
25 aws dynamodb transact-write-items \
26 --transact-items "${TRANSACT_ITEMS}" 2>&1
27
28 if [ $? -eq 0 ]; then
29 echo "✓ Transaction successful"
30 break
31 else
32 if [ ${ATTEMPT} -lt ${MAX_RETRIES} ]; then
33 # Exponential backoff: 10ms, 20ms, 40ms, 80ms, 160ms
34 DELAY_MS=$((10 * (2 ** (ATTEMPT - 1))))
35 DELAY_SEC=$(echo "scale=3; ${DELAY_MS} / 1000" | bc)
36 echo "✗ Transaction conflict, retrying in ${DELAY_SEC}s..."
37 sleep ${DELAY_SEC}
38 else
39 echo "✗ Transaction failed after ${MAX_RETRIES} attempts"
40 fi
41 fi
42doneReduce Transaction Scope to Minimize Conflicts
1#!/bin/bash
2echo "=== Reducing Transaction Scope ==="
3echo "\nProblem: Large transactions increase conflict probability"
4echo "Solution: Split into smaller transactions"
5
6echo "\n=== Example: Split Large Transaction ==="
7echo "Instead of one transaction with 10 items:"
8echo "- Split into 2 transactions with 5 items each"
9echo "- Or use individual operations with conditional expressions"
10
11echo "\n=== Benefits ==="
12echo "1. Reduces conflict probability"
13echo "2. Faster execution"
14echo "3. Better error handling"
15echo "4. More granular retry logic"
16
17echo "\n=== Use Conditional Expressions ==="
18echo "Instead of transactions, use conditional expressions:"
19echo "aws dynamodb update-item \"
20echo " --table-name TABLE_NAME \"
21echo " --key '{"id":{"S":"123"}}' \"
22echo " --update-expression 'SET status = :status' \"
23echo " --condition-expression 'attribute_exists(id)' \"
24echo " --expression-attribute-values '{":status":{"S":"active"}}'"↗Related Errors
Provider Information
This error code is specific to AWS services. For more information, refer to the official AWS documentation.