Hi, I have built a lottery system where the winner is given a unique gift card code that they can redeem. The problem is that, when two users enter the lottery at the same time and they both win, the same code is given to both users (the code is stored as a list in a field within a table entry)
I have tried checking if the code exists in the past winner data before giving out the code, and while this works in testing, it fails when a large amount of users win at the same time.
I have also tried the database trigger backend workflow where if there is a duplicate entry I change the code (I check this by reading the format of the slug of the entry; first make slug = gift card code, and if slug contains “-1” (since “-1” gets added automatically if the slug value already exists) , change the code to a new one) but this method also fails when many users win at once (some entries will still have a “-1” at the end of the slug)
I am running out of ideas on how I can resolved this issue. Any help will be greatly appreciated. Thank you!
Another possible method is to schedule the action of assigning the gift card code in a backend workflow. Not a guarantee but it’s a simple workaround. From what I understand of what you want to do, the user doesn’t have to receive the code immediately.
Alternatively have your Gift Codes as a separate Thing in your database with a User field. You can then search for(unassigned gift code) :random item
Pre make 10 million database items (new datatype Gift Code) each with a randomly generated string. Then put a User field to indicate it is taken/who took it.
Then the user searches for a random item (with a constraint User field is empty) and claims it.
Although Bubble :random item is kinda crap and might still return people the same item…
We actually show the code immediately to users, so the workflow needs to run when the user triggers the lottery.
I currently use :random item when selecting which code to give out to users to lessen the chance that the same code is given to multiple users, but this still fails and bubble gives out the same code sometimes.
I will try the method mentioned by @ihsanzainal84 and @tylerboodman and create a separate table for the gift codes, mark each entry with the winner’s username when won, and only give out codes that have the username field empty and see if this works. Using a separate table for the codes might be more reliable than using a list within a table entry. I will let you know how this goes.
I am still open to any other suggestions! Thank you.
Just to give an update, the implementation mentioned above also failed, unfortunately.
And the fail-safe I’ve implemented (quoted below) also failed, and bubble created two entries with the same slug within the same table (I didn’t think this was possible).
I have also tried the database trigger backend workflow where if there is a duplicate entry I change the code (I check this by reading the format of the slug of the entry; first make slug = gift card code, and if slug contains “-1” (since “-1” gets added automatically if the slug value already exists) , change the code to a new one) but this method also fails when many users win at once (some entries will still have a “-1” at the end of the slug)
Use list shifter’s hidden nanoid function to generate arbitrarily universally unique ids. These can be so entropic that none will reoccur in the lifespan of the universe. Just search the forum.
I currently have that workflow implemented, but when a large number of users do the lottery all at once, sometimes two or more users “claim” the same lottery ticket (even if I check if the ticket already exists in the winners list)
As a fail-safe for this, I’ve implemented a database trigger for this where I set the slug of the winner data as the code they’ve won, and if the slug gets appended by a “-1” meaning there already is an entry with that code, I replace the code with a new one. I’ve done this because it says in bubble’s documentation that no two entries can have the same slug in the same table, but this also failed and two entries were created with the same slug.
Thanks for the suggestion. If you’re suggesting that I generate the gift card code myself, I cannot do that since the gift card code is already created when I purchase them from Amazon for example. So I have a list of codes already pre determined, and I need to give it out to users on the spot when they win the lottery.
If that’s your process then linking a single user to a user field to a Gift Code database type when issuing gift codes should eliminate any duplicate issues.
I’m confused. How are you getting duplicate entries in your Gift Code’s single user field?
Are you creating gift code entries at the point of issuing the gift code to a user? You shouldn’t be doing that. Since you say gift codes are from Amazon gift cards you purchased the gift codes should already be in your database when a user is issued a gift code.
The gift code entries are already pre-made, like you suggested.
What happens is that when two users win at the same time, the linked user field for that gift card code entry is empty for both user’s workflow (the “only when linked user is empty” condition passes for both users), and both users end up getting the same code.
Again, this does not happen when the traffic is slow. The duplicates only get created when a large amount users enter the lottery within a short timeframe and multiple winners are created at the same time.
Ah okay so it sounds like you also have a “Gift Code” field in your User dB type. So here is what your workflow look like:
User wins a code:
search for available Gift Code> add User to the user field in Gift Code > add Gift Code (use the result of… operator) to gift code field in User > blah blah blah
Thanks for the suggestions, I really appreciate them. What you’ve suggested, however, I’ve actually already implemented; I also add the gift code to the user’s table entry, and
when giving out a new code to a user I do a “Do a search for user data where code=new code: count is 0” to make sure that the code has not already been given out. But this does not prevent duplicates from being created when two or more users win and claim the same code at the same time. It’s very frustrating!
What I suggested should in theory prevent duplicates since one Gift Code field can only store 1 user, but I guess there might be a chance of the user field in Gift Code being overwritten? I’ve not experienced this myself though.
Mind sharing a snapshot of the workflow in question?