ZREVRANGEBYSCORE

Introduction and Use Case(s)

The ZREVRANGEBYSCORE command in Redis is used to return a range of members in a sorted set, ordered from the highest to the lowest score. This command is particularly useful for scenarios like leaderboard systems, where you want to retrieve top-ranked items within specific score ranges.

Syntax

  1. ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

Parameter Explanations

  • key: The name of the sorted set.
  • max: The maximum score (inclusive) in the range to be queried.
  • min: The minimum score (inclusive) in the range to be queried.
  • WITHSCORES: Optional flag to include the scores of the returned elements.
  • LIMIT offset count: Optional arguments to limit the number of elements returned, starting at the specified offset.

Return Values

The command returns an array of members in the sorted set between the given scores, ordered from highest to lowest score. If WITHSCORES is specified, each member is followed by its score.

Examples:

  • Without WITHSCORES:
  1. dragonfly> ZREVRANGEBYSCORE myzset 3 1
  2. 1) "two"
  3. 2) "one"
  • With WITHSCORES:
  1. dragonfly> ZREVRANGEBYSCORE myzset 3 1 WITHSCORES
  2. 1) "two"
  3. 2) "2"
  4. 3) "one"
  5. 4) "1"

Code Examples

  1. dragonfly> ZADD myzset 1 "one"
  2. (integer) 1
  3. dragonfly> ZADD myzset 2 "two"
  4. (integer) 1
  5. dragonfly> ZADD myzset 3 "three"
  6. (integer) 1
  7. dragonfly> ZREVRANGEBYSCORE myzset 3 1
  8. 1) "three"
  9. 2) "two"
  10. 3) "one"
  11. dragonfly> ZREVRANGEBYSCORE myzset 3 2 WITHSCORES
  12. 1) "three"
  13. 2) "3"
  14. 3) "two"
  15. 4) "2"
  16. dragonfly> ZREVRANGEBYSCORE myzset 3 1 LIMIT 0 2
  17. 1) "three"
  18. 2) "two"

Best Practices

  • When using large sorted sets, consider utilizing the LIMIT option to paginate results and reduce memory overhead.
  • Use WITHSCORES only when necessary to avoid additional processing time if scores are not needed.

Common Mistakes

  • Incorrectly specifying the max and min values can lead to unexpected results. Ensure that max is greater than or equal to min.
  • Not considering the inclusive nature of the max and min parameters might cause confusion in the returned results.

FAQs

How does ZREVRANGEBYSCORE handle ties in scores?

When multiple members have the same score, they are returned in lexicographical order.

Can I use negative infinity and positive infinity as scores?

Yes, you can use +inf and -inf to represent positive and negative infinity respectively when specifying score ranges.