Viral Signup Queues
So you find yourself building the next Uber for cheese danishes and then the product guy tells you that he wants a viral signup queue. At first you’re angry, but then you think, if cheese danishes being delivered to your doorstep weren’t enough of a draw, the lure of being the first to experience a doorstep danish should do the trick.
You initially think that you might just use one of those SaaS solutions, but then you think of your programming honor (and also what your mother might think) and decide you can build it yourself.
You think to yourself, with any type of queue, you’ll need to keep track of a few things like:
- Which users you who have given premium danish doorstep delivering access
- How many users are currently in the queue for danishes
- How many danish eaters you are letting in at a time (for a controlled roll-out)
- A system for queue jumping to let more motivated danish eaters in earlier (if you want that feature)
For the queue you decide that you need a table that ties a user with the queue. The queue itself just contains the order that the user signed up at and the id of the user the queue is associated with.
Pondering what the user table needs you decide that it needs all the normal user things, plus a column that indicates if the user is in the queue. If they are not in the queue then they have full access to yummy cheesy danishes.
Because you cherish and honor your sys-ops gal, you decide you will do a controlled rollout of users. The only thing you really need for that is somewhere to store how many users you want to let in at a time.
When you set your rollout number, you decide that this number will check how many users have full access and how many users you want to have full access. The difference between the two determines how many new queued users you’ll let in.
Once you know how many queued users to let in, you can get and remove the chunk of users from the queue table and set their user status as a full blown cheesy danish loving user.
Virus’ are not cool, but you decide that going viral is pretty cool and want to implement queue jumping. To do this you decide to give a number for how far up a specific action will jump the user. For example, if the user shares a link, you might jump the user up 3 places in the queue.
You instantly think, what if I have millions of danish lovers queued up and trying to get in on this craze?! How would I update everyone’s order and not completely kill the database?
After watching many cat videos you come back to reality and realize that you don’t have to update every single row. You only need to update four rows. 1 row for the jumper and 3 rows for the users in front of the jumper.
First you grab the four rows, the jumper plus 3 users in front of the jumper. You set the jumper’s order (the last in the chunk) to the first in the chunk and then shift the original 3 non-jumping user’s order up one. Boom!
- User › 2
- User › 3
- User › 4
- Jumper › 1
Feeling proud of yourself for building your own viral signup queue, you flip the trigger and ship the product. Cheese loving danish eaters immediately start rejoicing and enshrine your beautiful face on a gigantic bronze cheese danish.