“Redis sets are unordered collections of strings that contain no duplicates. These are very much similar to the Java HashSets, Python Sets, etc. Most importantly, fundamental operations like adding, removing, and checking the existence of a member are highly efficient because they operate in O(1) time complexity.”
Members of a Set
As mentioned, Redis sets contain unique string items. The already existing items will not be accepted by Redis sets and will be ignored. Furthermore, one single set can hold up to 4 billion unique strings.
The Redis set’s nature of holding unique members comes in handy in several real-world use cases.
- Ability to perform standard set operations like Union, Intersection, and Difference.
- Tracking unique visitors to a website
- Represent real-world entity relations
The SCARD Command
The SCARD command, short for the Set Cardinality, returns the number of members in a set stored at a given key. It operates in O(1) time complexity which means the time the SCARD command takes to execute doesn’t rely on the number of members in the given set. It always takes a constant time.
The SCARD command has a very simple syntax, as shown in the following.
set_key: The key of the Redis set
This command returns an integer value which is the number of members in the set.
Use Case – Count the Unique Visitors to a Website
Assume a “YummyPizza” pizza company maintains a website called yummypizza.com where people can order pizza online. To keep track of their sales and customer base, they maintain a Redis database to store all the unique visitors to the website each month.
Whenever a user visits the YummyPizza website, the user id should be added to the Redis database. Also, the same user shouldn’t be added to the database as well. So, the ideal data structure is the Redis set, where sets only store unique members.
Let’s assume that five users have visited the website, and these members are added to the Redis database, as shown in the following.
As expected, the integer 5 has been returned, which means the five members are added to the set stored at key “YummyPizzaVisitors:October.”
At the end of the day, company admins need to check the total number of unique visitors to the website. So, the Set cardinality needs to be calculated. Fortunately, the previously discussed SCARD command comes in handy in this type of scenario.
Let’s execute the SCARD command on the set stored at key “YummyPizzaVisitors:October.”
The output is 5, which means five unique members are in the specified set. This command executes too fast. It doesn’t matter five members or 50000 members; the execution time will be constant.
Assume that the specified set key doesn’t exist in the Redis database. Then, the output will be 0, as shown in the following example. In this case, we are going to specify a key that is not in the database.
To summarize, Redis set is an ideal candidate for storing unique strings. As discussed, the most important thing about Redis set is that most of the associated set operations take a constant time to execute. The SCARD command is one of the most used set commands to calculate the total number of set members for a given set stored at a specified key. No matter how many set members are available, this command takes constant time to provide the output. As shown in the last example, if the set key doesn’t exist, then the output will be 0.