Skip to content

Redis - Sets

Redis Sets are unordered collections of unique strings. Think of them as a mathematical set: an item can only be in the set once. They are highly efficient for tasks involving uniqueness, membership testing, and set-based logic like unions and intersections.

The key advantages of Sets are that adding, removing, and checking for the existence of an element are all very fast O(1) operations (constant time, regardless of the set size). The maximum number of members in a set is 2^32 - 1 (over 4 billion).

Let’s use a set to store tags for a blog post. Notice how adding mysql a second time has no effect.

> SADD post:1:tags redis
(integer) 1 # Output is 1 because a new element was added.
> SADD post:1:tags mongodb
(integer) 1
> SADD post:1:tags mysql
(integer) 1
> SADD post:1:tags mysql
(integer) 0 # Output is 0 because 'mysql' was already in the set.
> SMEMBERS post:1:tags
1) "redis"
2) "mongodb"
3) "mysql"
(Note: the order is not guaranteed)
  • Tagging: As shown above, associating a set of unique tags with an object (e.g., a blog post, photo, or product).
  • Unique Visitor Tracking: Use SADD to add a user ID or IP address to a daily set (e.g., visitors:2023-10-27). SCARD gives you the total number of unique visitors for that day instantly.
  • Social Graphs: Finding commonalities. If you store the friends of each user in a set, you can use SINTER to find mutual friends between two users.
  • Access Control: Storing the roles of a user in a set. You can quickly check if a user has a specific role using SISMEMBER.

Here are some of the most useful commands for working with Sets.

CommandDescription
SADD key member [member …]Adds one or more members to a set. Returns the number of new members added.
SISMEMBER key memberReturns 1 if the member exists in the set, 0 otherwise. The perfect way to check for existence.
SMEMBERS keyReturns all members of the set. Caution: can be slow on very large sets.
SCARD keyReturns the number of members in a set (its cardinality).
SREM key member [member …]Removes one or more members from a set.
SPOP key [count]Removes and returns one or more random members from a set.
SRANDMEMBER key [count]Returns one or more random members, but does not remove them.
SINTER key [key …]Returns the intersection of multiple sets (elements common to all sets).
SUNION key [key …]Returns the union of multiple sets (all unique elements from all sets).
SDIFF key [key …]Returns the difference between the first set and subsequent sets.
SSCAN key cursor [MATCH …]Safely iterates through the members of a set without blocking the server. The recommended way to read all members of a large set.

While single-element operations are O(1), commands that operate on multiple sets like SINTER, SUNION, and SDIFF have a computational complexity proportional to the total number of elements in the sets. Use them judiciously with very large sets. For retrieving all members, prefer the non-blocking SSCAN over the blocking SMEMBERS in production environments.