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

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.

Syntax:
SADD key_name value-1 value-2 value-3
Example:
SADD greeting H i y a

If the set was created successfully, the number of elements in the list will be returned.

Output:
(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.

Example:
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.

Output:
(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.

Syntax:
SMEMBERS key_name

Because the list is unordered, we have no way of referencing a specific value.

Example:
SADD num 1 2 3 4 5

SMEMBERS num
Output:
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.

Syntax:
SCARD key_name
Example:
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.

Output:
(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 .

Syntax:
SDIFF main_set compare_set-1 compare-set-2 ...

As an example, let’s create three sets that have some of the same values.

Example:
SADD num-1 10 20 30 40 50

SADD num-2 40 50 60 70

Now let’s see the unique values.

Example:
SDIFF num-1 num-2
Output:
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.

Syntax:
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’.

Example:
SDIFFSTORE difference num-1 num-2

The result of the operation will be the amount of values that were added to the new set.

Output:
(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.

Example:
SMEMBERS difference
Output:
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 .

Syntax:
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.

Example:
SINTER num-1 num-2
Output:
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.

Syntax:
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’.

Example:
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.

Output:
(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.

Example:
SMEMBERS intersection
Output:
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 .

Syntax:
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.

Example:
SUNION num-1 num-2
Output:
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.

Syntax:
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’.

Example:
SUNIONSTORE union num-1 num-2

If the operation was successful, the number of elements in the new set will be returned.

Output:
(integer) 7

Let’s confirm the results by returning the values in the new set.

Example:
SMEMBERS union
Output:
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.

Syntax:
SREM set_name value(s)_to_remove

We’ll use the num-1 set from the SDIFF section above for the example below.

Example:
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.

Output:
(integer) 3

Let’s confirm the results by returning the values in ‘main-set’.

Example:
SMEMBERS main-set
Output:
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.

Syntax:
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 .

Example:
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.

Output:
(integer) 1

Let’s confirm the result by returning the values in ‘num-1’.

Example:
SMEMBERS num-1
Output:
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.

Example:
SMEMBERS num-2
Output:
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.

Example:
SMOVE compare-set-1 main-set 9999

The value 9999 doesn’t exist so the operation can’t be performed.

Output:
(integer) 0

If we try to move an element that does exist, into a set that doesn’t exist, the set will be created.

Example:
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.

Output:
(integer) 1

Let’s confirm that ‘some-set’ was created and the value moved from ‘num-1’.

Example:
SMEMBERS some-set
Output:
1) "40"