Badge/Achievement System using MERN stack

Hi.I’m building a SPA using the MERN stack (including React Router). I want to implement/integrate a badge system similar to Khan Academy’s or StackOverflow’s.
Beside running a cron job every a set amount of time, which checks certain conditions on user schema (ex: has more than 50 points) and adds the correct badge to the user schema, I have no other idea.
Edit: There is the event based method, where on certain events (ex: made a post) the system checks if it should issue a badge to the user.
As far as I searched, there is little information on the internet on how to implement what I just described.I’m wondering if there are other ways other than cron jobs, or perhaps if it is the best, should I have a separate server that does the job, or should the API server (separated from the webpack/react one) to the job asynchronously ?
Thank you in advance

It sounds like you want to have your app display a badge on the user’s profile once they meet a certain requirement (say they got 50 points)

I see a few ways to handle this:

  1. Handle it on an event, to create a “badge” once they are over “50 points” or whatever criteria. So lets say I get points after I complete a challenge, every time I do so you need to check to see how many points I have. Once I have enough, you also create the badge.
    This optimizes for reads against the badge as you create the badge 1 time, but are always reading to see if you need to create the badge on each completed challenge.

  2. Don’t create anything, and calculate if they have the badge on the fly. This means when the user goes to their “profile” or wherever the badge is, you calculate if they have enough points for the badge itself. This optimizes for writes, but is terrible for reads as you will be going thru the DB all the time, but you will only have to create anything more than “completed challenge”.

  3. Use the “cron job” approach here to check ever so often and create the badge. This option optimizes for data integrity, but will do a lot of work without any reason

I personally think the 1st approach is the best, and the 3rd approach (the cron job) approach would be good as a “backup” you run every now and then to make sure the denormalized stuff is still in order, but odds are you don’t really need/want a cron job running all the time when you could just manage everything on the actions where the user “does stuff”.

There is no getting around the fact a badge system relies on getting existing user information and adding it to what the user has already done to know if they get a badge, the question comes down to when and how often could this occur, and when do you want to show the user they get a badge? I think on the end user perspective the moment I should get a badge I want to get some feedback I do, so having a cron running every hour/day wouldn’t work just on user experience alone.

So, I say don’t use a cron, rather deal with the “badge logic” on events. Hope that helps :slight_smile:

Good luck, keep building!

1 Like