Raghav Mishra
Raghav Mishra

Reputation: 579

Like (voting) system using Queue and MongoDB

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:

  1. To avoid such inconsistency I'm planing to use a queue (AWS SQS). Will using a queue solve my issue? (As per my analysis, it will)
  2. However, if the queue's subscribers (systems polling the queue) scales tomorrow, will I not face the same issue of count inconsistency?
  3. If not queues, is there a better way for solving this issue?
  4. How is this problem of inconsistency on likes solved by other social media platforms?

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

Answers (2)

Warren Parad
Warren Parad

Reputation: 4074

In general there are three sorts of answers to these problems of database inconsistency:

  • DB Full control - usually called atomic increment the DB takes over will handle making sure every update is applied.
  • Version/LastUpdated Flag - Validate another field that has a unique timestamp/version to be sure that the update is being applied to the last known good state. It will error if not, and then handle it
  • AWS SQS use a FIFO queue with the 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

Lucasz
Lucasz

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

Related Questions