how to get a member with maximum (or minimum ) score from redis sorted set given a subset of members?

KCore picture KCore · Nov 16, 2013 · Viewed 12.4k times · Source

I am writing a algo for deducing the user with the least amount of work load. Based on the type/complexity of the tasks being created, i narrow down on the list of the users that are capable of performing it. Now, I need to find the user who has the least number tasks currently assigned to him/her. I am using redis sorted set to store the members/users along the score indicating the number of the tasks assigned to them. Is there a way to get a member with least score given a subset of members?

Answer

Mirek Rusin picture Mirek Rusin · Feb 26, 2014

Member with minimum score:

ZRANGEBYSCORE myset -inf +inf WITHSCORES LIMIT 0 1

Member with maximum score:

ZREVRANGEBYSCORE myset +inf -inf WITHSCORES LIMIT 0 1