title: Find the top N keys with highest values in Redis
body: <p>I have Redis database with <code>user_id: rating</code> stucture and I need to get the N users with the highest rating (value), like:</p>
  
  <pre><code>u_345: 198
  u_144: 180
  u_267: 179
  </code></pre>
  
  <p>The idea I have: take a list of all the keys, and for each key get its value (<code>db.mget(db.keys())</code>), after sort by value and get first N. Is there a better way?</p>
  
  <p>I use the <a href="https://github.com/andymccurdy/redis-py" rel="nofollow noreferrer">redis-py</a> Python library. But the main thing is to get the right algorithm (or a ready-made solution).</p>