Complete tutorial on Sorted Sets in Redis
Sorted sets are sets with some additional functionalities that is they can be sorted in some ways, allowing more control over the data that is stored in sets. Sorted sets maintain an ordered collection of unique elements, associated with scores (unique no).
Some of the best use cases of it are making a leaderboard, ranking systems, etc. Its actual Data structure is a combination of a list and hash table having characteristics of both sets and sorted lists. These sorted sets contain a unique score that allows the elements to be sorted in ascending or descending order.
Syntax and Commands of Redis Sorted Sets
1. “zadd” command:
Makes a sorted set, and adds elements to it
Syntax: zadd set_name Core_no. Value
Example: zadd Users 1 Amisha 2 Abhinav 3 Vijay
Explanation: We made a sorted set namely “Users” in which we added 3 elements “Amisha”, “Abhinav” and “Vijay”. We assigned these elements a Unique no./Core no. as 1, 2 and 3. Must note that Core no. are not index no.
Output:
2. “zrange” command:
Shows the elements present in sorted set according to the range you specify
Syntax: zrange set_name range_in_integer
Example: zrange Users 0 -1 (Shows the elements of the users_set from 0 to -1)
Explaination: Shows the elements of set “Users”, 0 -1 indicating all elements. If we write 0 1, then it will show “Amisha” and “Abhinav” only.
Output:
3. “zrange” command (zrange command with Core no.):
Syntax: zrange set_name range_integer withscores
Example: zrange Users 0 -1 withscores
Explanation: Shows the elements with core no. also
Output:
4. “zcard” command:
Shows length of sorted set
Syntax: zcard set_name
Example: zcard Users
Explanation: Counts no. of elements of set and displays total no. of elements
Output:
5. “zcount” command:
Counts no of elements
Syntax: zcount set_name range
Example: zcount Users -inf +inf
Explaination: -infinity to +infinity for all values
Output:
6. “zcount” command (Counts elements with their core no.):
Syntax: zcount set_name Core_no.
Example: zcount Users 0 2
Explanation: All elements in Users set with having Core no. 0 to 2 are displayed
Output:
7. “zrem” command:
Removes an element from the set
Syntax: zrem set_name element_name
Example: zrem Users Vijay
Explanation: Vijay will be removed from the Users set
Output:
8. “zrevrange” command:
Reverses the elements in the set
Syntax: zrevrange set_name range withscores
Example: zrevrange Users 0 -1 withscores
Explanation: Reverses the elements of the sorted set.
Output:
9. “zscore” command:
Returns Core no. of a particular element present in that set
Syntax: zscore set_name element_name
Example: zscore Users Abhinav
Explaination: Returns Core no. of element “Abhinav” which is “2”.
Output:
10. “zrevrangebyscore” command:
Reverses the list by core no.
Syntax: zrevrangebyscore set_name range withscores
Example: zrevrangebyscore Users 2 0 withscores
Explaination: Users set was reversed, and is getting displayed with Core no. as well.
Output:
11. “zremrangebyscore” command:
Removes the elements from set in particular Core no. range
Syntax: zremrangebyscore set_name core_no_range
Example: zremrangebyscore Users 0 2
Explaination: Removes the elements by core score from Users set
Output:
12. “zremrangebyrank” command:
Removes the elements by their index/rank
Syntax: zremrangebyrank set_name rank_range
Example: zremrangebyrank Users 0 2
Explaination: Removes the list by rank, hence the set is now empty
Output:
Performance of Sorted Sets in Redis:
Time Complexity of Sorted Sets operations is generally O(log(N)), where N is the number of members, but be careful while using the ZRANGE command with large number of return values because the time complexity of this command is O(log(N) + M), where M is the number of results returned by the command