title: Dynamic sort with Redis
body: <p>Let's say I have 5 entries in my Redis database:</p>
  
  <ul>
  <li><code>news::id</code>: the ID of the last news;</li>
  <li><code>news::list</code>: a list of all news IDs;</li>
  <li><code>news:n</code> where <em>n</em> is the news ID: a hash containing fields such as <code>title</code>, <code>url</code>, etc.;</li>
  <li><code>news:n:upvotes</code>: a list of all users' IDs who upvoted the news, thus giving the number of upvotes.</li>
  <li><code>news:n:downvotes</code>: a list of all users' IDs who downvoted the news, thus giving the number of downvotes.</li>
  </ul>
  
  <p>Then I have multiple ranking algorithms, where <code>rank =</code>:</p>
  
  <ol>
  <li><code>upvotes_count</code>;</li>
  <li><code>upvotes_count - downvotes_count</code>;</li>
  <li><code>upvotes_count - downvotes_count - age</code>;</li>
  <li><code>upvotes_count / downvotes_count</code>;</li>
  <li><code>age</code>.</li>
  </ol>
  
  <p>Now how do I sort those news according to each of these algorithms?</p>
  
  <p>I thought about computing the different ranks on every votes, but then if I introduce a new algorithm I need to compute the new rank for all the news.</p>
  
  <p><a href="http://redis.io/commands/eval" rel="nofollow"><code>EVAL</code></a> could help but it won't be available until v2.6, which surely I don't want to wait for.</p>
  
  <p>Eventually, I could retrieve all the news and put them in a Python list. But again it translates into a high memory usage, not to mention the fact that Redis stores its data in memory.</p>
  
  <p>So is there a proper way to do this or should I just move to MongoDB?</p>