Badmaash
Badmaash

Reputation: 795

Is Firestore (NoSQL) a good choice for social media apps?

We are building a social media web app using firebase and use firestore to store users and their posts.

When a user likes a post, we save it in posts/{postID}/likedBy/{userID} and also update totalLikes in the post document.

Let's say our app has 1 million daily users, and they all are liking viral posts very frequently.

Now, firebase says that a document cannot handle more than one write per second. However, we've seen that we can update the document several times per second, but they still don't recommend it.

My question is, what is the best way to store total post likes in firestore, if there's any. Or, should we use some other services? EDIT: Firestore's distributed counters are made for exactly as suggested by the answer below.

Also, I want to query only those posts which are not liked by a user.

The way I can query this is if our documents inside posts collection contains Map of all the users who liked it, and then run a query where the map doesn't contain current userID. This approach isn't good because it limits the number of likes a post can get as the document size in firestore cannot exceed 1mb.

Another way can be to save the liked posts in the user's document, however by this, we'll not only loose the functionality to just fetch those posts which are not liked by user, it'll also limit the number of posts a user can like.

Third way can be to store the users who liked the post in a sub-collection, which will also loose the query functionality. Similar case would be with storing posts liked by a user in a sub-collection.

Now, either I've not enough knowledge of firestore(actually any other NoSQL database), or I'm thinking right but it's just that NoSQL isn't made for social media apps.

Upvotes: 2

Views: 1603

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

Let's say our app has 1 million daily users, and they all are liking viral posts very frequently.

Now, firebase says that a document cannot handle more than one write per second.

My question is, what is the best way to store total post likes in firestore, if there's any. Or, should we use some other services?

This is the exact scenario for which Firebase recommends to use some distributed counters.

With distributed counterS, "each counter is a document with a subcollection of shards, and the value of the counter is the sum of the value of the shards."

"Write throughput increases linearly with the number of shards, so a distributed counter with 10 shards can handle 10x as many writes as a traditional counter." (traditional counter = counter in one document)

Upvotes: 2

Related Questions