Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB atomic counter for account balance

In DynamoDB an Atomic Counter is a number that avoids race conditions

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters

What makes a number atomic, and can I add/subtract from a float in non-unit values?

Currently I am doing: "SET balance = balance + :change"

(long version) I'm trying to use DynamoDB for user balances, so accuracy is paramount. The balance can be updated from multiple sources simultaneously. There is no need to pre-fetch the balance, we will never deny a transaction, I just care that when all the operations are finished we are left with the right balance. The operations can also be applied in any order, as long as the final result is correct.

From what I understand, this should be fine, but I haven't seen any atomic increment examples that do changes of values other than "1"

My hesitation arises because questions like Amazon DynamoDB Conditional Writes and Atomic Counters suggest using conditional writes for similar situation, which sounds like a terrible idea. If I fetch balance, change and do a conditional write, the write could fail if the value has changed in the meantime. However, balance is the definition of business critical, and I'm always nervous when ignoring documentation

-Additional Info-

All writes will originate from a Lambda function, and I expect pretty much 100% success rates in writes. However, I also maintain a history of all changes, and in the event the balance is in an "unknown" state (eg network timeout), could lock the table and recalculate the correct balance from history.

This I think gives the best "normal" operation. 99.999% of the time, all updates will work with a single write. Failure could be very costly, as we would need to scan a clients entire history to recreate the balance, but in terms of trade-off that seems a pretty safe bet.

like image 338
FrozenKiwi Avatar asked May 17 '16 21:05

FrozenKiwi


People also ask

What is atomic counter in DynamoDB?

You can use the UpdateItem operation to implement an atomic counter—a numeric attribute that is incremented, unconditionally, without interfering with other write requests. (All write requests are applied in the order in which they were received.) With an atomic counter, the updates are not idempotent.

Does DynamoDB support atomic transactions?

DynamoDB transactions provide developers atomicity, consistency, isolation, and durability (ACID) across one or more tables within a single AWS account and region.

Does Amazon DynamoDB support both increment and decrement atomic operations?

Does Amazon DynamoDB support both increment and decrement atomic operations? No, neither increment nor decrement operations.

What is an atomic counter?

An Atomic Counter is a GLSL variable type whose storage comes from a Buffer Object. Atomic counters, as the name suggests, can have atomic memory operations performed on them. They can be thought of as a very limited form of buffer image variable.


1 Answers

The documentation for atomic counter is pretty clear and in my opinion it will be not safe for your use case.

The problem you are solving is pretty common, AWS recommends using optimistic locking in such scenarios. Please refer to the following AWS documentation, http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html

like image 152
Shibashis Avatar answered Oct 02 '22 20:10

Shibashis