DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

3

[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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay