Atomic decimal counter

Hello! I have two entities: User and Cash documents. The cash documents represent transactions made by the user; either deposits or withdrawals. I would like to keep an updated balance of a given user without going through every Cash document and get the current balance, with two decimal point.

My current approach is to update the user everytime a cash transaction is made, but I fear that in a more concurrent enviroment I will face problems with CAS (the User object has more properties than the cash balance, and it is updated frequently).

I have been reading Couchbase documentation, and there are atomic counters in the Node SDK (see here), which would solve my issue, but it looks like they only support 64-bit integer values.

What is the correct approach (if there’s any) to achieve the requirements I am looking for? I am currently using the Community Edition 4.1 and the SDK version 2.2.4.

Hey @manusyone,

Why not store the users cash balance as a fixed-point whole number, then simply perform the division later?

Cheers, Brett

Thanks for the reply Brett. Yes, I guess that would solve it, store the balance in cents.

Just to make sure: the atomic counter is the best option to solve eventual concurrency issues in this use case, right?