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
EXPERTO
Uri
preguntada hace 5 años1317 visualizaciones
1 Respuesta
0
Respuesta aceptada

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
respondido hace 5 años
  • 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?

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas