SADD-&-SMEMBERS
Sets are unordered collections of unique strings. Perfect for tags, unique visitors, and set operations like intersections.
You'll Learn
- SADD, SMEMBERS, SREM syntax
- Set operations: SINTER, SUNION, SDIFF
- Checking membership with SISMEMBER
- How Redimo displays unique members
See Your Data, Not Terminal Text
Redimo visualizes every Redis data type beautifully. Edit inline, undo mistakes, stay safe in production.
1. What Is a Set?
A Set is an unordered collection where every member is unique. Adding a duplicate simply does nothing - no error, no duplicate. This makes Sets perfect for tracking unique items without worrying about deduplication logic in your application.
Sets support powerful mathematical operations like intersection, union, and difference. These operations are executed directly in Redis with O(N) complexity, making them much faster than fetching data and computing in your application.
Syntax
SADD key member [member ...] # Add members (returns count of new members added)
SMEMBERS key # Get all members
SREM key member [member ...] # Remove members
SISMEMBER key member # Check if member exists (returns 1 or 0)
SCARD key # Count members
SRANDMEMBER key [count] # Get random member(s)
SPOP key [count] # Remove and return random member(s)Set Advantages
- • Automatic deduplication
- • O(1) membership checking
- • Native set operations (∩, ∪, -)
- • Random member selection
Best Use Cases
- • Tags and categories
- • Unique visitor tracking
- • Friend lists and followers
- • Blacklists and whitelists
2. The CLI Way (Hard Mode)
Tracking user tags or permissions in CLI means lots of SMEMBERS and mental parsing of unordered output:
Building a Tag System
127.0.0.1:6379> SADD user:1001:tags "premium" "newsletter" "beta-tester" "early-adopter"
(integer) 4
127.0.0.1:6379> SADD user:1001:tags "premium"
(integer) 0
127.0.0.1:6379> SMEMBERS user:1001:tags
1) "beta-tester"
2) "early-adopter"
3) "newsletter"
4) "premium"
127.0.0.1:6379> SISMEMBER user:1001:tags "premium"
(integer) 1
127.0.0.1:6379> SISMEMBER user:1001:tags "admin"
(integer) 0Finding Common Tags Between Users
127.0.0.1:6379> SADD user:1002:tags "newsletter" "vip" "early-adopter"
(integer) 3
127.0.0.1:6379> SINTER user:1001:tags user:1002:tags
1) "early-adopter"
2) "newsletter"
127.0.0.1:6379> SUNION user:1001:tags user:1002:tags
1) "beta-tester"
2) "early-adopter"
3) "newsletter"
4) "premium"
5) "vip"
127.0.0.1:6379> SDIFF user:1001:tags user:1002:tags
1) "beta-tester"
2) "premium"
127.0.0.1:6379> SCARD user:1001:tags
(integer) 4The Problem
3. The Redimo Way (Smart Mode)
Redimo sorts set members alphabetically for easy scanning, shows the total count, and lets you add or remove members with a single click. No more random ordering confusion.
What Redimo Does for Sets:
- ●Sorted Display: Members are sorted alphabetically, making it easy to scan for specific values or spot patterns.
- ●Duplicate Prevention: When you try to add an existing member, Redimo shows a clear message instead of silently ignoring it.
- ●One-Click Operations: Add new members, delete existing ones, or copy values - all without typing commands.
- ●Member Count: Total member count is always visible in the header, no need for SCARD.
- ●Search Within Set: Search for specific members in large sets without scrolling through everything.
CLI Pain Points
- • Random member ordering
- • Silent duplicate rejection
- • No visual structure
- • Set operations hard to visualize
- • Must use SCARD for count
Redimo Solutions
- • Alphabetically sorted for scanning
- • Clear duplicate feedback
- • Clean list with actions
- • Member count always visible
- • Search within the set
4. Common Patterns
Tag System
# Add tags to an article
SADD article:123:tags "javascript" "tutorial" "react" "frontend"
# Check if article has specific tag
SISMEMBER article:123:tags "react"
# Get all articles with a specific tag (using reverse index)
SADD tag:react article:123 article:456 article:789
# Find articles with BOTH javascript AND react tags
SINTER tag:javascript tag:reactSocial Connections
# User follows other users
SADD user:1001:following user:1002 user:1003 user:1004
# User's followers
SADD user:1001:followers user:1005 user:1006
# Find mutual follows (friends)
SINTER user:1001:following user:1001:followers
# Suggest follows: people your friends follow that you don't
SDIFF user:1002:following user:1001:followingUnique Tracking
# Track unique visitors per day
SADD visitors:2024-12-25 "ip:1.2.3.4" "ip:5.6.7.8" "ip:1.2.3.4" # Returns 2
# Count unique visitors
SCARD visitors:2024-12-25
# Weekly unique visitors (union of daily sets)
SUNIONSTORE visitors:week:52 visitors:2024-12-23 visitors:2024-12-24 visitors:2024-12-25
# Random sample of visitors
SRANDMEMBER visitors:2024-12-25 105. Set Operations Explained
Set operations are one of the most powerful features of Redis Sets. They're computed server-side, which is much faster than fetching data and computing in your application.
| Operation | Command | Description |
|---|---|---|
| Intersection | SINTER A B | Members in BOTH A and B |
| Union | SUNION A B | Members in A OR B (combined) |
| Difference | SDIFF A B | Members in A but NOT in B |
| Store Result | SINTERSTORE C A B | Store operation result in new key C |
Practical Example: Feature Flags
# Users with each feature flag
SADD feature:dark-mode user:1001 user:1002 user:1003
SADD feature:beta-ui user:1002 user:1004
# Find users with BOTH features enabled
SINTER feature:dark-mode feature:beta-ui
# Result: user:1002
# Find users with dark mode but NOT beta UI
SDIFF feature:dark-mode feature:beta-ui
# Result: user:1001, user:10036. Pro Tips
SMEMBERS Warning
Useful Set Commands
# Move member between sets atomically
SMOVE source:set destination:set "member-value"
# Check multiple members at once (Redis 6.2+)
SMISMEMBER user:tags "premium" "beta" "vip" # Returns [1, 0, 1]
# Pop random member (useful for job queues)
SPOP job:queue
# Iterate large sets safely
SSCAN user:tags 0 MATCH "pre*" COUNT 100
# Store operation results (more efficient than getting + storing)
SINTERSTORE common:tags user:1001:tags user:1002:tagsRedimo Bonus
Ready to Visualize?
Stop scrolling through random set members in your terminal.
Download Redimo and manage sets with sorted, searchable views.