0

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.

Varun Gawande
  • 870
  • 9
  • 21

2 Answers2

1

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.

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
  • Okay one such technique to make the update idempotent is using Conditional Update then. Are you aware of any resources or books that focus on advanced techniques like these? – Varun Gawande Jul 04 '22 at 10:37
  • 1
    Easiest is do a transactional update with a client request token. Then any repeats within ten minutes won’t be processed. Best book is by Alex DeBrie. – hunterhacker Jul 05 '22 at 03:44
-1

DynamoDB lets you choose a consistency model based on the needs of your application.

By default, DynamoDB is eventually consistent. Eventually consistent might not always reflect the results of the latest write. However, according to the AWS docs, consistency across all copies of data is usually reached within a second.

If your application cannot tolerate eventual consistency, you have the option of strong consistency. A strongly consistent read in DynamoDB returns a result that reflects all writes that received a successful response prior to the read. You can achieve this by sending an optional parameter to the request in DynamoDB.

For a more thorough treatment on this topic as it relates to DynamoDB, check out the AWS documentation about read consistency.

Seth Geoghegan
  • 5,372
  • 2
  • 8
  • 23
  • I'm not concerned about reading the table right now. I'm more worried about the case where suppose an increment and a decrement occur together, can one be lost with the above update logic? Like they do with Race Conditions. – Varun Gawande Jul 01 '22 at 14:16