Rank of post in Reddit is based on Up-Votes and Down-Votes and Age of the post. As per the Reddit Source Code on Github , The ranking algorithm was updated on January 12, 2014. This algorithm keeps interesting articles in front page. In this article I am going to explain how to implement this algorithm with SQL

Reddit Ranking Algorithm

cpdef double _hot( long ups, long downs, double date): """The hot formula. Should match the equivalent function in postgres.""" s = score(ups, downs) order = log10( max ( abs (s), 1 )) if s > 0 : sign = 1 elif s < 0 : sign = - 1 else : sign = 0 seconds = date - 1134028003 return round (order + sign * seconds / 45000 , 7 )

score = upvotes - downvotes

date = Age of the post (Submission Time)

Simplified math equation of the algorithm look like this.

f(score,age) = log 10 (score) + (sign)(age / 45000)

sign = sign value is based on score. if score is greater than zero then sign is +1 else sign is -1

Database Design

We need minimum 2 tables to implement this algorithm.

reddit_post - To maintain rank and age

reddit_vote - To keep track of user votes for posts

Create reddit_post table