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)

33 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. Understanding pieces of furniture refinishing seems simple and in some cases this can be quite true. Most often it's not necessarily as slice & dried. Products including lacquer support re shell by aerosol gun. Lacquer that can expand in addition to contract and also the wood.Wood Refinishing

    ReplyDelete
  5. The truth is that almost any business in addition to any idea is usually franchised. And this Canadian Franchises also believe in. The concept Franchise from the directory in termed a exclusive privilege awarded. It seemed to be also termed the wave for the future by this futurist. In this environment, Franchising is a means of doing business throughout the marketing of any goods and services.owning a N-Hance Canada Franchise

    ReplyDelete
  6. Your success involving any restaurant is dependent upon well it can be managed. Good operations practices are very important to buying and maintaining customers. While one goes and useful restaurant, the reward has to be great reputation and also a thriving long lasting business.Carpet Cleaning Ottawa

    ReplyDelete
  7. Carpet comes in different components and reductions that suit different wants. Whether you might be shopping to get a kids room or even a little-used conventional area, you will find a sort that works to suit your needs.Carpet Cleaning Hamilton

    ReplyDelete
  8. Speaking of vacuum clearer, in the modern day scenario, just about every household has got this machines. It can help families cleanse their dwellings. It is cleaning the painless and swiftly way. Have you wonder how has it been to live wthout using vacuum clearer Victoria carpet cleaners

    ReplyDelete
  9. If you're an entrepreneur looking to purchase a business or perhaps a franchise you should have an awareness of the actual model along with the options these days. This evaluation will describe some tips of the actual franchise Chem Dry Rug cleaning.Metro ChemDry

    ReplyDelete
  10. Cleaning tile plus grout is definitely never an amazing process and the majority of people wait until people absolutely need to get out its scrub brushes. Whether you will have a tiled bathroom or perhaps backsplash in the kitchen, these grout-sporting accessories should get a fine scrub down one or more times a month to hold their natural beauty. Ajax carpet cleaning

    ReplyDelete
  11. I am sure you see the trend here... Commercial Mortgage Financing can be used in practically any industry for any kind of commercial property. Speak with your Business Finance Broker when you are ready to invest in a Commercial Real estate opportunity. finance types

    ReplyDelete
  12. Make use of a travel agent that provides travel deals for handicapped travelers. Booking plane tickets and rooms in hotels with journey agents that provide travel methods to disabled individuals is advantageous in many ways. They make sure they book for his or her disabled customers airlines as well as hotels which are disabled pleasant. They make an effort to offer providers and suggestions which will ensure their own clients journey comfortably. best adventure travel

    ReplyDelete
  13. One of the biggest competitions of today is staying fit. It seems like people nowadays want to lose weight instantly as if there is no tomorrow or it is as if it is a passport for the general population. Losing weight and staying fit is never easy. health and workout plan

    ReplyDelete
  14. The laws of nature are just as they treat every specie (and not only man) with equality. In a jungle, every specie gets its due share of food, air and water which enable them to live a dignified life without being dependent on any other creation. Nature makes no distinction between one specie, and the other as all species are the children of the same God. law information

    ReplyDelete
  15. I am certain you begin to see the trend right here... Commercial Home loan Financing may be used in virtually any business for any type of commercial home. Speak together with your Business Finance Broker when you're ready to purchase a Commercial Property opportunity. my business plan

    ReplyDelete
  16. You will see all styles and models of fashion blogs as being the internet is stuffed with them. It can take a look at the advantages of wearing lean jeans through boyfriend bluejeans or the best way pink takes the destination of dark colored plus many of the extras. Just like you go internet, you receives your fill of your latest popular trends as a result of fashion blogs. styles beauty tips

    ReplyDelete
  17. Real estate shareholders live in addition to die by means of their chance to add importance. With not any added importance, there usually are no gains. This applies with almost any business, but what makes real estate a really great business as well as a great investment decision, is the volume of ways you can contribute value and benefit from big gains. realestate valley

    ReplyDelete
  18. Health fitness tips also recommend that vices should be stopped. Smoking predisposes one to vascular conditions such as arteriosclerosis and heart diseases. Alcohol consumption need not be eliminated but should be limited to 30 jiggers a day only. Alcohol still has health benefits such as vasodilation. These are just some of what Health fitness tips recommend healthyactivities

    ReplyDelete
  19. Insurance can be synonymous to a lot people expressing risks involving losses expected coming from a supposed car accident. Here, the price of your losses are going to be borne by the many insurers. insurance guider

    ReplyDelete
  20. Discomfort heard this argument intended for spending far more now with quality just to save money in the future. But why not consider the impression your purchase has on the earth, shouldn't shopping ethically participate in the talk too? services expert

    ReplyDelete
  21. Health fitness ideas also suggest that vices ought to be stopped. Smoking predisposes someone to vascular conditions for example arteriosclerosis as well as heart illnesses. Alcohol consumption do not need to be removed but ought to be limited in order to 30 jiggers each day only. Alcohol nevertheless has many benefits such because vasodilation. These are are just some of what Health fitness ideas recommend health improve

    ReplyDelete
  22. Traveling, for individuals with disabilities, is extremely hectic, particularly when they get to the airport terminal. They face the process of obtaining proper info and instructions. This is actually further aggravated through the huge number of individuals traveling as well as long lists. In international airports, there tend to be attendants which are supposed for carrying on the needs of these travelling along with disabilities. safe travel guide

    ReplyDelete
  23. I know you start to see the trend the following... Commercial Mortgage loan Financing can be utilized in almost any market for almost any commercial house. Speak along with your Business Finance Broker if you are ready to buy a Commercial Real-estate opportunity. financial grow

    ReplyDelete
  24. You will find all forms and shapes of fashion blogs as the internet is full of them. It can talk about the main benefit of wearing lanky jeans over boyfriend jeans or how pink takes the place of black plus all the extras. As you go online, you will get your fill of the latest in fashion trends via fashion blogs. news village

    ReplyDelete
  25. Many of us have heard your argument pertaining to spending additional now in quality to avoid wasting money after. But why don't you consider the result your purchase is wearing the entire world, shouldn't shopping ethically join in on the conversation too? shoppin guide

    ReplyDelete
  26. You can find all kinds and forms of fashion blogs because internet is brimming with them. It can mention the luxury of wearing lanky jeans around boyfriend skinny jeans or precisely how pink takes the best place of dark-colored plus the many extras. Because you go on-line, you is certain to get your fill in the latest popular trends through fashion blogs. online fashion shop

    ReplyDelete
  27. Real estate buyers live and also die simply by their power to add benefit. With simply no added benefit, there are usually no income. This does work with virtually any business, but what makes real estate this kind of great business plus a great purchase, is how many ways you can value and make the most of big income. land property

    ReplyDelete
  28. Were you thinking with regards to remodeling your current home? Understand that you may achieve your current goal and not using a giant price. Small changes introduced in nearly all room, including the outdoors on the town can make the many difference. the budget decorator

    ReplyDelete
  29. Insurance is actually synonymous to numerous people discussing risks associated with losses expected from the supposed incident. Here, the expense of the actual losses is going to be borne by all of the insurers. insurance advice

    ReplyDelete
  30. You can see all creates and shapes and sizes of fashion blogs for the internet is jam packed with them. It can converse about the luxury of wearing slim jeans across boyfriend shorts or ways pink takes the position of schokohäutige plus lots of the extras. Simply because go via the internet, you becomes your fill for the latest popular trends with fashion blogs. services dealer

    ReplyDelete
  31. Most likely you view the trend in this article... Commercial House loan Financing can be employed in pretty much any marketplace for virtually any commercial property or home. Speak with all your Business Finance Broker while you are ready to invest in a Commercial Real estate investment opportunity. business terms

    ReplyDelete
  32. Travel's importance is actually underestimated by lots of people. Travel isn't just fun, enjoyable and pleasant. With the current life styles and function conditions, travel is becoming more than a choice. It is a lot more than just having a good time. cheap travel deals

    ReplyDelete
  33. Health fitness suggestions also advise that vices needs to be stopped. Smoking predisposes anyone to vascular conditions for instance arteriosclerosis and also heart conditions. Alcohol consumption will not need to be eradicated but needs to be limited to be able to 30 jiggers per day only. Alcohol nonetheless has health improvements such since vasodilation. These are just some of what Health fitness suggestions recommend ideal health fitness

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts