Search code examples
iosalgorithmmongodbranking

I don't understand how to use a Ranking Algorithm in my App


So there's an alogrithm that I found called hotrank. Reddit uses it and it can be found here. I'm trying to implement this algorithm into an iOS app. The app is connected to a node js server that stores posts in a mongodb database.

I believe the best place to implement the algorithm would be in the server. My idea of how it would work is when the post is created and when another user likes/dislikes the post, the algorithm runs and updates a score. The score is then saved with the post in the database. Then in the app it displays the posts with the highest score in descending order.

The problem with this, is the algorithm takes in the date as an input (i.e older posts have a lower ranking). So if a user makes a post, that post is assigned a rank and then the post never runs through the algorithm again, the rank would remain the same... regardless of how old it is.

To fix this, would I constantly have to be running linearly through each post and updating the score assigned to it?

I'm fairly new to this side of things and any help would be greatly appreciated.


Solution

  • It depends on a few things like how many posts you expect to have, expected page views and up/down vote activity.

    The algorithm looks fairly simple so if you have a low number of posts you could just calculate this number dynamically on the server every time you retrieve posts and sort by it before returning the results.

    Alternatively you could schedule a task to update this ranking every 10 mins/hour/day/etc and store the value against each post. Then you can retrieve posts ordered by this ranking. This means your ranking may be a little out of date but will be quick to return results that you can cache.

    As you point out the ranking only really changes when you do a new post or up vote/down vote so you could recalculate rankings on these events only. You would need to consider how often this occurs and could potentially schedule an update in 10 mins (if one is not already scheduled, so consecutive up votes would be batched in 10 min lots).