A set is an unordered collection of unique string members. Unlike lists, a set will never hold two identical values — attempting to add a duplicate is silently ignored. Sets excel at tracking membership: unique visitors, active session IDs, feature flags enabled for an account, or any group where you need fast O(1) existence checks and don’t care about order.
SADD
Adds one or more members to the set stored at key. Members that already belong to the set are ignored. If key does not exist, a new set is created.
Syntax: SADD key member [member ...]
SADD tags "redis" "cache" "database"
# (integer) 3
SADD tags "cache"
# (integer) 0 (already a member, nothing added)
SADD tags "search" "vector"
# (integer) 2
Returns: Integer — the number of members that were newly added (duplicates are not counted).
SREM
Removes one or more members from the set stored at key. Members that are not in the set are silently ignored.
Syntax: SREM key member [member ...]
SADD roles "admin" "editor" "viewer"
SREM roles "editor"
# (integer) 1
SREM roles "superuser"
# (integer) 0 (member did not exist)
Returns: Integer — the number of members that were actually removed.
SMEMBERS
Returns all members of the set stored at key. Because sets are unordered, the order of members in the response is not guaranteed.
Syntax: SMEMBERS key
SADD fruits "apple" "banana" "cherry"
SMEMBERS fruits
# 1) "banana"
# 2) "cherry"
# 3) "apple"
Returns: Array of bulk strings containing every member. Returns an empty array if the key does not exist.
The order of elements returned by SMEMBERS is not deterministic. If you need a stable order, sort the result in your application layer.
SISMEMBER
Tests whether member belongs to the set stored at key.
Syntax: SISMEMBER key member
SADD permissions "read" "write"
SISMEMBER permissions "read"
# (integer) 1
SISMEMBER permissions "delete"
# (integer) 0
Returns: 1 if the member exists in the set, 0 if it does not (or if the key does not exist).
SCARD
Returns the cardinality (number of members) of the set stored at key.
Syntax: SCARD key
SADD online_users "u1" "u2" "u3"
SCARD online_users
# (integer) 3
SCARD nonexistent
# (integer) 0
Returns: Integer — the number of members, or 0 if the key does not exist.
Practical example: tracking unique visitors
Sets are a natural choice for daily unique-visitor counting. Each user ID is recorded at most once per day, and you can query the exact count at any time:
# Record visits for January 1st
SADD visitors:2024-01-01 user1 user2 user3
# (integer) 3
# user2 visits again — duplicate is ignored
SADD visitors:2024-01-01 user2
# (integer) 0
# Check if a specific user visited
SISMEMBER visitors:2024-01-01 user2
# (integer) 1
SISMEMBER visitors:2024-01-01 user99
# (integer) 0
# Exact unique count
SCARD visitors:2024-01-01
# (integer) 3
# See all unique visitors
SMEMBERS visitors:2024-01-01
# 1) "user1"
# 2) "user2"
# 3) "user3"
Combine EXPIRE with your visitor sets to automatically clean up old day-buckets. For example: EXPIRE visitors:2024-01-01 604800 will remove the key after seven days.