Skip to content
SADD, SMEMBERS & SISMEMBER
step 1/3

Reading — step 1 of 3

Redis Sets

~1 min readSets & Sorted Sets

SADD, SMEMBERS & SISMEMBER

Redis sets are unordered collections of unique strings.

Commands

CommandDescriptionReturns
SADD key member [member ...]Add membersCount of NEW members added
SMEMBERS keyGet all membersRESP array
SISMEMBER key memberCheck membership:1 or :0
SCARD keyGet sizeInteger
SREM key member [member ...]Remove membersCount removed

Uniqueness

Adding a duplicate does nothing: SADD myset a a a returns :1 (only one new member).

Implementation

Use your language's hash set:

  • Python: set()
  • JavaScript: Set
  • Go: map[string]bool
  • C++: std::unordered_set<std::string>

Note: For testing SMEMBERS, we sort the output alphabetically to ensure deterministic comparison.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

SADD, SMEMBERS & SISMEMBER — Build Redis from Scratch