I have a web application where users are allowed to like items. Each item have a property of the total likes he got.
The issue happens when a lot of users like the same item at the same time, then I am receiving incorrect values in SQL (caused by race condition).
As a temporary solution, I have created a worker thread in the controller constructor that works against a queue, when an item like/dislike request is receiving, I am queuing this request. The worker thread is dequeing the values and updating a dictionary that maps itemid to totalcount.
The worker thread then updates the database once every one minute with the result.
Side question: does context.SaveChanges()
save only what changed in an object? Or does it save all of the properties of the objects?
I have a feeling that this solution is not the right one, what is the best way to deal with such an issue?