141

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).
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
Sindhu
  • 2,422
  • 2
  • 19
  • 17

3 Answers3

222

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.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Harshal Bulsara
  • 7,898
  • 4
  • 43
  • 61
  • Thanks Harshal , In my use case, I always overwrite all the fields for each unique (partitionKey+sortKey) . I was wondering if I can mitigate some performance overhead using updateItem instead of PutItem . But seems like ,internally they works the same way (Internally it Deletes the item and Add new item) . – Sindhu Apr 28 '17 at 18:56
  • 5
    Any idea on performance implications using `update` instead of `put`? – Amir Karimi Oct 09 '17 at 09:15
  • 6
    @AmirKarimi, Basically Put will do 3 operations internally FindKey-> Delete -> Add and update will do FindKey -> Update so I think update will be faster – Harshal Bulsara Oct 09 '17 at 09:21
  • Note: In the above example, if we do PUT item with "UserId = 1 Country = India", it will delete the attribute "Name= ABC". Like Harshal Bulsara mentioned, it will delete it, then add the new Item – Juggernaut17 Jul 01 '20 at 17:21
  • 2
    Are you able to run UpdateItem when there is no item found and it will insert it? – Freddie Jan 05 '22 at 21:33
  • I haven't tried that. Logically that should not work though. – Harshal Bulsara Jan 06 '22 at 03:17
34

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 :(

Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 1
    From cost perspective, isn't it frustrating? I want to append a little data on a list attribute. Why would I consume writing capacity of whole item? Is there a work-around for this case? Let's say, the data I append is like 50byte. And my item is 200 kb. What is the correct approach to make it with minimum WCU? – neiloth Jan 14 '21 at 13:46
  • @neiloth You may want to ask that as a separate question, but typically for a list of items with frequent append operations, you'd want to make that data its own table with each append as a put, not a single record that needs to keep getting updated. – GrandOpener Aug 25 '21 at 14:57
  • @GrandOpener no need for new table, just new object with dedicated pk/sk namespace for the list items. 200kb is huge item. i would consider splitting it to multiple unless you really need all of it for all reads & you actually update most/all of it during writes. which sounds like not the case here. – Lukas Liesis Dec 15 '22 at 11:54
3

Slight correction in the above answer: Even the update item API takes the larger of the two item sizes(pre-update and post-update) into consideration. See documentation here.