<p>How to implement a website with a recommendation system similar to stackoverflow/digg/reddit? I.e., users submit content and the website needs to calculate some sort of "hotness" according to how popular the item is. The flow is as follows:</p>

<ul>
<li>Users submit content</li>
<li>Other users view and vote on the content (assume 90% of the users only views content and 10% actively votes up or down on content)</li>
<li>New content is continuously submitted</li>
</ul>

<p>How do I implement an algorithm that calculates the "hotness" of a submitted item, preferably in real-time? Are there any best-practices or design patterns?</p>

<p>I would assume that the algorithm takes the following into consideration:</p>

<ul>
<li>When an item was submitted</li>
<li>When each vote was cast</li>
<li>When the item was viewed</li>
</ul>

<p>E.g. an item that gets a constant trickle of votes would stay somewhat "hot" constantly while an item that receives a burst of votes when it is first submitted will jump to the top of the "hotness"-list but then fall down as the votes stop coming in.</p>

<p>(I am using a MySQL+PHP but I am interested in general design patterns).</p>

[{'answer_id': 71970, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 4, 'selected': True, 'text': '<p>You could use something similar to the <a href="http://redflavor.com/reddit.cf.algorithm.png" rel="noreferrer">Reddit algorithm</a> - the basic principle of which is you compute a value for a post based on the time it was posted and the score. What\'s neat about the Reddit algorithm is that you only need recompute the value when the score of a post changes. When you want to display your front page, you just get the top n posts from your database based on that score. As time goes on the scores will naturally increase, so you don\'t have to do any special processing to remove items from the front page.</p>\n'}, {'answer_id': 103737, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 2, 'selected': False, 'text': "<p>On my own site, I assign each entry a unique integer from a monotonically increasing series (newer posts get higher numbers). Each up vote increases the number by one, and each down vote decreases it by one (you can tweak these values, of course). Then, simply sort by the number to display the 'hottest' entries.</p>\n"}, {'answer_id': 103877, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>I developed an social bookmarking site, <a href="http://www.sites-favoritos.com" rel="nofollow noreferrer">Sites Favoritos</a>, and used a complex algoritm:</p>\n\n<ol>\n<li>First, the votes are finite, an user only have a limited number of votes, and the number of votes depends on the user points. To earn points each user must add links that get positive votes.</li>\n<li>Then, users can vote -3,-2,-1,1,2 or 3 votes for each link. As the votes are limited, each user will vote only on those links that they like.</li>\n<li>To prevent user to vote only on links for the same user, creating support groups, the points each vote adds to the link depends on a racio between total votes and votes to links of the owner of the voted link. If you always vote on the same users links, your votes will lose value.</li>\n<li>Votes lose value with time.</li>\n<li>New links from users who don\'t have points (new users) will have a starting 0 points. New links from older users will have points depending on their points. Ranging from +3 to -infinite. Links from users with negative points will have negative starting points, links from users with positive points will have positive starting points.</li>\n</ol>\n\n<p>Users will get random points when their links are voted. Positive votes give positive points, negative votes for negative points.</p>\n'}, {'answer_id': 2617419, 'author': 'Rich Apodaca', 'author_id': 54426, 'author_profile': 'https://Stackoverflow.com/users/54426', 'pm_score': 1, 'selected': False, 'text': '<p>Paul Graham wrote an essay on what he learned in <a href="http://www.paulgraham.com/hackernews.html" rel="nofollow noreferrer">developing Hacker News</a>. The emphasis is more on the people/interactions he was trying to attract/create than on the algorithm per se, but still well worth a read. For example, he discusses the different outcomes when stories bubble up from the bottom (HN) versus exploding to the top (Digg) of the front page. (Although from what I\'ve seen of HN, it looks like stories explode to the top there also).</p>\n\n<p>He offers this quote:</p>\n\n<blockquote>\n  <p>The key to performance is elegance, not battalions of special cases.</p>\n</blockquote>\n\n<p>which in light of the <a href="http://www.seomoz.org/blog/reddit-stumbleupon-delicious-and-hacker-news-algorithms-exposed" rel="nofollow noreferrer">purported algorithm</a> for generating the HN front page:</p>\n\n<blockquote>\n  <p>(p - 1) / (t + 2)^1.5</p>\n  \n  <p>where</p>\n  \n  <p>p = an article\'s points and</p>\n  \n  <p>t = time from submission of article</p>\n</blockquote>\n\n<p>might be a good starting point.</p>\n'}, {'answer_id': 13159442, 'author': 'Chris Broski', 'author_id': 468111, 'author_profile': 'https://Stackoverflow.com/users/468111', 'pm_score': 1, 'selected': False, 'text': "<p>I implemented an SQL version of Reddit's ranking algorithm for a video aggregator like so:</p>\n\n<pre><code>SELECT id, title\nFROM videos\nORDER BY \n    LOG10(ABS(cached_votes_total) + 1) * SIGN(cached_votes_total)   \n    + (UNIX_TIMESTAMP(created_at) / 300000) DESC\nLIMIT 50\n</code></pre>\n\n<p>*cached_votes_total* is updated by a trigger whenever a new vote is cast. It runs fast enough on our current site, but I am planning on adding a ranking value column and updating it with the same trigger as the *cached_votes_total* column. After that optimization, it should be fast enough for most any size site.</p>\n"}]
['https://Stackoverflow.com/questions/71920', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/103373/']