views:

407

answers:

4

Hi,

If you were to build a badge system similiar to how SO does it, would you put the logic/business layer in the database directly (via stored procedure, scheduled sql jobs) or put it in the server side?

From what I can think of, you have to:

  1. list badges that pertain to the current user action
  2. check if the user has a badge already or not
  3. insert badge for user

Potential options

  1. business logic in the web application that calls stored procedures etc.
  2. stored procedures ONLY
  3. sql server job that runs every x minutes
  4. windows service that runs every x minutes

Would a combination of these be required? I think it would since some badges are based on milestones for a given question, maybe a batch job is better?

Update

A system where you can modify the badge system, then re-run the entire badge linking for everyone would be even better. i.e. say you change the logic for some badges, now you have to re-apply it to all the questions/answers/votes/etc.

interesting problem to solve!!

A: 

I would write a stored procedure, since all the information needed resides in the database so this is the most efficient place to access that data.

A tyical rule could be implemented via a single INSERT statement along these lines:

IF eligible for <new badge> THEN
    INSERT INTO user_badges
    SELECT <new_badge>
    WHERE NOT EXISTS (SELECT NULL FROM user_badges
                      WHERE badge = <new_badge>);
END IF;

(I simplify somewhat!)

Tony Andrews
you will be creating too many variables in sql, not to mention tons of control of flow statments that will make it pretty messy don't you think?
Blankman
No, I don't think that. It will be a self-contained stored procedure with 1 parameter - user ID. Mind you, I work in Oracle which has a fantastic stored procedure language, PL/SQL. Maybe in other DBMSs things aren't so good?
Tony Andrews
But you have to track previously assigned badges, so and IF they are not assigned, assign it if the current action necessitates it. So you will have variables to store the current status of a badge. Messy I think!
Blankman
one advantage is that you can modify on the fly w/o recompiling the app.
Blankman
Your comment about "you will have variables", implying variables are a "bad thing". I don't understand why, but perhaps your DBMS handles variables strangely? Which DBMS do you have in mind? To me, variables are an internal part of the stored procedure and are not a problem.
Tony Andrews
Tony, sqlserver. Yeah right they are not a bad thing, I for some reason don't like making a sproc with 30-40+ variables, and hard coding threshhold values inside of a sproc. Do you agree at some level with any of my objections? :)
Blankman
Tony, say a particular action might make some eligible for 15 badges, and say the user has all of the badges already. Your sproc is going to make 15 queries for no reason! not good with load.
Blankman
I don't really agree with your objections, but my experience of SPs seems to be different from yours. As for hard-coding thresholds in a SP, the arguments seem no different from those about hard-coding anywhere. You could hold them in a table instead.
Tony Andrews
"Your sproc is going to make 15 queries for no reason!" - no, with the reason that we need to find out whether the user already has the badge. How else will you know if you don't look? Again, you seem to me to have unfounded fears...
Tony Andrews
Well if you do it in code, you can just pull a single query to load all the badges, then you don't need to check if the person has it for each individual badge in question.
Blankman
Blankman, all your comments seem to be based on the assumption that querying the database from an SP is a BAD thing to do. Now, SQL Server must be very different from Oracle for that to be the case. Look at it the other way, you propose to load in ALL my 57 badges whether relevant or not...
Tony Andrews
Hey don't knock sql server, its just my opinion hehe. Leave sql server out of it, its innocent I tell ya! When you can put somethign in the business logic layer (which you can cache also), why put business logic into the db layer?
Blankman
For me, business layer = Oracle PL/SQL packages (stored procedures). The application layer just collects user input, calls stored procedures, and displays results.
Tony Andrews
+3  A: 

I would put it in the business layer, after all this is business logic that we are talking about. Stored procedures can of course be used to pull back the appropriate data, but I am not a fan of implementing decision logic solely in the database. If nothing else just because it becomes harder and harder to track what is going on when revisiting the code later on.

Jack Ryan
+1. Keep your business logic out of the database unless you absolutely need the performance increase on a particular query/SP. In real life you rarely do; this would be textbook premature optimisation.
bobince
+7  A: 

I would recommend putting all business logic in the business layer. I recommend this for a few reasons:

  • Keep the business logic in one language / place
  • Scalability - You can partition data, implement different caching mechanisms, etc.
  • Seperation of concerns - let your DB do what it does best...store data, let your programming language make decisions on that data.
Rich Kroll
+1  A: 

Personally, I'd leave the database to do the data storage / retrieval and have the logic in a 'business layer'.

Following the success of StackOverflow, I'm quite interested in implementing an achievements system for one of my sites, too - so I've been giving this some thought myself.

I'm currently trying to assess the value of having a lightweight (in terms of processing power) routine which I could run in response to specific user actions (up-votes, new answers, etc.) which could keep most of the badges up-to-date as the site goes.

This would be supported by a more heavyweight routine which could recalculate every badge from scratch. This could be run periodically from a service (or at least a simulated service) to make sure nothing had been missed - but also in response to a change in badge rules.

I guess a big part of the answer to this is going to hinge around the data you're basing the badge awards on. The StackOverflow badges appear to be based on both data (answers, questions, votes, etc.) and events (edits, re-tagging, etc.). So - the badge algorithm must presumably be interrogating some sort of audit log or 'actions' log.

Chris Roberts