sidss
sidss

Reputation: 973

DynamoDBMapper conditional save for list of objects

My use case : Given list of objects, save all objects which are not already present in the DynamoDB table (i.e if an object is already present with same has and range key then don't override instead ignore or throw exception for all existing items)

I have 2 options in DynamoDBMapper :

  1. In serial or parallel loop conditionally save individual objects.

  2. BatchLoad all objects using given hash and range keys, compare yourself and filter out existing objects and save only non existing ones

Could you please help me understand which approach is better in terms of latency and cost ?

Upvotes: 2

Views: 897

Answers (1)

F_SO_K
F_SO_K

Reputation: 14799

The second option is not efficient. You are spending extra RCUs on reading the items when you don't need to, plus you are using extra bandwidth, adding latency and loading more things into application memory than you need to.

The easiest option is a serial loop to write the items with an attribute_not_exists condition (see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html#Expressions.ConditionExpressions.PreventingOverwrites. DynamoDB is seriously fast so unless you have a huge number of items to save you should be fine with that.

You cannot put conditions on DynamoDB BatchWrite operations, so if you want multi-threaded writes with a condition, you will have to write it yourself.

Upvotes: 2

Related Questions