Redis Set Data Structure Tutorial
In this Redis tutorial we learn about an unordered array-like data structure called a Set.
We learn how to create a Set and how to use the various Set commands that Redis provides for us.
- Redis Set Data Structure
- How to add items to a Set
- How to list all the values (members) in a Set
- How to count the amount of members in a Set
- How to show the difference in values between multiple Sets
- How to store the difference in values between multiple Sets
- How to find similar values between multiple Sets
- How to store similar values between multiple Sets
- How to combine multiple Sets
- How to combine and store multiple Sets
- How to remove one or more values from a Set
- How to move values from one Set to another
Redis Set Data Structure
A Set in Redis is similar to a list except that it’s an unordered collection and each value in a set must be unique.
We can add, remove and test for the existence of members in the set in Order(1) and in constant time, regardless of how many elements are inside the set.
How to add items to a Set
To insert values into a set we use the SADD command. The first argument is the key name, followed by the values we want to add to the set.
If the key does not exist, it will be created automatically.
SADD key_name value-1 value-2 value-3
SADD greeting H i y a
If the set was created successfully, the number of elements in the list will be returned.
(integer) 4
Remember that each value in a set must be unique. If we try to add the same value twice, it simply won’t be inserted.
SADD greet H e l l o
Here we try to add five characters to the set, two of which are the same. The output shows that only 4 characters was added.
(integer) 4
Redis ignored the second ‘l’ character and didn’t add it to the set.
How to list all the values (members) in a Set
To list all the values in a set, we use the SMEMBERS command with the key name as the argument.
SMEMBERS key_name
Because the list is unordered, we have no way of referencing a specific value.
SADD num 1 2 3 4 5
SMEMBERS num
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
How to count the amount of members in a Set
To return the amount of elements inside the list, we use the SCARD command with the key name as its argument.
SCARD key_name
SADD halfabet a b c d e f g h i j k l m
SCARD halfabet
From the output we can see that there’s 13 elements in the set.
(integer) 13
How to show the difference in values between multiple Sets
If we have more than one set with the same values, we can show which values are unique between them with the SDIFF command.
The first argument is the set we want to compare with all the successive sets.
Note that the SDIFF command doesn’t store the result of the evaluation, it only performs the operation and returns the result. To store the result, see the next section on SDIFFSTORE .
SDIFF main_set compare_set-1 compare-set-2 ...
As an example, let’s create three sets that have some of the same values.
SADD num-1 10 20 30 40 50
SADD num-2 40 50 60 70
Now let’s see the unique values.
SDIFF num-1 num-2
1) "10"
2) "20"
3) "30"
We have three unique values in the num-1 set. The values 40 and 50 are present in both sets, so the result will be what’s left in the num-1 set.
How to store the difference in values between multiple Sets
We’re not limited to only checking the differences between multiple sets, we can also store the results of the evaluation in a new set.
To do this, we use the SDIFFSTORE command. The first argument is the name of the new set that will contain the result of the difference check and the second and subsequent arguments are the sets we want to compare.
SDIFFSTORE new_result_set main_set compare_set-1 compare_set-2
We’ll use the num-1 and num-2 sets from the SDIFF section above and store the results in a new set called ‘difference’.
SDIFFSTORE difference num-1 num-2
The result of the operation will be the amount of values that were added to the new set.
(integer) 3
If we check the members of the new set, it will be the same result as in the previous section but now the data is stored and we can do something with it.
SMEMBERS difference
1) "10"
2) "20"
3) "30"
How to find similar values between multiple Sets
To check if multipe sets have the same values, we use intersection. Specifically, the SINTER command.
Like the SDIFF command, the first argument is the set we want to compare with all the successive sets.
Note that the SINTER command doesn’t store the result of the intersection, it only performs the operation and returns the result. To store the result, see the next section on SINTERSTORE .
SINTER main_set compare_set-1 compare-set-2
We’ll use the num-1 and num-2 sets from the SDIFF section above and see which values are the same.
SINTER num-1 num-2
1) "40"
2) "50"
Both sets contain the values 40 and 50.
How to store similar values between multiple Sets
Evaluating for the same values between sets isn’t very useful unless we can store and use those values.
Redis allows us to do just that with the SINTERSTORE command. It will evaluate the sets and create a new set with the results.
The first argument is the name of the new set, followed by the main set and any subsequent sets we want to compare to the main.
SINTERSTORE new_result_set main_set compare_set-1 compare_set-2
We’ll use the num-1 and num-2 sets from the SDIFF section above and store the results in a new set called ‘intersection’.
SINTERSTORE intersection num-1 num-2
If the operation was successful, Redis will return the amount of values found and stored in the new set.
(integer) 2
Because only 40 and 50 are present in both sets, the new result set only contains those two values.
Let’s confirm the results and return the values in the result set with the SMEMBERS command.
SMEMBERS intersection
1) "40"
2) "50"
How to combine multiple Sets
Redis allows us to combine multiple sets into one with the SUNION command. Values that are the same across sets will only be added once.
The arguments are the sets we want to combine.
Note that the SUNION command doesn’t store the result of the union, it only performs the operation and returns the result. To store the result, see the next section on SUNIONSTORE .
SUNION set_name-1 set_name-2
Once again we’ll use the num-1 and num-2 sets from the SDIFF section above and combine the two sets.
SUNION num-1 num-2
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
The operation was a success and the two sets were combined. The values 40 and 50 existed in both sets but were only added once in the union.
How to combine and store multiple Sets
As with the difference and intersection, Redis allows us to store the result of a union in a new seperate set with the SUNIONSTORE command.
The first argument is the name of the new result set, followed by the sets we want to combine.
SINTERSTORE new_result_set union_set-1 union_set-2
We’ll use the num-1 and num-2 sets from the SDIFF section above and store the results in a set called ‘union’.
SUNIONSTORE union num-1 num-2
If the operation was successful, the number of elements in the new set will be returned.
(integer) 7
Let’s confirm the results by returning the values in the new set.
SMEMBERS union
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
The two sets were combined and any doubles that existed were discarded.
How to remove one or more values from a Set
We know that a set is an unordered list and doesn’t have any indices or keys that we can use to reference a value. When we need to remove one or more values from a set, we need to specify the values themselves with the SREM command.
The first argument is the set we want to perform the operation on. The second and subsequent arguments are the values we want to remove from the set.
SREM set_name value(s)_to_remove
We’ll use the num-1 set from the SDIFF section above for the example below.
SREM num-1 10 20 30
The operation above will attempt to remove the values 10, 20 and 30 from the set. If the operation was successful, the number of removed values will be returned.
(integer) 3
Let’s confirm the results by returning the values in ‘main-set’.
SMEMBERS main-set
1) "40"
2) "50"
How to move values from one Set to another
Sometimes you will need to move a value from one set to another. Redis makes moving values across sets easy with the SMOVE command.
We specify the source and destination sets as the first two arguments, followed by the value that we want to move.
SMOVE source_set destination_set value_to_be_moved
We’ll use the num-2 set as the source and the num-1 set as the destination from the SDIFF section above .
SMOVE num-2 num-1 60
We want to move the value 60 from num-2 into num-1. If the operation has been successful, the number of values moved will be returned.
(integer) 1
Let’s confirm the result by returning the values in ‘num-1’.
SMEMBERS num-1
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
The value 60 was added to ‘num-1’. Now let’s confirm that it was moved from the source set and not just copied over.
SMEMBERS num-2
1) "40"
2) "50"
3) "70"
Good, the value 60 doesn’t exist in the source set anymore, confirming it was moved.
If we try to move an element that doesn’t exist, nothing happens and 0 is returned.
SMOVE compare-set-1 main-set 9999
The value 9999 doesn’t exist so the operation can’t be performed.
(integer) 0
If we try to move an element that does exist, into a set that doesn’t exist, the set will be created.
SMOVE num-1 some-set 40
The value 40 exists in ‘num-1’ but ‘some-set’ doesn’t exist. It will be created and should return the number of elements that was moved to it.
(integer) 1
Let’s confirm that ‘some-set’ was created and the value moved from ‘num-1’.
SMEMBERS some-set
1) "40"