I would definitely not calculate their rank every time you show them.
A simple and not very effective solution would be to cache the ranking of messages, and after changing one ranking, you clear or update the cache.
This is not ideal, but it is possible.
Another way would be to do as you were told: calculate and store the ranks in the database (and ideally cache them), and then update these ranks with a cron job every x minutes.
Again, these are the basic approaches to what you want to do. Then you can build them over time.
The algorithm you choose is likely to be very important for your needs.
You also need to determine what traffic will be received on your site, since it will determine what lengths you must go in order to get the right algorithm.
source share