Redis Set Operations

RedisRedisBeginner
Practice Now

Introduction

In this lab, you will explore Redis set operations, focusing on manipulating unordered collections of unique strings. You'll learn how to add elements to a set using the SADD command, perform set unions with SUNION, find common elements using SINTER, and identify differences between sets with SDIFF. Finally, you'll learn how to store the results of set operations for later use.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") redis/RedisGroup -.-> redis/insert_set("Add Set Element") redis/RedisGroup -.-> redis/verify_set("Check Set Membership") redis/RedisGroup -.-> redis/view_set("List Set Members") subgraph Lab Skills redis/access_cli -.-> lab-552104{{"Redis Set Operations"}} redis/insert_set -.-> lab-552104{{"Redis Set Operations"}} redis/verify_set -.-> lab-552104{{"Redis Set Operations"}} redis/view_set -.-> lab-552104{{"Redis Set Operations"}} end

Add Elements to a Set with SADD

In this step, you will learn how to add elements to a Redis set using the SADD command. Sets in Redis are unordered collections of unique strings. This means that each element in a set must be unique, and the order in which elements are added to the set is not preserved.

The SADD command adds one or more members to a set. If the specified member is already a member of the set, it is ignored. If the set does not exist, a new set is created with the specified member(s).

Let's start by connecting to the Redis server. Open your terminal and execute the following command:

redis-cli

This command will open the Redis command-line interface.

Now, let's add some elements to a set named my_set. Execute the following command:

SADD my_set "apple" "banana" "cherry"

This command will add the strings "apple", "banana", and "cherry" to the set my_set. The output will be an integer representing the number of elements that were successfully added to the set. In this case, the output should be 3.

(integer) 3

Now, let's try adding an element that already exists in the set. Execute the following command:

SADD my_set "apple"

This command will attempt to add the string "apple" to the set my_set. However, since "apple" is already a member of the set, it will be ignored. The output will be 0, indicating that no new elements were added.

(integer) 0

To verify that the elements have been added to the set, you can use the SMEMBERS command. Execute the following command:

SMEMBERS my_set

This command will return a list of all the members of the set my_set. The output should be:

1) "cherry"
2) "banana"
3) "apple"

Note that the order of the elements in the output may be different from the order in which they were added. This is because sets are unordered collections.

Finally, exit the Redis CLI:

exit

It's important to exit the redis-cli so that your commands are logged.

You have now successfully added elements to a Redis set using the SADD command. This is a fundamental operation for working with sets in Redis, and it will be used in the following steps to perform more complex operations.

Compute Union with SUNION

In this step, you will learn how to compute the union of multiple Redis sets using the SUNION command. The union of sets is a new set containing all the unique elements present in any of the original sets.

First, let's add some elements to a new set named my_set2. Connect to the Redis server using:

redis-cli

Now, add elements to my_set2:

SADD my_set2 "banana" "grape" "orange"

This command adds "banana", "grape", and "orange" to my_set2. The output should be 3.

(integer) 3

Now that we have two sets, my_set (containing "apple", "banana", "cherry") and my_set2 (containing "banana", "grape", "orange"), we can compute their union using the SUNION command. Execute the following command:

SUNION my_set my_set2

This command will return the union of my_set and my_set2. The output will be a list of all the unique elements present in either set:

1) "orange"
2) "grape"
3) "apple"
4) "banana"
5) "cherry"

Notice that "banana" appears only once in the result, even though it is present in both my_set and my_set2. This is because sets only contain unique elements. The order of elements in the result is not guaranteed.

Finally, exit the Redis CLI:

exit

The SUNION command returns the union of the sets directly to the client. If you want to store the result in a new set, you can use the SUNIONSTORE command, which is not covered in this lab.

In this step, you have learned how to compute the union of multiple Redis sets using the SUNION command. This is a useful operation for combining data from multiple sets into a single set.

Compute Intersection with SINTER

In this step, you will learn how to compute the intersection of multiple Redis sets using the SINTER command. The intersection of sets is a new set containing only the elements that are present in all of the original sets.

Connect to Redis server using:

redis-cli

We already have two sets, my_set (containing "apple", "banana", "cherry") and my_set2 (containing "banana", "grape", "orange"). Let's compute their intersection using the SINTER command. Execute the following command:

SINTER my_set my_set2

This command will return the intersection of my_set and my_set2. The output will be a list of all the elements that are present in both sets:

1) "banana"

