Skip to the workspace
Pebellslearn by building
Sets  ›  Lesson

SADD, SREM and SMEMBERS

SADD 12 minSADD adds members to a set and replies with how many were new, SREM removes them, and SMEMBERS returns all members in no guaranteed order.

redis / SADD, SREM and SMEMBERS

Explore each scene at your own pace.

01 · The rule

The rule

SADD, SREM and SMEMBERS

Wordcraft awards badges: first win, a three-game streak, a speedy round. A player either has a badge or does not, earning it twice changes nothing, and the order they were earned in does not matter on the badge shelf. That is a set.

The rule

A set holds each member once, in no particular order.

A set is an unordered collection of unique strings. Adding a member that is already there does nothing.

127.0.0.1:6379> SADD badges:asha first-win streak-3
(integer) 2
127.0.0.1:6379> SADD badges:asha streak-3 speedster
(integer) 1
127.0.0.1:6379> SMEMBERS badges:asha
1) "streak-3"
2) "first-win"
3) "speedster"
127.0.0.1:6379> SREM badges:asha speedster night-owl
(integer) 1
127.0.0.1:6379> SMEMBERS badges:nobody
(empty array)
  • SADD key member [member ...] creates the set if needed and replies with how many members were new. Above, streak-3 was already there, so only speedster counted.
  • SREM key member [member ...] replies with how many members were really removed. night-owl was never there.
  • SMEMBERS key returns every member. The order is not guaranteed and can differ from the order you added them; sort in your app if you need a fixed order.
  • Like hashes and lists, an empty set is deleted automatically.

Typical uses:

  • Tags and categories: tags:word:orbit holds space, science.
  • Unique visitors: SADD visitors:2026-09-25 asha on every visit; duplicates collapse, so the set size is the number of distinct visitors.
  • Relationships: friends:asha, blocked:ravi, the members of a chat room.
  • Deduplication: "has this email already been sent?"

SMEMBERS returns the whole set in one reply. That is fine for a handful of badges, but for a set with millions of members use SSCAN, which walks through it in small batches.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.