DynamoDB
Boto3
AWS
Database Transactions
Python Library

Transactions with DynamoDB library Boto3

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. When working with DynamoDB using Python, the boto3 library is often the tool of choice. This article will explore how to perform transactions using boto3, offering technical explanations and code examples.

Transactions in DynamoDB

Transactions in DynamoDB allow you to perform multiple operations in a single, all-or-nothing request. This is crucial for maintaining data consistency and integrity. Transactions in DynamoDB are composed of two core operations:

  • TransactWriteItems: This operation can contain up to 25 action requests, which might include Put, Update, Delete, or ConditionCheck actions.
  • TransactGetItems: This operation retrieves multiple items from the tables in a single call.

Setting Up Boto3

To interact with DynamoDB using boto3, you need to install the library, set up your AWS credentials, and create a boto3 DynamoDB client:

bash
pip install boto3

Creating a boto3 Client

Set up your AWS credentials, then create a client:

python
import boto3

dynamodb = boto3.client('dynamodb', region_name='us-west-2')

Performing TransactWriteItems

The TransactWriteItems API provides atomic writes, meaning that all operations included must succeed, or all changes are rolled back. Consider the following use case where you need to make several updates across different items:

python
1response = dynamodb.transact_write_items(
2    TransactItems=[
3        {
4            'Put': {
5                'Item': {
6                    'PK': {'S': 'item1'},
7                    'attribute': {'S': 'value1'}
8                },
9                'TableName': 'MyTable',
10            }
11        },
12        {
13            'Update': {
14                'Key': {
15                    'PK': {'S': 'item2'}
16                },
17                'UpdateExpression': 'SET attribute = :value',
18                'ExpressionAttributeValues': {
19                    ':value': {'S': 'new_value'}
20                },
21                'TableName': 'MyTable',
22            }
23        }
24    ]
25)

Important Considerations

  • Atomicity: All tasks succeed or fail as a unit.
  • Limitations: Up to 25 actions per transaction.
  • Costs: Billed based on the size of read and write units.

Performing TransactGetItems

TransactGetItems lets you retrieve multiple items across several tables:

python
1response = dynamodb.transact_get_items(
2    TransactItems=[
3        {
4            'Get': {
5                'Key': {
6                    'PK': {'S': 'item1'},
7                },
8                'TableName': 'MyTable',
9            }
10        },
11        {
12            'Get': {
13                'Key': {
14                    'PK': {'S': 'item2'},
15                },
16                'TableName': 'AnotherTable',
17            }
18        }
19    ]
20)
21
22for item in response['Responses']:
23    print(item)

Points to Note

  • The call is consistent.
  • Immediate consistency is maintained.
  • 20 items maximum per call.

Use Cases for Transactions

  1. Coordinated Updates: Ensuring multi-table or multi-item writes that must be kept in sync.
  2. Curated Reads: Gathering exact sets of information atomically.
  3. Data Migrations and ETL Processes: Moving data while maintaining relational integrity.

Advanced Features

  • Condition Checks: Ensure certain conditions are met for transaction components; useful in optimistic concurrency scenarios.
  • Embedded Expressions: Use expressions for limited in-transaction computation.

Example with Condition Checks

python
1response = dynamodb.transact_write_items(
2    TransactItems=[
3        {
4            'Put': {
5                'Item': {
6                    'PK': {'S': 'item3'},
7                    'attribute': {'S': 'value3'}
8                },
9                'TableName': 'MyTable',
10                'ConditionExpression': 'attribute_not_exists(PK)'
11            }
12        }
13    ]
14)

Boto3 Error Handling

Consider handling exceptions such as TransactionCanceledException, which can occur if a condition fails:

python
1try:
2    response = dynamodb.transact_write_items(...)
3except botocore.exceptions.ClientError as e:
4    if e.response['Error']['Code'] == 'TransactionCanceledException':
5        print("Transaction failed:", e.response['Error']['Message'])

Summary Table of Key Points

FeatureTypeKey Notes
TransactWriteItemsWriteAtomic Max 25 actions per transaction
TransactGetItemsReadConsistent Max 20 items per transaction
Condition ChecksValidationEnsures item condition satisfaction
CostsPricingBased on read and write capacity used
Error HandlingReliabilityUse exceptions like TransactionCanceledException

In conclusion, transactions with boto3 and DynamoDB enhance the robustness and data integrity of your applications. Understanding and leveraging these transactional capabilities can greatly benefit use cases requiring consistent and atomic operations.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.