100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Redis

Sets in Redis

Explore Redis Sets as unordered collections of unique strings, covering membership checks, set algebra like intersection and union, and randomized sampling.

Core Data StructuresBeginner7 min readJul 10, 2026
Analogies

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.

bash
# 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 1

Set 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

Was this page helpful?

Topics covered

#Redis#RedisStudyNotes#Database#SetsInRedis#Sets#Membership#Add#Remove#StudyNotes#SkillVeris