Complete tutorial on Sets in Redis
In Redis, sets are collections of unique elements, where each element is unique and the order of elements doesn’t matter, as they are unordered. They are handy and useful while working with unique items. Sets are a great way to store and manage various unique items. In Redis, there are several commands for using sets that have their own use cases.
Important Topics for Sets in Redis
- Installation & getting started with Redis
- Syntax and Commands of Redis Sets
- Performance of Redis sets:
- Conclusion
Installation & getting started with Redis
Before, getting started with Sets or any other commands of Redis make sure you have successfully installed Redis on your system/package manager/docker or you won’t be able to use Redis CLI or its commands. We will be using Docker here for the installation of Redis, after you have successfully installed Docker, follow the below steps:-
- Open any terminal or shell (Bash/Zsh/PowerShell) and type “docker run redis” Wait patiently, till it doesn’t display a message like “Ready to accept connections”.
- Minimize this terminal and open another terminal and type “docker ps”, You will see a container of Redis running, copy its container ID.
- Then type this command to activate Redis-CLI “docker container exec -it paste_container_id redis-cli”, an interface like the below one will appear, indicating Redis-CLI is activated, and now you can leverage Redis Commands, to exit Redis-CLI simply type “Ctrl + C”.
Syntax and Commands of Redis Sets
1. “sadd” command:
Creates a set, and adds element to it
Syntax: sadd set_name set_element
Example: sadd SocialMedia Facebook Twitter WhatsApp
Explanation: We created a set namely “SocialMedia” and added 3 unique elements into it as “Facebook”, “Twitter”, “WhatsApp”.
Output:
2. “smembers” command:
Shows all the elements, present in that set
Syntax: smembers set_name
Example: smembers SocialMedia
Explanation: Previously, we stored “Facebook”, “Twitter”, “WhatsApp” in SocialMedia set. Hence, it’s displaying these elements.
Output:
3. “scard” command:
Shows no. of elements, present in that set
Syntax: scard set_name
Example: scard SocialMedia
Explanation: There ae 3 elements in this set, hence its showing 3 as output
Output:
4. “sismember” command:
Checks if that element exists in that set, if yes then returns 1
Syntax: sismember set_name set_element
Example: sismember SocialMedia Twitter
Explanation: Returned 1, “Twitter” is present in SocialMedia set.
Output:
5. “sdiff” command:
Shows difference of two sets by displaying those elements, elements that are in set 1 butt not in set 2
Syntax: sdiff set_name1 set_name2
Example: sdiff FavSub1 FavSub2
Explanation: FavSub1 and FavSub2 are two sets that contain favorite subjects of User1 and User2, respectively. Elements of FavSub1 and FavSub2 are {Computer Science, Data Science} and {Computer Science, Math’s}. The ouput of this command is showing diffrence of two sets as their elements that is the elements that are in FavSub1 but not in FavSub2. Hence, output is Data Science.
Output:
6. “sdiffstore” command:
Stores elements that differ in two sets, elements that are in set1 but not in set2 in a new set
Syntax: sdiffstore set_name3 set_name1 set_name2
Example: sdiffstore NewSet FavSub1 FavSub2
Explanation: As the elements that differ in FavSub1 and FavSub2 is only Data Science. Hence, we made a new set namely “NewSet” that stored this element.
Output:
7. “sinter” command:
Shows elements that are present in both sets that is intersection of 2 given sets
Syntax: sinter set_name1 set_name2
Example: sinter FavSub1 FavSub2
Explanation: Displaying Computer Science, as its the only common element present in both sets
Output:
8. “sinterstore” command:
Stores elements that are present in both sets in a new set
Syntax: sinterstore set_name4 set_name1 set_name2
Example: sinterstore SamePinch FavSub1 FavSub2
Explanation: SamePinch is a new set created that stores the elements that are common in FavSub1 and FavSub2 that is Computer Science
Output:
9. “sunion” command:
Shows all the unique elements that are there overall including those 2 sets as union of sets
Syntax: sunion set_name1 set_name2
Example: sunion FavSub1 FavSub2
Explanation: Displaying Computer Science, Maths and Data Science as these are the unique elements in whole as union of sets
Output:
10. “sunionstore” command:
Stores elements that are union of two sets in a new set
Syntax: sunionstore set_name5 set_name1 set_name2
Example: sunionstore Sub FavSub1 FavSub2
Explanation: Sub is a new set created that stores all the unique elements of whole in these 2 sets
Output:
11. “srem” command:
Removes a particular element from given set
Syntax: srem set_name element
Example: srem Sub Math’s
Explanation: Removed Math’s element from the set “Sub”, hence output after executing srem commands is Data Science and Computer Science.
Output:
Performance of Redis sets:
The Majority of Set operations including finding an element, adding and removing are O(1), i.e constant time-complexity. However, when performing the SMEMBERS command on big sets with hundreds of thousands or more members, you should proceed with caution. This command has an O(n) execution time and returns the complete set in a single response. You can consider the SSCAN, which allows you to iteratively retrieve all members of a set.
Conclusion
Redis offers us various Sets commands, and above-mentioned ones are most important and prominent ones, there will be few commands that you may need to use as per your work requirements, in that case you can refer offical documentation of Redis. But for majority of your work with Sets in Redis, this article in itself is more than enough.
Contact Us