How to use Redis Sorted Set

Sorted sets are one of the most valuable and advanced data types in Redis. Sorted sets are very similar to sets because they both contain non-repeating string sequences. However, unlike standard sets, sorted sets include a score value associated with each member.

The score value allows them to be sorted in a specific order. Each member of a sorted set must be unique but can share a score value with multiple members.

In this tutorial, you will learn all about the sorted sets in Redis and how you can use them in your databases.

Ensure you have an existing Redis server installed and running to follow this tutorial.

Creating Sorted Sets

To create a sorted set in Redis, use the ZADD command in the Redis CLI. The ZADD command takes three main arguments.

The first one is the name of the key that holds the sorted set.

The second argument holds the member’s score that is being added to the sorted set.

The final and third argument is the actual value of the member in the sorted set.

NOTE: The order of the arguments matters as each represents the values as dictated above.

Take a look at the example shown below.> ZADD captains 1 "Jonathan Archer"

The command above creates a sorted set containing the names of Star Trek captains. In the example above, the value of “Jonathan Archer” has a score of 1.

The command returns an integer value that indicates the total number of members added to the sorted set.

Redis does not restrict you to adding a single member to the set. You can add multiple values. For example:

ZADD captains 2 "Carol Freeman" 4 "Christopher Pike" 1 "Jean-Luc Picard" 3 "Kathryn Janeway."

You will notice two main things in the command above:

  1. First, the score for each member in the sorted does not have to be sequential. You can assign a score of 100 to a member even if the other scores are not set to any value.
  2. Second, a member can hold a similar score as another member in the sorted set.

ZADD command Options.

The ZADD command accepts multiple options to modify its behavior and functionality. These options include:

  1. NX – the NX option tells the ZADD command only to add new members. This option prevents the command from updating existing members in the specified set.
  2. On the other hand, XX – The XX option will only update the existing members and not add any new members to the set.

NOTE: NX and XX options are conflicting. Hence, you can only use one option in a single command.

  1. CH – The CH option tells ZADD to include the number of changed items. By default, ZADD will only return the number of newly added items. The ZADD command will return the number of added items and updated elements using the CH option.
  2. INCR – The INCR option tells the ZADD command to increment a member’s score. If the specified member does not exist in the sorted set, Redis will create it automatically and set the increment as the score.

NOTE: Using the ZINCRBY command instead of ZADD with the INCR option is recommended. They both perform the same function.

Fetch Members from a Sorted Set

To retrieve members of a sorted set in Redis, use the ZRANGE command. The command takes the name of the key and a specific range of the members you want to retrieve. The range values of the sorted set’s members are zero-based indexes. Hence, the first member in the set is at index 0.

For example, to retrieve the members from index 0 to 10, enter the command:> ZRANGE captains 0 10

The command will return the members within that specified range. Example output is as shown below:

1) "Jonathan Archer"
2) "Carol Freeman"
3) "Kathryn Janeway"
4) "Christopher Pike"
5) "Jean-Luc Picard"

In the cases where the sorted set contains members with a similar score value, the members will be sorted in Lexicographic order.

If you want to get the members within the set and their associated scores, you can use the WITHSCORES option.> ZRANGE captains 0 10 WITHSCORES

The example output from the command above is as shown.

1) "Jonathan Archer"
 2) "1"
 3) "Carol Freeman"
 4) "2"
 5) "Kathryn Janeway"
 6) "3"
 7) "Christopher Pike"
 8) "4"
 9) "Jean-Luc Picard"
10) "5"

Suppose you want to get the members in reverse order. Remember that the ZRANGE command returns the members in ascending order only. To get the reverse order, use the ZREVRANGE command.> ZREVRANGE captains 0 10

The command will result in reverse order.

1) "Jean-Luc Picard"
2) "Christopher Pike"
3) "Kathryn Janeway"
4) "Carol Freeman"
5) "Jonathan Archer"

You can also include the WITHOPTIONS option in the ZREVRANGE command.

1) "Jean-Luc Picard"
 2) "5"
 3) "Christopher Pike"
 4) "4"
 5) "Kathryn Janeway"
 6) "3"
 7) "Carol Freeman"
 8) "2"
 9) "Jonathan Archer"
10) "1"

The resulting value will contain the scores in reverse order.

Remove Members from a sorted set

To remove members from a sorted set, use the ZREM command.> ZREM captains "Jonathan Archer"

The command will return an integer value showing the number of items removed from the sorted set.

To remove items by range, use the ZREMBYRANGE command.> ZREMRANGEBYLEX captains [A [Z

The command will remove the values based on their lexico range.

To remove the members on their score range, use the ZREMRANGEBYSCORE command.> ZREMRANGEBYSCORE captains 0 5

Get Info about Sorted Sets

Use the ZCARD command to fetch the number of members in a given sorted set.> ZCARD captains

The return value is an integer showing the number of items in a set.

If you want to get the number of members within a specific range, use the ZCOUNT command.> ZCOUNT captains 0 10

Similarly, the command returns an integer value.

To get the score of a specific member within a sorted set, use the ZSCORE command:> ZSCORE captains "Carol Freeman"

The command will return the score of the member if it exists.


This article discussed how to get started and use Sorted Sets in Ruby. Keep in mind that there are more to sorted sets than discussed in this guide. Consider the documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list