What is the recommended way to build functionality similar to Stackoverflow's "Inbox"?
Asked Answered
P

5

12

I have an asp.net-mvc website and people manage a list of projects. Based on some algorithm, I can tell if a project is out of date. When a user logs in, i want it to show the number of stale projects (similar to when i see a number of updates in the inbox).

The algorithm to calculate stale projects is kind of slow so if everytime a user logs in, i have to:

  1. Run a query for all project where they are the owner
  2. Run the IsStale() algorithm
  3. Display the count where IsStale = true

My guess is that will be real slow. Also, on everything project write, i would have to recalculate the above to see if changed.

Another idea i had was to create a table and run a job everything minutes to calculate stale projects and store the latest count in this metrics table. Then just query that when users log in. The issue there is I still have to keep that table in sync and if it only recalcs once every minute, if people update projects, it won't change the value until after a minute.

Any idea for a fast, scalable way to support this inbox concept to alert users of number of items to review ??

Puccini answered 12/3, 2012 at 4:41 Comment(0)
E
10

The first step is always proper requirement analysis. Let's assume I'm a Project Manager. I log in to the system and it displays my only project as on time. A developer comes to my office an tells me there is a delay in his activity. I select the developer's activity and change its duration. The system still displays my project as on time, so I happily leave work.

How do you think I would feel if I receive a phone call at 3:00 AM from the client asking me for an explanation of why the project is no longer on time? Obviously, quite surprised, because the system didn't warn me in any way. Why did that happen? Because I had to wait 30 seconds (why not only 1 second?) for the next run of a scheduled job to update the project status.

That just can't be a solution. A warning must be sent immediately to the user, even if it takes 30 seconds to run the IsStale() process. Show the user a loading... image or anything else, but make sure the user has accurate data.

Now, regarding the implementation, nothing can be done to run away from the previous issue: you will have to run that process when something that affects some due date changes. However, what you can do is not unnecessarily run that process. For example, you mentioned that you could run it whenever the user logs in. What if 2 or more users log in and see the same project and don't change anything? It would be unnecessary to run the process twice.

Whatsmore, if you make sure the process is run when the user updates the project, you won't need to run the process at any other time. In conclusion, this schema has the following advantages and disadvantages compared to the "polling" solution:

Advantages

  • No scheduled job
  • No unneeded process runs (this is arguable because you could set a dirty flag on the project and only run it if it is true)
  • No unneeded queries of the dirty value
  • The user will always be informed of the current and real state of the project (which is by far, the most important item to address in any solution provided)

Disadvantages

  • If a user updates a project and then upates it again in a matter of seconds the process would be run twice (in the polling schema the process might not even be run once in that period, depending on the frequency it has been scheduled)
  • The user who updates the project will have to wait for the process to finish

Changing to how you implement the notification system in a similar way to StackOverflow, that's quite a different question. I guess you have a many-to-many relationship with users and projects. The simplest solution would be adding a single attribute to the relationship between those entities (the middle table):

Cardinalities: A user has many projects. A project has many users

That way when you run the process you should update each user's Has_pending_notifications with the new result. For example, if a user updates a project and it is no longer on time then you should set to true all users Has_pending_notifications field so that they're aware of the situation. Similarly, set it to false when the project is on time (I understand you just want to make sure the notifications are displayed when the project is no longer on time).

Taking StackOverflow's example, when a user reads a notification you should set the flag to false. Make sure you don't use timestamps to guess if a user has read a notification: logging in doesn't mean reading notifications.

Finally, if the notification itself is complex enough, you can move it away from the relationship between users and projects and go for something like this:

Cardinalities: A user has many projects. A project has many users. A user has many notifications. A notifications has one user. A project has many notifications. A notification has one project.

I hope something I've said has made sense, or give you some other better idea :)

Episcopalism answered 15/3, 2012 at 22:58 Comment(0)
L
6

You can do as follows:

  1. To each user record add a datetime field sayng the last time the slow computation was done. Call it LastDate.
  2. To each project add a boolean to say if it has to be listed. Call it: Selected
  3. When you run the Slow procedure set you update the Selected fileds
  4. Now when the user logs if LastDate is enough close to now you use the results of the last slow computation and just take all project with Selected true. Otherwise yourun again the slow computation. The above procedure is optimal, becuase it re-compute the slow procedure ONLY IF ACTUALLY NEEDED, while running a procedure at fixed intervals of time...has the risk of wasting time because maybe the user will neber use the result of a computation.
