DynamoDB UpdateItem

0

Hello,

What happen if two (or more) clients try to update the same item at the same time, each client updating different attributes in the item, e.g., client one tries to update an item with id=1 and set attribute a1=10, while client two tries to update the same item, but attribute a2=10?

Will one change overwrite the other? Will there be an error? Will both be accepted?

What if the item did not exist when the two requests were sent? What if the item did exist but the updated attributes did not? Does it make a difference?

To clarify the use case: I have a data source that looks like this: ID, Key, Value. For example:

  • 1, a1, 10
  • 1, a2, 20
  • 2, a5, 30
  • 1, a4, 15

Etc.
As can be see, there are multiple records with the same ID. I Would like to add it into DynamoDB in a way that the Partition key will be the ID and the attributes will be Keys. In the above example, I need to items in DDB that will look like:

  • ID: 1, a1: 10, a2: 20, a4: 15
  • ID: 2, a5: 30

As I want to handle these records concurrently, I want to verify what will happen if two clients/threads try to modify the same item at the same time. For instance, if I have an item in DDB with ID: 1 with A1:10 and I have two threads that one tries to set a2 to 20 and one that tries to set a4 to 15, without reading the item first. Will there be a situation were one of these updates will be lost?

Thanks

profile pictureAWS
专家
Uri
已提问 5 年前1317 查看次数
1 回答
0
已接受的回答

You can do an update item and just set the value for one attribute. Client1 can set a1=10 and client2 can set a2=10, that is no problem.

# aws dynamodb put-item --table-name test --item '{"pk": { "S": "1" }}'

# aws dynamodb get-item --table-name test --key '{"pk": { "S": "1" }}'
{
    "Item": {
        "pk": {
            "S": "1"
        }
    }
}

# aws dynamodb update-item --table-name test --key '{"pk": { "S": "1" }}' --update-expression "SET a1 = :a1" --expression-attribute-values '{":a1" : {"N":"10"}}' --return-values ALL_NEW
{
    "Attributes": {
        "a1": {
            "N": "10"
        },
        "pk": {
            "S": "1"
        }
    }
}

# aws dynamodb update-item --table-name test --key '{"pk": { "S": "1" }}' --update-expression "SET a2 = :a2" --expression-attribute-values '{":a2" : {"N":"10"}}' --return-values ALL_NEW
{
    "Attributes": {
        "a1": {
            "N": "10"
        },
        "pk": {
            "S": "1"
        },
        "a2": {
            "N": "10"
        }
    }
}
AWS
已回答 5 年前
  • There is no problem when attributes are different and updating at the same time and same item? what if are nested object, but different attributes?

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则