What Redis Sets Are
A Redis set is an unordered collection of unique strings, backed internally by a hash table (or a compact intset encoding when every member is an integer), giving O(1) average-case time for SADD, SREM, and SISMEMBER regardless of how large the set grows. Because duplicates are automatically rejected, sets are the natural structure for tracking distinct membership — unique visitors to a page, tags on an article, or the set of user IDs who have completed an action — without any application-side deduplication logic.
Cricket analogy: A Redis set is like the list of players who have scored a century in an IPL season — Virat Kohli either appears once or not at all, and SADD attempting to add him again after his first century simply has no effect.
Membership, Add, and Remove
SADD adds one or more members and returns how many were newly added (ignoring ones already present), SISMEMBER checks membership in O(1), and SMISMEMBER (added in Redis 6.2) checks several members at once, returning an array of 0/1 flags. SCARD returns the set's size in O(1) since Redis tracks the count internally, and SPOP removes and returns one or more random members, which is handy for tasks like picking a random winner from a pool of entrants without pulling the whole set client-side.
Cricket analogy: SADD is like adding a player to the 'centurions this series' set — the return value tells the scorer whether Rohit Sharma's century was newly recorded or if he was already on the list.
# Track unique visitors to a page today
SADD visitors:20260710 "user:42" "user:88"
SISMEMBER visitors:20260710 "user:42" # => 1
SCARD visitors:20260710 # unique visitor count
# Set algebra across two days
SINTER visitors:20260709 visitors:20260710 # returning visitors
SUNIONSTORE visitors:weekly visitors:20260709 visitors:20260710
SDIFF visitors:20260710 visitors:20260709 # new visitors today only
# Random sampling without removal
SRANDMEMBER raffle:entries 3
# Random pick with removal (pick and eliminate a winner)
SPOP raffle:entries 1Set Algebra: Union, Intersection, and Difference
Redis provides SINTER, SUNION, and SDIFF for computing the intersection, union, and difference of multiple sets in a single command, plus the *STORE variants (SINTERSTORE, SUNIONSTORE, SDIFFSTORE) that save the result directly into a new key instead of returning it to the client. This makes sets excellent for tag-based filtering (find products tagged both 'wireless' and 'noise-cancelling'), social graph queries (mutual followers between two accounts), or cohort analysis (users active both yesterday and today), all computed server-side without pulling raw data into the application.
Cricket analogy: SINTER is like finding players who have both scored a century and taken a five-wicket haul in the same IPL season — an intersection of two sets like 'centurions' and 'five-fors' computed instantly server-side.
SRANDMEMBER returns random members without modifying the set (useful for sampling), while SPOP removes the members it returns (useful for one-time draws like raffle winners or dealing cards). Passing a negative count to SRANDMEMBER allows the same member to be returned more than once, which is useful for weighted or repeated random sampling.
SINTER, SUNION, and SDIFF are computed synchronously and their cost scales with the total size of the input sets, so running them across sets with millions of members can block the single Redis thread noticeably. For very large sets, prefer the *STORE variants during off-peak windows, or shard the computation, rather than running raw SINTER on hot-path requests.
- Redis sets store unique, unordered string members with O(1) average-case add, remove, and membership checks.
- SADD, SREM, SISMEMBER, and SMISMEMBER manage and query membership without application-side deduplication.
- SCARD gives set size in O(1); SPOP removes random members, SRANDMEMBER samples without removing them.
- SINTER/SUNION/SDIFF compute set algebra server-side, ideal for tag filters, mutual-follower queries, and cohort analysis.
- The *STORE variants persist algebra results into a new key instead of returning them to the client.
- Large-scale SINTER/SUNION/SDIFF calls can be expensive; run them off the hot path or during off-peak windows for huge sets.
Practice what you learned
1. What happens if you SADD a member that already exists in a Redis set?
2. Which command computes members present in one set but not another?
3. What is the difference between SPOP and SRANDMEMBER?
4. What does SINTERSTORE do differently from SINTER?
5. Why are Redis sets a good fit for tracking unique daily visitors to a page?
Was this page helpful?
You May Also Like
Hashes in Redis
Learn how Redis Hashes model field-value objects efficiently, covering HSET/HGET, partial updates, memory-efficient small-hash encoding, and common object-modeling patterns.
Sorted Sets in Redis
Master Redis Sorted Sets, where every unique member carries a floating-point score, enabling leaderboards, priority queues, and range-based queries by rank or score.
Redis Key Naming Conventions
Understand why disciplined key naming matters in Redis and how to design namespaces that scale across teams and services.