0
0
DynamodbConceptBeginner · 4 min read

DynamoDB Transaction Limits: What You Need to Know

In 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.

python
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'])
Output
Transaction succeeded
🎯

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.

Key Takeaways

DynamoDB transactions can include up to 25 unique items with a combined size limit of 4 MB.
Transactions can span a maximum of 10 tables in a single operation.
Ensure your table's throughput capacity can handle the transaction to avoid failures.
Use transactions to keep related data changes consistent and atomic.
Keep transactions small and efficient to maintain performance and avoid throttling.