Reputation: 579
Scenario: We're building an application with posts. These posts can be liked by users (just like a social media platform). There may be continuous likes for a specific post. We're using Node.JS on our server and MongoDB as our database.
Concern: I was planing to just build a RESTful API that is hit every time a post is liked. This API simply updates the Database with the incremented/decremented like count for the post. But, if the same post faces multiple activities for likes, then the Database entry will start becoming inconsistent and the count values will start becoming inconsistent.
Questions:
Additional Details
What kind of inconsistency is expected? --> If 2 users like a post at the same time, the node.js instance will accept both requests in parallel, they will both fetch the database entry (at almost the same time) and increment the count of like. When the update takes place, both the instances update the same data value with different values, creating inconsistency.
Upvotes: 0
Views: 310
Reputation: 4074
In general there are three sorts of answers to these problems of database inconsistency:
atomic increment
the DB takes over will handle making sure every update is applied.postId
which will guarantee serial processing of the messages, you will have to make sure you also process them in serial in your lambda/processing unit, but that's trivial.Upvotes: 1
Reputation: 1218
I think Mongodb has an atomic increment that would be solving your "inconsistency issues" https://docs.mongodb.com/manual/reference/operator/update/inc/
Upvotes: 1