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.
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:
You will notice two main things in the command above:
- 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.
- 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:
- 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.
- 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.
- 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.
- 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:
The command will return the members within that specified range. Example output is as shown below:
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.
The example output from the command above is as shown.
3) "Carol Freeman"
5) "Kathryn Janeway"
7) "Christopher Pike"
9) "Jean-Luc Picard"
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.
The command will result in reverse order.
2) "Christopher Pike"
3) "Kathryn Janeway"
4) "Carol Freeman"
5) "Jonathan Archer"
You can also include the WITHOPTIONS option in the ZREVRANGE command.
3) "Christopher Pike"
5) "Kathryn Janeway"
7) "Carol Freeman"
9) "Jonathan Archer"
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.
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.
The command will remove the values based on their lexico range.
To remove the members on their score range, use the ZREMRANGEBYSCORE command.
Get Info about Sorted Sets
Use the ZCARD command to fetch the number of members in a given sorted set.
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.
Similarly, the command returns an integer value.
To get the score of a specific member within a sorted set, use the ZSCORE command:
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.