Does DynamoDB `UpdateItem` operation read data with strong consistency while `UpdateExpression` is specified?

0

So I have such use case for this operation. I am appending an item to a list attribute with UpdateExpression, and what I also have is that if this attribute does not exist, just create new one.

So expression looks like this

SET history = list_append(if_not_exists(history, :historyEmpty), :historyRecord)

However I am doing this call twice in a very short time span and I we have observed that second call might overwrite the first one. For example, if first call was done with :historyRecord = [{ data: 'data' }], and second one was done with :historyRecord = [{ data: 'data2'}] I will only have the second one in the database. This does not happen all the time tho, only if those two calls happened closely in time and that is why I suspect it might be related to read consistency. (first operation happens during an API call, and second one is during a state machine that gets started from that API call). The expected behavior is to have historyRecord = [{ data: 'data' }, { data: 'data2' }] in the database.

asked 2 years ago1095 views
1 Answer
1

DynamoDB does not do any reads when executing a write operation. Furthermore, it provides serializable isolation, so when you execute 2 requests concurrently, one of those items will get executed before the other and the second request will always see the most up to date data (the results of the first request).

I suspect that perhaps there is an issue in your logic when executing the requests, as both values should exist when all processes are complete. Ensure you are not overwriting data by calling PutItem etc...

profile pictureAWS
EXPERT
answered 2 years ago
  • I think you misunderstood. I am not doing two operations. I do only a single operation, which is UpdateItem. Which, I was thinking does reading by itself since it needs to resolve if_not_exists in updateExpression.

  • Understood, but my answer remains relevant. DynamoDB does not read, there's no consistency at play. All write operations go to the leader node of a partition, meaning it will only see the most up-to-date values for that item. if_not_exists is resolved at the underlying storage level, on the SSD disk, in a serializable way. All single operation writes (PutItem, UpdateItem, DeleteItem) provide full ACID guarantees.

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