Update expression consistency


Do the ADD and DELETE update expressions guarantee write consistency?

Example: Given this code from the AWS docs

aws dynamodb update-item \
    --table-name ProductCatalog \
    --key '{"Id":{"N":"789"}}' \
    --update-expression "ADD Color :c" \
    --expression-attribute-values '{":c": {"SS":["Yellow", "Green", "Blue"]}}' \
    --return-values ALL_NEW


Is it guaranteed that the colors Yellow, Green, Blue will eventually show up? Or could this update to the item simply get overwritten and lost if there is another update at the same instant that adds the color 'Black'?

asked 3 years ago76 views
4 Answers

Hi, I think you meant to ask about read-after-write consistency. DynamoDB support strongly consistent reads, which means you will always get the most up to date data. If your read request is served in between the two write requests, then you will see {Yellow, Green Blue}. If your read request is served after the second write request is completed, you will see {Black}. NoSQL is a distributed data processing system, and is designed to support many types of applications. I would recommend starting with your business use case and working from there. If you have a need to capture and read every update to your table, then you may want to consider the DynamoDB Streams + Lambda/Kinesis architecture. If you are looking for the latest update, with read after write consistency, then use DynamoDB's strongly consistent reads. Hope this helps.

answered 3 years ago

Sorry, probably my question wasn't formulated clear enough.

If your read request is served after the second write request is completed, you will see {Black}
Considering that the ADD operation is used I would expect that generally you will see {“Black”, "Yellow", "Green", "Blue"}. This is at least what you'll get when executing it via the CLI manually.

My question was whether we have a guarantee that the result will eventually always be the above four values. Or could it happen that only one of the two update-item operations wins and I’ll only see either {"Yellow", "Green", "Blue"} or {“Black”}?
(I’m working on the assumption that initially the set “Color” is empty).

I’m aware I could do an additional read request first and use a ConditionExpression to make sure I don't lose any previously added values. But considering that I’m using ADD which is an idempotent operation I feel using an ConditionExpression should not be necessary.

answered 3 years ago

Thanks for that clarification. You are right, the result set will eventually show the four values.
As a programming best practice in general, and this applies to any API operation, you will implement an exponential backoff/retry in your code to catch any exceptions and retry them.
Let me know if you have any more questions.

answered 3 years ago

Okay great. Thanks for confirming that the four values will eventually show up and non of the update operations using ADD might silently get dropped.

answered 3 years ago

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