Redis Sets (Part 1)
A set is an unordered collection of unique strings. This lesson covers basic set operations.
What is a Set?
A set is similar to:
- Python's set
- JavaScript's Set
- Java's HashSet
Set features:
- Unordered: elements have no specific order
- Unique: automatic deduplication
- Supports set operations: intersection, union, difference
Basic Commands
SADD: Add Elements
REDIS
# Add a single element
SADD myset "a"
(integer) 1
# Add multiple elements
SADD myset "b" "c" "d"
(integer) 3
# Add an existing element (returns 0, no duplicate)
SADD myset "a"
(integer) 0
SMEMBERS: Get All Elements
REDIS
SMEMBERS myset
1) "a"
2) "b"
3) "c"
4) "d"
⚠️ Note: SMEMBERS returns all elements at once. For large datasets, this may block. Use SSCAN in production.
SISMEMBER: Check if Element Exists
REDIS
SISMEMBER myset "a"
(integer) 1
SISMEMBER myset "z"
(integer) 0
SCARD: Get Set Size
REDIS
SCARD myset
(integer) 4
SREM: Remove Elements
REDIS
SREM myset "a"
(integer) 1
SREM myset "z"
(integer) 0
SPOP: Randomly Pop Elements
REDIS
# Pop one random element
SPOP myset
"b"
# Pop 2 random elements
SPOP myset 2
1) "c"
2) "d"
SRANDMEMBER: Randomly Get Elements (Without Removing)
REDIS
SADD myset2 "x" "y" "z"
# Get 1 random element
SRANDMEMBER myset2
"x"
# Get 2 distinct random elements
SRANDMEMBER myset2 2
1) "y"
2) "z"
# Get 2 random elements (may repeat)
SRANDMEMBER myset2 -2
1) "x"
2) "x"
Use Cases
| Scenario | Description |
|---|---|
| Tag system | User tags, article tags |
| Social relationships | Friend lists, follow lists |
| Deduplication | Unique counts, UV statistics |
| Lottery | Randomly draw winners |
Example: Tag System
BASH
# Add tags to an article
SADD article:123:tags "redis" "database" "nosql"
# Get all tags of an article
SMEMBERS article:123:tags
# Check if article has a specific tag
SISMEMBER article:123:tags "redis"
# Remove a tag
SREM article:123:tags "nosql"
Example: Mutual Friends
BASH
# User 1's friends
SADD friends:user1 "Alice" "Bob" "Charlie"
# User 2's friends
SADD friends:user2 "Bob" "Charlie" "David"
# Mutual friends
SINTER friends:user1 friends:user2
Example: Lottery System
BASH
# Add participants
SADD lottery "user1" "user2" "user3" "user4" "user5"
# Draw 1 winner
SPOP lottery
# Draw 3 winners (no repeats)
SPOP lottery 3
Set Operations
SINTER: Intersection
REDIS
SADD set1 "a" "b" "c"
SADD set2 "b" "c" "d"
SINTER set1 set2
1) "b"
2) "c"
SUNION: Union
REDIS
SUNION set1 set2
1) "a"
2) "b"
3) "c"
4) "d"
SDIFF: Difference
REDIS
# Elements in set1 but not in set2
SDIFF set1 set2
1) "a"
# Elements in set2 but not in set1
SDIFF set2 set1
1) "d"
Storing Results
REDIS
# Store intersection into a new set
SINTERSTORE result set1 set2
(integer) 2
# Store union into a new set
SUNIONSTORE result set1 set2
(integer) 4
# Store difference into a new set
SDIFFSTORE result set1 set2
(integer) 1
❓ FAQ
Q What's the difference between SMEMBERS and SSCAN?
A SMEMBERS returns all elements at once and may block for large sets. SSCAN returns batches — suitable for large datasets.
Q What's the maximum number of elements in a set?
A Theoretically 2^32 - 1 elements (about 4.2 billion).
Q What's the difference between a set and a list?
A Sets are unordered with unique elements; lists are ordered and allow duplicates. Choose based on your needs.
📖 Summary
- Sets are unordered collections of unique strings
- SADD adds, SREM removes, SMEMBERS gets all elements
- SISMEMBER checks existence, SCARD gets size
- Supports intersection (SINTER), union (SUNION), difference (SDIFF)
- Suitable for tags, social relationships, deduplication, lottery
📝 Exercises
- Tag practice: Create an article tag system, add different tags to 3 articles, find articles with common tags
- Friends practice: Simulate friend relationships for 3 users, find mutual friends between any two users
- Lottery practice: Create a lottery system, add 10 participants, randomly draw 3 winners
Next Lesson
In the next lesson, we will learn Redis Sets (Part 2), covering advanced set operations.



