Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DynamoDb PutItem vs UpdateItem?

Based on DynamoDb documentation why would anyone use updateItem instead of putItem?


  • PutItem - Writes a single item to a table. If an item with the same primary key exists in the table, the operation replaces the item. For calculating provisioned throughput consumption, the item size that matters is the larger of the two.
  • UpdateItem - Modifies a single item in the table. DynamoDB considers the size of the item as it appears before and after the update. The provisioned throughput consumed reflects the larger of these item sizes. Even if you update just a subset of the item's attributes, UpdateItem will still consume the full amount of provisioned throughput (the larger of the "before" and "after" item sizes).
like image 985
Sindhu Avatar asked Apr 27 '17 20:04

Sindhu


People also ask

What is DynamoDB PutItem?

PDF. Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item.

Does PutItem overwrite DynamoDB?

With the PutItem call, you provide an entire Item to be placed into your DynamoDB table. This action will create a new Item if no Item with the given primary key exists, or it will overwrite an existing Item if an Item with that primary key already exists.

Is DynamoDB PutItem Atomic?

DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality. All these operations are atomic. PutItem — Create an item. GetItem — Read an item.


2 Answers

The main difference between the two is, PutItem will Replace an entire item while UpdateItem will Update it.

Eg.

I have an item like:

userId = 1 Name= ABC Gender= Male 

If I use PutItem item with

UserId = 1 Country = India 

This will replace Name and Gender and now new Item is UserId and Country. While if you want to update an item from Name = ABC to Name = 123 you have to use UpdateItem.

You can use PutItem item to update it but you need to send all the parameters instead of just the Parameter you want to update because it Replaces the item with the new attribute(Internally it Deletes the item and Adds a new item)

Hope this makes sense.

like image 167
Harshal Bulsara Avatar answered Oct 21 '22 00:10

Harshal Bulsara


PutItem overwrites the whole item (all attributes) with the new version being passed while UpdateItem will only Update the passed attributes

Performance: PutItem may affect on the performance if you overwrite the entire item so often as it involves more operations than UpdateItem FindItem, DeleteOldVersion, and AddNewVersion

From cost prospective, it is different as well:

AWS calculates the cost based on used read/write capacity units that are completely tied to the size of the item being overwritten/updated.

In case of PutItem, the size will be the larger of the new and old versions of the item. For example, replacing a 2 KB item with a 1 KB, It will consume 2 WCUs however subsequent requests will only use 1 WCU. so if you're overwriting so often and the size of the item changes heavily that will calculate always the larger version of the item and affects on the cost.

In case of modifying items using UpdateItem, the size includes all of the item’s pre-existing attributes, not the larger version like PutItem :) but also not just the ones being added or updated :(

like image 39
Muhammad Soliman Avatar answered Oct 21 '22 00:10

Muhammad Soliman