Skip to content

Redis Data Type : Set & Sorted Sets


A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:

  • Track unique items (e.g., track all unique IP addresses accessing a given blog post).
  • Represent relations (e.g., the set of all users with a given role).
  • Perform common set operations such as intersection, unions, and differences.

Add elements to a set

You can add single element or multiple elements to a set by using SADD> SADD fruits "banana"
(integer) 1127.0.0.1:6379> SADD fruits "orange"
(integer) 1> SADD fruits "Kiwi" "Grape"
(integer) 2

No Duplicate elements allowed

Sets in Redis do not allow duplicate elements. When you attempt to add a member to a set that already exists, it won’t be added again, and the command will return 0 to indicate that the element was not added.This ensures that each member of a set is unique.

For example, there is a “orange” in the set. What if I try to add another “orange”?>  SADD fruits "orange"
(integer) 0

Retrieve all elements in the set

  • You can retrieve all elements in a set by using SMEMBERS
  • You can verify whether an element exists in a set by using SISMEMBER, 1 indicates that the element exists, 0 indicates that the element does not exist.> SMEMBERS  fruits
1) "banana"
2) "orange"
3) "Kiwi"
4) "Grape"> SISMEMBER fruits "Grape"
(integer) 1> SISMEMBER fruits "Ananas"
(integer) 0

Remove an element from a set

You can remove one or multiple elements from a set by using SREM> SREM fruits "banana"
(integer) 1> SREM fruits "Kiwi" "Grape"
(integer) 2> SMEMBERS fruits
1) "orange"

Compare two sets

We can create two new sets and add some elements. Then we can use SDIFF to compare the two sets and find the difference.> SADD set1 "apple" "banana" "orange"
(integer) 3> SADD set2 "banana" "orange" "grape"
(integer) 3> SADD set3 "apple" "banana" "orange"
(integer) 3> SDIFF set1 set2
1) "apple"> SDIFF set1 set3
(empty array)

The SDIFF command returns an empty array when the difference between all sets is empty. You’ll also note that the order of sets passed to SDIFF matters, since the difference is not commutative.

Sorted Sets

Sorted sets are similar to sets, but each element is associated with a score which is used to order the elements.If more than one string has the same score, the strings are ordered lexicographically.

Let’s create a Sorted set and add some elements by using ZADD

The retrieve method is a bit different than that of set.

# The Sorted Set is called Sset. 1 2 3 4 are the scores of elements.> ZADD Sset 1 "apple"
(integer) 1> ZADD Sset 2 "banana" 3 "Ananas" 4 "kiwi" 4 "orange"
(integer) 4

Retrieve all elements by using ZRANGE> ZRANGE  Sset 0 -1
1) "apple"
2) "banana"
3) "Ananas"
4) "kiwi"
5) "orange"

Retrieve elements with scores by using ZRANGEBYSCORE

Notice that “kiwi” and “orange” have the same score 4 but “kiwi”‘s position is in front of “orange”. Because “K” is in front of “O” in lexicographical order.> ZRANGE Sset 0 -1 WITHSCORES
 1) "apple"
 2) "1"
 3) "banana"
 4) "2"
 5) "Ananas"
 6) "3"
 7) "kiwi"
 8) "4"
 9) "orange"
10) "4"

Increment score of an element in a Sorted Set

For example, I want to change the orange’s score from 4 to 5.> ZINCRBY Sset 1 "orange"

#verify the result> ZRANGE Sset 0 -1 WITHSCORES
 1) "apple"
 2) "1"
 3) "banana"
 4) "2"
 5) "Ananas"
 6) "3"
 7) "kiwi"
 8) "4"
 9) "orange"
10) "5"

Rank the element in a Sorted Set

ZRANK retrieves the rank of a member in a sorted set. The rank of an element in a sorted set is determined by the score associated with the element, with elements sorted from the smallest score to the largest score.

If the element does not exist in the sorted set, it will return nil.

Remember the first member has a rank of 0, the second member has a rank of 1, and so on.> ZRANK Sset "apple"
(integer) 0> ZRANK Sset  "banana"
(integer) 1> ZRANK Sset "watermelon"