DynamoDB updateItem

0

I was wondering what happen if many clients try to update the same item at the same time, each client updating different attributes in the item.

For example:

1° case:

{
"PK": "PK1",
"SK": "SK1",
"Attr1: "Value1",
"Attr2: "Value2"
}

One client is updating "Attr1" and second client "Attr2" at the same time.

2° case:

{
"PK": "PK1",
"SK": "SK1",
"Attr1: {
      "SubAttr1": "SubValue1",
      "SubAttr2: "SubValue2"
     }
}

One client is updating "SubAttr1" and second client "SubAttr2" at the same time.

These updates can be done by AppSync Resolvers.

Thanks

Eddy
asked a year ago970 views
1 Answer
3
Accepted Answer

DynamoDB handles concurrent updates using a technique called optimistic locking. When multiple clients try to update the same item simultaneously, DynamoDB ensures that the last write doesn't overwrite the previous writes without considering the changes.

First Case: When one client updates "Attr1" and another updates "Attr2" at the same time, these updates will be considered as separate and independent actions. DynamoDB will merge these updates and apply them to the item without overwriting each other. So both updates will be successful and the final item will have the updated values for both "Attr1" and "Attr2".

Second Case: When one client updates "SubAttr1" and another updates "SubAttr2" at the same time, these updates will also be treated as separate and independent actions. However, since the attributes being updated are nested within the same parent attribute "Attr1", it's essential to use the appropriate DynamoDB expression to update the nested attributes.

Use the UpdateExpression with the "SET" action and the appropriate document path to update the nested attributes:

UpdateExpression: "SET Attr1.SubAttr1 = :newSubAttr1, Attr1.SubAttr2 = :newSubAttr2"

This will ensure that only the specified sub-attributes are updated, and no data is lost in the process.

When using AppSync resolvers, the same principle applies. You need to use the correct mapping templates in your VTL (Velocity Template Language) scripts to update the nested attributes in DynamoDB. This will ensure that concurrent updates to different sub-attributes are handled correctly without overwriting or losing data.

profile picture
EXPERT
answered a year ago
  • Just to clarify, there's no optimistic locking going on. Mutations to individual items are strictly serialized internally. There's no such thing as at the same time. One update will happen before the other.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions