DynamoDB Transaction Limits: What You Need to Know
DynamoDB, transactions have limits such as a maximum of 25 unique items per transaction and a total size limit of 4 MB for all items combined. Each transaction can include up to 10 unique tables, and throughput capacity must be sufficient to handle the transaction's read and write operations.How It Works
Think of a DynamoDB transaction like a small shopping cart where you can add up to 25 different items at once. These items represent the database records you want to read or write together. The total size of all these items combined cannot exceed 4 MB, which is like the maximum weight your shopping cart can carry.
Additionally, these items can come from up to 10 different tables, similar to shopping in up to 10 different stores but checking out in one go. DynamoDB ensures that either all these operations succeed together or none do, keeping your data safe and consistent.
However, your database capacity (read/write throughput) must be enough to handle the transaction. If your capacity is too low, the transaction might be throttled or fail.
Example
This example shows a DynamoDB transaction that writes two items to two different tables within the allowed limits.
import boto3 from botocore.exceptions import ClientError dynamodb = boto3.client('dynamodb') try: response = dynamodb.transact_write_items( TransactItems=[ { 'Put': { 'TableName': 'Table1', 'Item': { 'PK': {'S': 'item1'}, 'Data': {'S': 'value1'} } } }, { 'Put': { 'TableName': 'Table2', 'Item': { 'PK': {'S': 'item2'}, 'Data': {'S': 'value2'} } } } ] ) print('Transaction succeeded') except ClientError as e: print('Transaction failed:', e.response['Error']['Message'])
When to Use
Use DynamoDB transactions when you need to make multiple changes that must all succeed or fail together, such as updating related records in different tables. This is important in financial applications, inventory management, or booking systems where data consistency is critical.
Keep transactions small and within limits to avoid throttling and ensure good performance. For example, if you need to update more than 25 items or exceed 4 MB, consider breaking the operation into smaller transactions or redesigning your data model.
Key Points
- Maximum 25 unique items per transaction.
- Total size of all items in a transaction cannot exceed 4 MB.
- Transactions can span up to 10 different tables.
- Throughput capacity must support the transaction's read/write operations.
- Transactions ensure all-or-nothing execution for data consistency.