Redis - Sorted Sets
Sorted sets in Redis are sets but with one exception; each element also contains a score value. That score value is used to sort the set in order by the value (smallest to greatest score). Learn more
Adding Elements to a Sorted Set (ZADD)
The ZADD command can be used to add values to a sorted set. For the arguments you will first need to set the score value and the element after that. Learn more
Time Complexity: O( log(N) ) for each item added, where N is the number of elements in the sorted set.
Example usage:
Viewing Elements in a Sorted Set (ZRANGE)
The ZRANGE command can be used to view the elements of a sorted set. For the command you need to pass the starting and ending element number. Note that the ending number can also be -1 to view all the records. Learn more
Time Complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.
Example usage:
Viewing Amount of Records in a Sorted Set (ZCARD)
The ZCARD command can be used to view the number of records in a sorted set. Learn more
Time Complexity: O(1)
Example usage:
Removing Element from a Sorted Set (ZREM)
The ZREM command can be used to remove a element from a sorted set. Learn more
Time Complexity: O(M*log(N)) with N being the number of elements in the sorted set and M the number of elements to be removed.
Example usage:
Incrementing the score of an Element
The ZINCRBY command can be used to increment the score of an element. Learn more
Time Complexity: O(log(N)) where N is the number of elements in the sorted set.
Example usage:
Getting the Score of an Element
The ZSCORE command can be used to get the score of an element. Learn more
Time Complexity: O(1)
Example usage: