Tutorial :How would you implement a badge system like StackOverflow's?


Just wondering how you would do this. Would you have some sort of process that scanned through all the actions that took place over the past X minutes? Would you trigger a badge update check every time any action took place (up vote, down vote, tag, etc.)? It seems like this would be a relatively expensive process one way or another.


Perhaps a bit of both. I would probably queue up a check every time an action occurred and have a process that goes down the queue and determines if a badge has been earned for each check. This way it's semi-real time without causing pages to take more time to load. In fact, here on StackOverflow I've noticed a delay between when the badge is earned and when the system realizes it, so this site probably uses the same approach.


I wouldn't trigger an update every time someone did an action; what I would do instead is a batch process that looks through every action performed in the last minute, sums things up with the "minute ago" state of every user, and gives out new badges based on that.


Use a database with 'hooks/triggers'. Then you can trigger an event on certain state changes, say for instance, you have a table that records every single up vote on a given answer.

You know that Answer > 10 Upvotes == "Good Answer", so you'd just put a trigger that occurs when LAST_STATE = 9 and NEXT_STATE = 10 and ENTRY_AWARDED_GOODANSWER = false.

Postgresql is one such database that supports such triggers.

Read more here: http://www.postgresql.org/docs/8.1/static/triggers.html


I'd probably use a Service Broker Queue in SQL Server to send actions onto the queue and activation procedure picks up the a bunch of actions at a time and runs it against a Badge checking procedure. A Badge Rules Engine for instance.


I would personally have a cron job or similar that processed these on a regular basis. Probably on a "fake cron" flow system, where I'd have an invisible image call and go through the internal "cron stack" to see what needs to be done.

A lot of the badges however, shouldn't need that much to do with them... they should be able to be done with a nicely crafted query


Khan Academy goes into great lengths to award the badges in real-time. They argue it is important to the gamey-feeling of the website. They also have a batch job that periodically reevaluates the data and awards any missed badges.

Ben Kamens, Badges on App Engine: Implementing a real-time.... The blog was published in January 2011, so things migh've changed since.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »