Imagine a simple PHP application where an user can post notes and the other users can upvote or downvote them.We want to implement a system where each note has a different rating (normal, good, great ...) based on the number of upvotes and downvotes it has.My question is: When should I handle these rating changes?My first idea was to check every time an user votes on a note if the vote doesn't change it's rating (e.g. from normal to good) but considering that voting on notes will be one of the most frequently used features on our site, it might slow down the site a lot.We could also implement a cron job which fires every, let's say, half an hour and checks if the number of votes on every note corresponds to its rating but I suppose that would be a waste of our resources since most of the notes might not have been voted on during that time interval.
When is the best time to compute rating changes in a PHP application?
design;php
I would suggest, if you have the capability to daemonize processes on your server, that the best way of doing this would be to include in the 'Notes' object itself the business rules of ratings changes, to notify a message queue service (such as rabbitmq) when ratings change, and consume the message queue in the daemonized service in order to notify users that their note has a changed rating. This will allow you to keep individual vote request response times snappy and deal with the notification asynchronously.