dynamo-db
  1. dynamo-db-acid-properties

ACID Properties - DynamoDB Transactions

Syntax

transact_write_items(
    TransactItems=[
        {
            'ConditionCheck': {
                'TableName': 'string',
                'Key': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ConditionExpression': 'string',
                'ExpressionAttributeNames': {
                    'string': 'string'
                },
                'ExpressionAttributeValues': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ReturnValuesOnConditionCheckFailure': 'ALL_OLD'|'NONE'
            },
            'Delete': {
                'TableName': 'string',
                'Key': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ConditionExpression': 'string',
                'ExpressionAttributeNames': {
                    'string': 'string'
                },
                'ExpressionAttributeValues': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ReturnValuesOnConditionCheckFailure': 'ALL_OLD'|'NONE'
            },
            'Put': {
                'TableName': 'string',
                'Item': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ConditionExpression': 'string',
                'ExpressionAttributeNames': {
                    'string': 'string'
                },
                'ExpressionAttributeValues': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ReturnValuesOnConditionCheckFailure': 'ALL_OLD'|'NONE'
            },
            'Update': {
                'TableName': 'string',
                'Key': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'UpdateExpression': 'string',
                'ConditionExpression': 'string',
                'ExpressionAttributeNames': {
                    'string': 'string'
                },
                'ExpressionAttributeValues': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ReturnValuesOnConditionCheckFailure': 'ALL_OLD'|'NONE',
                'ReturnValues': 'NONE'|'ALL_OLD'|'UPDATED_OLD'|'ALL_NEW'|'UPDATED_NEW'
            },
            'ConditionCheck': {
                'TableName': 'string',
                'Key': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ConditionExpression': 'string',
                'ExpressionAttributeNames': {
                    'string': 'string'
                },
                'ExpressionAttributeValues': {
                    'string': {
                        'B': b'bytes',
                        'BOOL': True|False,
                        'BS': [
                            b'bytes',
                        ],
                        'L': [
                            {
                                ...
                            },
                        ],
                        'M': {
                            'string': {
                                ...
                            }
                        },
                        'N': 'string',
                        'NS': [
                            'string',
                        ],
                        'NULL': True|False,
                        'S': 'string',
                        'SS': [
                            'string',
                        ]
                    }
                },
                'ReturnValuesOnConditionCheckFailure': 'ALL_OLD'|'NONE'
            }
        },
    ],
    ReturnConsumedCapacity='INDEXES'|'TOTAL'|'NONE',
    ReturnItemCollectionMetrics='SIZE'|'NONE'
)

Example

client.transact_write_items(
        TransactItems=[
            {
                'Put': {
                    'TableName': 'MyTable',
                    'Item': {
                        'PartitionKey': {'S': 'key'},
                        'SortKey': {'N': '1'},
                        'Attribute': {'S': 'value'}
                     },
                      'ConditionExpression': 'attribute_not_exists(PartitionKey) and attribute_not_exists(SortKey)'   
                }
            }
        ]
    )

Output

The output of the above code will be the transaction result containing information about the operations that were carried out.

Explanation

DynamoDB Transactions provide features such as Atomicity, Consistency, Isolation, and Durability (ACID) to ensure that all the tables are updated in a transaction either completely or not at all. In DynamoDB, transactions work with application programming, and it helps to manage multiple related tasks that store data in DynamoDB tables.

In the example above, we are using a TransactWriteItems operation to put a new item in the MyTable. We have also specified a ConditionExpression to check if the PartitionKey and SortKey attributes do not exist and only if the condition is satisfied, the item will be put.

Use

Use DynamoDB transactions when you need to carry out multiple related tasks that store data in DynamoDB tables. Transactions ensure that either all the tables are updated by the transaction (completeness) or none (atomicity) and provide ACID properties for you.

Important Points

  • Transactions are not available in all AWS regions.
  • You can perform up to 25 write operations in a single transaction.
  • In a transaction, DynamoDB uses strongly consistent reads, and it returns a transactional view of the data for all items read or written in the transaction.
  • Every operation in a transaction must target the same partition key.
  • Transactions do not support global secondary indexes (GSIs), but they work with local secondary indexes (LSIs) defined on transaction tables.
  • Transactions last for a maximum of 10 seconds.

Summary

In summary, DynamoDB Transactions provide features such as Atomicity, Consistency, Isolation, and Durability (ACID) to ensure that all the tables are updated in a transaction either completely or not at all. Transactions allow you to manage multiple related tasks that store data in DynamoDB tables and provide ACID properties for you. Transactions do not support GSIs, but they work with LSIs defined on transaction tables, and they last for a maximum of 10 seconds.

Published on: