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) = log10(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

create table reddit_post (
 postid varchar(20),
 rank double,
 ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 primary key(postid)
)
postid - Unique ID of the post
rank - Rank of the post
ctime - Age of the post

Create reddit_vote table

create table reddit_vote (
 postid varchar(20),
 user varchar(20),
 vflag integer,
 ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 primary key(postid,user,vflag),
 foreign key(postid) references reddit_post(postid)
)
postid - Unique ID of the post
user - User ID
vflag - vote flag ( +1 for up-vote and -1 for down-vote)

Insert into reddit_post

insert into reddit_post(postid, rank) values('post1',3.1029353) 

Insert into reddit_vote

If user up-voted the post then insert 1 for vflag
If user down-voted the post then insert -1 for vflag
insert into reddit_vote(postid, user, vflag) values('post1','srinivas',1) 
insert into reddit_vote(postid, user, vflag) values('post1','srinivas',-1) 

SQL query to Get age

select ctime from reddit_vote where postid = 'post1'

SQL query to Get score

select sum(vflag) from reddit_vote where postid = 'post1'

Calculate rank

  1. Find age of the post by using above SQL query
  2. Find score of the post by using above SQL query
  3. Calculate sign ( If score is greater than 0 then sign is +1 else sign is -1)
  4. Now calculate the rank of the post  f(score,age) = log10(score) + (sign)(age / 45000)

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! Buy Reddit Upvotes

    ReplyDelete
  3. I really like your take on the issue. I now have a clear idea on what this matter is all about.. posizionamento sui motori di ricerca

    ReplyDelete
  4. Well, you can scrub the grout with white vinegar and an old toothbrush, but it's a job that you'll end up doing every couple of days. (Plus, the acids in the vinegar erode your grout.) Visit this link: https:howtoclean.info

    ReplyDelete
  5. This is cool post and i enjoy to read this post. your blog is fantastic and you have good staff in your blog. nice sharing keep it up. expertise

    ReplyDelete
  6. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks Page Rank

    ReplyDelete
  7. Your blogs are amazing. Keep sharing. I love them Are you also searching for urgent assignment help? we are the best solution for you. We are best known for delivering the best urgent assignment help.

    ReplyDelete

Blogroll

Popular Posts