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?

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