D
D
Dmitry Bashinsky2019-06-06 15:16:45
ASP.NET
Dmitry Bashinsky, 2019-06-06 15:16:45

Concurrency in working with cache?

Hello, I want to achieve great performance in api.
Now the model is simple: Request -> in the database -> response
And there are no problems with competition because transactions help and the additional condition helps.
I want to do everything in the cache, and periodically upload the cache to the database.
There was a question on concurrency.
Here is a case
2 flows simultaneously want to change the balance of the user
Both took 1 state (100 UAH), but one of them was the first to fill in a new state (120) The
second, respectively, does not know about it and dumps its value there from 100
I thought I would record the change events (ChangeBalance +100) (ChangeBalance +40)
and aggregate everything into a heap before uploading it to the database and save it.
But here is the same problem, the collection of events in the cache, I pull the collection from the cache, add the event there and save it, and there is a possibility that I will overwrite the changes of another thread.
How can this problem be solved? At least cancel overwriting the cache so as not to lose data

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2019-06-08
@2ord

I'm not a concurrency specialist, but I think that the rebalancing itself should not be done competitively.
I think it's worth putting tasks to change the balance in a queue and that the queue has a lot of parallel handlers. Thus, only one handler will be responsible for each balance change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question