The Concurrency in the cache?

Hi, want to achieve big performance in an IPA.
Now the model is simple: Query -> dB -> response
And problems with konkuretnost no help for the transaction and additional condition helps.

Want to do everything in the cache, and the cache periodically to fill in the database.

Had a question on concurrency.

That's the case

2 streams at the same time I want to change the balance of the user
Both picked up 1 state (100 UAH), but some of them first filled the new state (120)
Second not know about it and knocks back its value from 100

When I was planning to do it I thought I will write eventy changes (ChangeBalance +100) (ChangeBalance +40)
and before pouring in the database to segregrate all together and save.

But then the same problem, the collection of events in the cache, I pull the collection from the cache add to the event and retain, and there is a possibility that I will overwrite the changes of another thread.

How can I solve this problem? At least to cancel the overwriting of the cache so as not to lose data
March 23rd 20 at 19:22
1 answer
March 23rd 20 at 19:24
Not an expert on concurrency, but I think that the rebalancing is not necessary to perform competitive.
I think you should put the task of changing the balance in the queue and that the queue had many parallel processors. Thus, for every balance change will respond to only one handler.

Find more questions by tags CachingRedisASP.NET