views:

53

answers:

4

This is not SO Meta question. I am using SO only as example.

In StackoverFlow each answer, each comment, each question, each vote has a effect which produces a badge at some point of time. I mean after every action a list of queries are tested.

E.g. If Mr.A up votes Mr.B Answer. So we have to check is this Mr.B's answer upvoted 100 times so give the Mr.B a badge , Has Mr.A upvoted 100th time so give him a badge.

It means I have to run at least 100 queries/IfElse for each action.

Now my real life example is I have an application where I receive online data from an attendance machine. When a user shows his card to machine. I receive this and store it as a record. Now based on this record I have multiple calculations.i.e Is he late. Is he late for continues 3 days. Is he in a right shift(Day shift/Night Shift). Is today holiday. Is this a overtime. Is he early.......etc.,etc.,etc.

What is the best strategy for this kind of requirements.

Update: Can SO team guide us on this?

A: 

Couldn't you just use "flags" (other tables, other columns, whatever) to indicate when those special cases occur? That way you would only have to do one lookup (per special case) than a ton of lookups and/or joins. You could record the changes (third day late, etc.) on insert.

Michael Todd
+3  A: 

You use queues and workflows. This way you decouple the moment of the update from the actual notifications, allowing the system to scale. Tighly coupled, trigger based or similar solutions cannot scale, as each update has to wait for all the interested parties to react to the notification. The design of processing engine using workflows allows to easily add steps and notifications consumers by changing the data, w/o changing the schema.

For instance see how MSDN uses queues to handle similar problems with MSDN content: Building the MSDN Aggregation System.

Remus Rusanu
Queues allow for neat and flexibale architectures. It sounds like the OP should really broadcast events, and have a series of processes check for particular conditions. (e.g. a LatenessEngine, an overtimeEngine etc.). The big problem in such systems is how to cope with non-events e.g. the guy doesnt turn up to work when hes supposed to. Either you revert to big scheduled sql searches, or, have a process fire of psuedo events.
James Anderson
Publish-subscribe is the messaging equivalent of broadcast and is easily implemented on top of ordinary messaging. Push of events vs. pull for changes usually has a tipping point: on low throughput is more efficient to push, on high throughput is better to pull.
Remus Rusanu
Actually non-events are quite easy, you place timers in the system. A system like the one I quoted in the MSDN link would use conversation timers, which are an excellent match. They scale, they're persistent and they're transitional: http://msdn.microsoft.com/en-us/library/ms187804.aspx.
Remus Rusanu
'quite-easy' as in 'possible without excessive overhead' ;)
Remus Rusanu
typo: 'transitional' -> 'transactional'
Remus Rusanu
@Remus. Thanks +1 for the same. Close enough. I will wait for more answers(for more specific implementations).
Sachin
A: 

Also, what to check depends on a threshold.

e.g. Is a person absent from last 3 days? That check is required only when the person is absent for 2 days.

I mean - you need not check everything, everytime.
Also, how much of info needs to be updated immediately? SO doesn't update things real time.

shahkalpesh
A: 

May be you must use two databases with online replication between them - one for getting realtime data and nothing else, in second you may use hard calculations (for example calculate all latings every 10 minutes or by requsts). Locate this databases on different servers.

Alexey Sviridov