Viral Waitlist: Assigns same rank to users

Context:
I have built a viral waitlist feature so that when a user signs up they are assigned a rank in my waitlist. If they refer a friend they move up the waitlist, see screenshot below:


I calculate the waitlist rank by doing a search for all users who are currently marked as being on the waitlist, taking a count and then adding 1:

Problem!:
When multiple users are signing up at the same time or just a few seconds apart, they all get the same waitlist rank assigned to them!! I am guessing that doing the search for users on the waitlist, taking the count and adding 1 is taking too long.

How can i make sure that it correctly assigns a unique number for each new user who joins the waitlist?

Thank you so much for your help!!

it sounds like you’re dealing with concurrency/race condition issues.

You’re most likely correct.

There are a couple of discussions about race conditions in the forum. Some users have their own solutions like making the list static etc.

Here’s a great topic about achieving sequential numbers relaiably: How to achieve Unique, Sequential Numbers Reliably? - #25 by keith

This topic was automatically closed after 70 days. New replies are no longer allowed.