Notice that only "banana" is present in both my_set and my_set2. Therefore, the intersection of the two sets is a set containing only "banana".

Let's add a third set named my_set3 and add "banana" and "apple" to it.

SADD my_set3 "banana" "apple"

The output should be 2.

(integer) 2

Now, let's compute the intersection of my_set, my_set2, and my_set3. Execute the following command:

SINTER my_set my_set2 my_set3

This command will return the intersection of all three sets. The output will be:

1) "banana"

Only "banana" is present in all three sets.

Finally, exit the Redis CLI:

exit

The SINTER command returns the intersection of the sets directly to the client. If you want to store the result in a new set, you can use the SINTERSTORE command, which you will learn about later.

In this step, you have learned how to compute the intersection of multiple Redis sets using the SINTER command. This is a useful operation for finding common elements between multiple sets.

Compute Difference with SDIFF

In this step, you will learn how to compute the difference between Redis sets using the SDIFF command. The difference between sets is a new set containing only the elements that are present in the first set, but not in any of the subsequent sets.

Connect to Redis server using:

redis-cli

We have three sets: my_set (containing "apple", "banana", "cherry"), my_set2 (containing "banana", "grape", "orange"), and my_set3 (containing "banana", "apple"). Let's compute the difference between my_set and my_set2 using the SDIFF command. Execute the following command:

SDIFF my_set my_set2

This command will return the difference between my_set and my_set2. The output will be a list of all the elements that are present in my_set but not in my_set2:

1) "apple"
2) "cherry"

"apple" and "cherry" are in my_set but not in my_set2. "banana" is in both sets, so it's not included in the result.

Now, let's compute the difference between my_set and both my_set2 and my_set3. Execute the following command:

SDIFF my_set my_set2 my_set3

This command will return the difference between my_set and the union of my_set2 and my_set3. The output will be:

1) "cherry"

"apple" is in my_set but also in my_set3, so it's excluded. "banana" is in my_set but also in my_set2 and my_set3, so it's excluded. Only "cherry" is exclusively in my_set.

Finally, exit the Redis CLI:

exit

The SDIFF command returns the difference of the sets directly to the client. If you want to store the result in a new set, you can use the SDIFFSTORE command (not covered in this lab).

In this step, you have learned how to compute the difference between Redis sets using the SDIFF command. This is a useful operation for finding elements that are unique to a particular set.

Store Results with SINTERSTORE

In this step, you will learn how to store the result of an intersection operation into a new set using the SINTERSTORE command. This is useful when you want to perform further operations on the intersection of sets without recomputing it every time.

Connect to Redis server using:

redis-cli

We have three sets: my_set (containing "apple", "banana", "cherry"), my_set2 (containing "banana", "grape", "orange"), and my_set3 (containing "banana", "apple"). In the previous step, we computed the intersection of these sets and found that only "banana" was present in all three.

Now, let's store the result of the intersection of my_set, my_set2, and my_set3 into a new set named intersection_set using the SINTERSTORE command. Execute the following command:

SINTERSTORE intersection_set my_set my_set2 my_set3

This command will compute the intersection of my_set, my_set2, and my_set3, and store the result in a new set named intersection_set. The output will be an integer representing the number of elements in the resulting set. In this case, the output should be 1, because the intersection contains only one element ("banana").

(integer) 1

To verify that the intersection has been stored correctly, you can use the SMEMBERS command to view the members of the intersection_set. Execute the following command:

SMEMBERS intersection_set

This command will return a list of all the members of the set intersection_set. The output should be:

1) "banana"

This confirms that the intersection of my_set, my_set2, and my_set3 has been successfully stored in the intersection_set.

Finally, exit the Redis CLI:

exit

In this step, you have learned how to store the result of an intersection operation into a new set using the SINTERSTORE command. This allows you to reuse the result of the intersection in subsequent operations without having to recompute it.

Summary

In this lab, you learned how to add elements to a Redis set using the SADD command. Redis sets are unordered collections of unique strings, meaning each element must be unique and order is not preserved.

The SADD command adds one or more members to a set, ignoring duplicates. The lab demonstrated adding elements to a set, observing the return value indicating the number of added elements, and verifying the set's contents using SMEMBERS. The output order of SMEMBERS may differ from the insertion order due to the unordered nature of sets. You also learned how to compute set unions with SUNION, find common elements using SINTER, identify differences between sets with SDIFF, and store the results of set operations for later use with SINTERSTORE. Remember to exit the redis-cli after each step to ensure your commands are logged.