Search code examples
amazon-dynamodbdynamodb-queries

How Consistent are DynamoDB Atomic Counters during Race Condition possibilities when using ADD updates?


I wish to use a simple increment and decrement for my dynamoDB table.

The update looks something like:

    aws dynamodb update-item \
    --table-name table_name \
    --key 'le key' \
    --update-expression "ADD #C :delta" \
    --expression-attribute-names '{"#C": "count"}' \
    --expression-attribute-values '{":delta":{"N":"1"}}' \
    --return-values UPDATED_NEW

For increment delta would be 1, for decrement it would be -1.

In concurrent workloads and race condition possibilities, how consistent would such updates be if we have multiple increments and decrements occurring together?

I hope it would be pretty consistent, considering we are NOT "getting and setting" the values, but directly passing the delta to be added. This is the reason I've added this as a separate question and don't consider it a duplicate of questions like these where people often suggest conditional updates.


Solution

  • Write access to an individual item is serialized, so you don’t have to worry about that - even under heavy load.

    The thing to consider (worry about) is when your call produces something like a network timeout error you won’t know if the mutation happened or not. Should you redo the call? You won’t know.

    So usually the fancier techniques are to handle this situation and make the call idempotent somehow.