Transaction Write in DynamoDB with Golang

2021.06.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction

There is a Go sdk for AWS services that allow you to interact with resources from your programs. One part of it is dedicated to DynamoDB. Today I want to show you how to use TransactWriteItems.

Basics

The basic way to update with the table is to use UpdateItem function.

Below UpdateItemInput is the argument to UpdateItem function. In ExpressionAttributeNames we specify that we want to update a username for which we create a placeholder #U. In ExpressionAttributeValues is where we provide the value to be updated. We also need to provide a primary Key and a TableName.

uii := &dynamodb.UpdateItemInput{
	ExpressionAttributeNames: map[string]*string{
		"#U": aws.String("username"),
	},
	ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
		":v": {
			S: aws.String("Hiro"),
		},
	},
	Key:              key,
	TableName:        aws.String("table_name"),
	UpdateExpression: aws.String("SET #U = :v"),
}

Transaction write

To perform transaction write we create TransactWriteItemsInput, that will go into TransactWriteItems function. Just like in case of UpdateItems and UpdateItemInput. In TransactWriteItemsInput there is TransactItems attribute that is a list of the operations we want to perform. TransactWriteItem is where we specify what we want to do, Update, Put or Delete. In our case we use Update and if you look closely, the value of Update is the same as the basic UpdateItemInput.

twii := &dynamodb.TransactWriteItemsInput{
    TransactItems: []*dynamodb.TransactWriteItem{
        &dynamodb.TransactWriteItem{
            Update: &dynamodb.Update{
                ExpressionAttributeNames: map[string]*string{
                    "#U": aws.String("username"),
                },
                ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
                    ":v": {
                        S: aws.String("username"),
                    },
                },
                TableName: aws.String("table_name"),
                Key:       key,
                UpdateExpression: aws.String("SET #U = :v"),
            },
        },
        &dynamodb.TransactWriteItem{
            Update: &dynamodb.Update{
                ...
            },
        },
    },
}

Limitations

There are several limitations to Transaction write. From the docs we read:

TransactWriteItems is a synchronous write operation that groups up to 25 action requests. These actions can target items in different tables, but not in different AWS accounts or Regions, and no two actions can target the same item. For example, you cannot both ConditionCheck and Update the same item. The aggregate size of the items in the transaction cannot exceed 4 MB.

Conclusion

We discovered how to perform transactional write to DynamoDB. We briefly introduced a basic UpdateItem operation. We went through TransactWriteItemsInput which goes into TransactWriteItems function and compared it to the Update operation from the beginning of the post. Lastly we found that there are limitations to Transactional write like up 25 action requests, we can target different tables as long as there are in same AWS account or Region.