DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

[20 Days of DynamoDB] Day 16 - Enhancing Write Performance with Batching

Posted: 2/Feb/2024

The DynamoDB BatchWriteItem operation can provide a performance boost by allowing you to squeeze in 25 individual PutItem and DeleteItem requests in a single API call - this can be done across multiple tables.

Here is an example that combines PutItem and DeleteItem operations for two different tables (customer, orders):

    _, err := client.BatchWriteItem(context.Background(), &dynamodb.BatchWriteItemInput{
        RequestItems: map[string][]types.WriteRequest{
            "customer": []types.WriteRequest{
                {
                    PutRequest: &types.PutRequest{
                        Item: map[string]types.AttributeValue{
                            "email": &types.AttributeValueMemberS{Value: "c3@foo.com"},
                        },
                    },
                },
                {
                    DeleteRequest: &types.DeleteRequest{
                        Key: map[string]types.AttributeValue{
                            "email": &types.AttributeValueMemberS{Value: "c1@foo.com"},
                        },
                    },
                },
            },
            "orders": []types.WriteRequest{
                {
                    PutRequest: &types.PutRequest{
                        Item: map[string]types.AttributeValue{
                            "order_id": &types.AttributeValueMemberS{Value: "oid_1234"},
                        },
                    },
                },
                {
                    DeleteRequest: &types.DeleteRequest{
                        Key: map[string]types.AttributeValue{
                            "order_id": &types.AttributeValueMemberS{Value: "oid_4321"},
                        },
                    },
                },
            },
        },
    })
Enter fullscreen mode Exit fullscreen mode

Be aware of the following constraints:

  • The total request size cannot exceed 16 MB
  • BatchWriteItem cannot update items

Recommended reading: BatchWriteItem API doc

Top comments (0)