Lavalava answered 14/3, 2012 at 19:46 Comment(0)
M
3

Make a field "stale". Run a SQL statement that updates stale=1 with all records where stale=0 AND (that algorithm returns true). Then run a SQL statement that selects all records where stale=1.

The reason this will work fast is because SQL parsers, like PHP, shouldn't do the second half of the AND statement if the first half returns true, making it a very fast run through the whole list, checking all the records, trying to make them stale IF NOT already stale. If it's already stale, the algorithm won't be executed, saving you time. If it's not, the algorithm will be run to see if it's become stale, and then stale will be set to 1.

The second query then just returns all the stale records where stale=1.

Mushro answered 12/3, 2012 at 4:48 Comment(0)
U
0

You can do this:

In the database change the timestamp every time a project is accessed by the user. When the user logs in, pull all their projects. Check the timestamp and compare it with with today's date, if it's older than n-days, add it to the stale list. I don't believe that comparing dates will result in any slow logic.

Unable answered 12/3, 2012 at 4:48 Comment(5)
its the IsStale() method that is slowPuccini
Time stamp comparison shouldn't be slow but your other option is to add this logic on the database side. If you can, create a stored procedure that does the comparison and returns the result as an extra column.Unable
IsStale() is NOT just comparing timestamp. its a more complicated algorith to see if the data is out of date based on a number of factorsPuccini
this is not the point of the question anyway . just assume for now to indicate a notification is an expensive process . .Puccini
If something is an expensive process on the application side, then it makes sense to move the processing to the database. As I mentioned earlier, if you can, use a stored procedure or inline sql to return an extra bit column that returns 0 or 1 depending on any number of those other extra factors. If you don't want to go the database route then you can post the code to your OP and I or someone else can go line-by-line to tweak it.Unable
B
0

I think the fundamental questions need to be resolved before you think about databases and code. The primary of these is: "Why is IsStale() slow?"

From comments elsewhere it is clear that the concept that this is slow is non-negotiable. Is this computation out of your hands? Are the results resistant to caching? What level of change triggers the re-computation.

Having written scheduling systems in the past, there are two types of changes: those that can happen within the slack and those that cause cascading schedule changes. Likewise, there are two types of rebuilds: total and local. Total rebuilds are obvious; local rebuilds try to minimize "damage" to other scheduled resources.

Here is the crux of the matter: if you have total rebuild on every update, you could be looking at 30 minute lags from the time of the change to the time that the schedule is stable. (I'm basing this on my experience with an ERP system's rebuild time with a very complex workload).

If the reality of your system is that such tasks take 30 minutes, having a design goal of instant gratification for your users is contrary to the ground truth of the matter. However, you may be able to detect schedule inconsistency far faster than the rebuild. In that case you could show the user "schedule has been overrun, recomputing new end times" or something similar... but I suspect that if you have a lot of schedule changes being entered by different users at the same time the system would degrade into one continuous display of that notice. However, you at least gain the advantage that you could batch changes happening over a period of time for the next rebuild.

It is for this reason that most of the scheduling problems I have seen don't actually do real time re-computations. In the context of the ERP situation there is a schedule master who is responsible for the scheduling of the shop floor and any changes get funneled through them. The "master" schedule was regenerated prior to each shift (shifts were 12 hours, so twice a day) and during the shift delays were worked in via "local" modifications that did not shuffle the master schedule until the next 12 hour block.

In a much simpler situation (software design) the schedule was updated once a day in response to the day's progress reporting. Bad news was delivered during the next morning's scrum, along with the updated schedule.

Making a long story short, I'm thinking that perhaps this is an "unask the question" moment, where the assumption needs to be challenged. If the re-computation is large enough that continuous updates are impractical, then aligning expectations with reality is in order. Either the algorithm needs work (optimizing for local changes), the hardware farm needs expansion or the timing of expectations of "truth" needs to be recalibrated.

A more refined answer would frankly require more details than "just assume an expensive process" because the proper points of attack on that process are impossible to know.

Bagger answered 21/3, 2012 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.