TExcelTAD
Home › Redis › Redis Sets

Redis Sets

A set is an unordered collection of unique strings. Adding an element that is already there does nothing, and checking whether an element is a member is fast (O(1)) no matter how big the set is. Sets are perfect for tags, unique visitors, permissions, "who liked this post", and for computing intersections and unions.

Adding, checking and removing

SADD returns how many elements were new; duplicates are ignored. SISMEMBER answers yes (1) or no (0), and SMISMEMBER checks several elements at once.

redis · try it yourself
SADD tags:post:1 redis database nosql
SADD tags:post:1 redis cache
SCARD tags:post:1
SISMEMBER tags:post:1 cache
SISMEMBER tags:post:1 sql
SMISMEMBER tags:post:1 nosql sql redis
SREM tags:post:1 nosql sql
SMEMBERS tags:post:1
redis> SADD tags:post:1 redis database nosql
(integer) 3
redis> SADD tags:post:1 redis cache
(integer) 1
redis> SCARD tags:post:1
(integer) 4
redis> SISMEMBER tags:post:1 cache
(integer) 1
redis> SISMEMBER tags:post:1 sql
(integer) 0
redis> SMISMEMBER tags:post:1 nosql sql redis
1) (integer) 1
2) (integer) 0
3) (integer) 1
redis> SREM tags:post:1 nosql sql
(integer) 1
redis> SMEMBERS tags:post:1
1) "redis"
2) "database"
3) "cache"

SCARD is the number of members (the set's cardinality). SREM returns how many members it really removed.

Note: Sets are unordered. SMEMBERS happens to return small sets in a stable order, but large sets come back in an order that depends on the internal hash table. Never rely on it: use a sorted set or sort in your program when order matters.

Small sets of integers

When every member is an integer, Redis stores a small set as a sorted array of numbers (the intset encoding), which is why such sets print in numeric order:

redis · try it yourself
SADD lucky 42 7 19 3 7
SMEMBERS lucky
OBJECT ENCODING lucky
SADD lucky seven
OBJECT ENCODING lucky
redis> SADD lucky 42 7 19 3 7
(integer) 4
redis> SMEMBERS lucky
1) "3"
2) "7"
3) "19"
4) "42"
redis> OBJECT ENCODING lucky
"intset"
redis> SADD lucky seven
(integer) 1
redis> OBJECT ENCODING lucky
"listpack"

As soon as a non-number joins, the encoding changes. You never have to manage this yourself.

Set algebra

The real power of sets is combining them on the server:

Command Result Picture
SINTER a b members in both A ∩ B
SUNION a b members in either A ∪ B
SDIFF a b in a but not in b A − B
SINTERCARD n a b only the size of the intersection |A ∩ B|

The examples below keep the results small. When a result has several members, the order in which they are listed can change from one server run to the next, because Redis builds the result in a temporary hash-based set.

redis · try it yourself
SADD skills:ada python redis
SADD skills:linus c git redis
SINTER skills:ada skills:linus
SDIFF skills:ada skills:linus
SINTERCARD 2 skills:ada skills:linus
SUNIONSTORE skills:team skills:ada skills:linus
SISMEMBER skills:team git
redis> SADD skills:ada python redis
(integer) 2
redis> SADD skills:linus c git redis
(integer) 3
redis> SINTER skills:ada skills:linus
1) "redis"
redis> SDIFF skills:ada skills:linus
1) "python"
redis> SINTERCARD 2 skills:ada skills:linus
(integer) 1
redis> SUNIONSTORE skills:team skills:ada skills:linus
(integer) 4
redis> SISMEMBER skills:team git
(integer) 1

The union has four members (redis is counted once). It is stored with SUNIONSTORE here because the order in which SUNION lists a union's members is not stable between server runs.

Each of these commands has a ...STORE variant (SINTERSTORE, SUNIONSTORE, SDIFFSTORE) that saves the result in a new key instead of returning it, and returns the size of the result.

Example: friends in common

Social features are a natural fit. Suppose every user has a set of the people they follow:

redis · try it yourself
SADD follows:mai an binh chi dung
SADD follows:an binh em
SINTERSTORE common:mai:an follows:mai follows:an
SMEMBERS common:mai:an
SDIFF follows:an follows:mai
SISMEMBER follows:mai em
redis> SADD follows:mai an binh chi dung
(integer) 4
redis> SADD follows:an binh em
(integer) 2
redis> SINTERSTORE common:mai:an follows:mai follows:an
(integer) 1
redis> SMEMBERS common:mai:an
1) "binh"
redis> SDIFF follows:an follows:mai
1) "em"
redis> SISMEMBER follows:mai em
(integer) 0

Mai and An both follow Binh. An also follows Em, whom Mai does not follow yet: a candidate for a "you may know" suggestion.

Moving members

SMOVE source destination member moves one member atomically. It is a simple way to model state changes such as "online" → "away":

redis · try it yourself
SADD users:online u1 u2 u3
SMOVE users:online users:away u2
SMEMBERS users:online
SMEMBERS users:away
SMOVE users:online users:away u9
redis> SADD users:online u1 u2 u3
(integer) 3
redis> SMOVE users:online users:away u2
(integer) 1
redis> SMEMBERS users:online
1) "u1"
2) "u3"
redis> SMEMBERS users:away
1) "u2"
redis> SMOVE users:online users:away u9
(integer) 0

Counting unique things

A set gives you exact unique counts. For example, the unique visitors of a page on one day:

redis · try it yourself
SADD visitors:2026-10-17 user:1 user:2 user:1 user:3 user:2
SCARD visitors:2026-10-17
redis> SADD visitors:2026-10-17 user:1 user:2 user:1 user:3 user:2
(integer) 3
redis> SCARD visitors:2026-10-17
(integer) 3

Five visits came from only three different users. This is exact, but it stores every member. For millions of visitors where an estimate is good enough, the HyperLogLog lesson shows a structure that uses a fixed 12 KB.

Commands to avoid on huge sets

  • SMEMBERS returns every member at once. On a set with millions of members it is slow and blocks the server; iterate with SSCAN instead.
  • SPOP and SRANDMEMBER return random members. They are useful for raffles and sampling, but their results change from run to run, so they are not shown in these playgrounds.

Test yourself

What does SADD s a b a return on an empty key?

  1. (integer) 3
  2. (integer) 2
  3. (integer) 1
  4. OK

Which command returns the members that are in set a but not in set b?

  1. SINTER a b
  2. SUNION a b
  3. SDIFF a b
  4. SDIFF b a

You need the members of a set in a guaranteed order. What should you do?

  1. Use SMEMBERS, it is always sorted
  2. Use a sorted set or sort the result in your program
  3. Use SCARD
  4. Use SPOP