Home > OS >  BatchPutItem vs PutItem in DynamoDB
BatchPutItem vs PutItem in DynamoDB

Time:04-26

I have a use case where we don't get that many cases where there are list of entries in a single request to be updated and mostly, it is the case where only a single entry is requested to be updated. But in future, this might grow and hence I was thinking of using BatchPutItem instead of PutItem.

  • Are there any disadvantages using BatchPutItem when the there is only a single item to be updated in the request?
  • I think the RCUs consumed are the same, but are there any other differences or the BatchPutItem will behave same as PutItem when there is only a single entry in the request?

CodePudding user response:

From a monetary cost perspective, it shouldn't matter - the WCUs consumed are the same. The benefit of the Batch API is that fewer requests go over the wire and thus reduce the overall latency for writing multiple items.

The error handling is different between the two APIs and it requires a bit of additional complexity to deal with BatchPutItem. Even if the BatchPutItem request is successful, individual item writes may have failed and you need to inspect the response from the API and retry and failed writes yourself.

The regular PutItem would fail in a more straightforward way by returning an error / raising an exception.

This is just something to keep in mind and if you're going to use BatchWriteItem anyway, you'll have to build logic for that in any case and this wouldn't be a drawback.

tl;dr: Using BatchWriteItem with a single item should have no drawbacks in your case, because you'll have to build the retry logic anyway.

CodePudding user response:

Maurice's answer already covers most of the non-differences between a single-operation BatchWriteItem (sic) and a standalone PutItem. But you should be aware that there's also a difference between the capabilities of these operations:

The operation that a BatchWriteItem can do on items are very limited - you can delete an item, or completely replace it, but that's it.

In contrast, the stand-alone operations can do more:

  1. a stand-alone PutItem operation can be conditional (only replace the item if some condition matches on the previous value of the item). The operation can also return the previous value of the item, at no extra cost.

  2. a stand-alone UpdateItem operation can do even more: In addition to being conditional and returning the previous value of an item, this operation can also modify attributes of the item - not replace it entirely. These modification can even include expressions - e.g., increment an attribute in-place.

These additional capabilities are not available through BatchWriteItem. If you need any of these capabilities, you will have to use PutItem or UpdateItem directly, and won't be able to use a single-request BatchWriteItem.

  • Related