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

abhirockzz

Abhishek Gupta

Posted on February 2, 2024

[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

💖 💪 🙅 🚩
abhirockzz
Abhishek Gupta

Posted on February 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related