Back

Race condition while updating data

  • 1
  • Android
  • Flutter
  • Databases
  • Cloud
Raman
16 Feb, 2024, 09:53

I'm implementing a like system in my app. So, the users can like a post posted by another user. Now, I'm storing the likes by creating a new collection with 2 attributes (userId and postId). The problem I'm facing is, how to know how many likes this post already has?

I looked up the docs to find a way to perform aggregation queries, but can't find any.

So, I thought of this solution, I will create an appwrite function which will be responsible for toggling the like on a post by a user. Whenever the user likes a post, I'm incrementing an integer totalLikes stored along with the post data.

For now, I'm doing this in an appwrite function, every time a user likes a post, I'm fetching the current likes count for that post, adding 1 to it, and then updating it back onto the database.

But, now again, I wasn't able to find a way to increment this variable atomically. Or is a race condition never going to happen, even for millions of users?

So, I have a couple of questions? 1.⁠ ⁠Does appwrite have support for aggregation queries? 2.⁠ ⁠In appwrite, is there any way to atomically increment values? I want something like this: update({ 'totalLikes': FieldValue.increment(1), }) 3.⁠ ⁠Is there any way to run transactions, just like in firebase?

TL;DR
Developers are facing a race condition issue while updating data in their app. They are implementing a like system and are storing likes in a separate collection. However, they are unsure how to retrieve the total number of likes for a post. They considered using aggregation queries but couldn't find any support for it in the appwrite documentation. Their current solution involves creating an appwrite function to toggle likes and increment the totalLikes count for a post. However, they are concerned about race conditions and are unsure if appwrite supports atomic increment operations. They also want to know if appwrite supports transactions like Firebase. Solution: 1. Appwrite currently
Raman
16 Feb, 2024, 09:54

@Steven Sorry for tagging but this is kinda urgent as this is affecting our development phase

Raman
16 Feb, 2024, 09:55

If a certain user have reached the limit rate than how many hours or minutes the user have to wait to sign in again in cloud

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more