VALKEY-SETS(7)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ VALKEY-SETS ✨

okay bestie, pull up a chair, because this is valkey-sets from the Valkey Manual (section 7, she's fancy like that) and honestly? sets are lowkey one of the most underrated data types in the whole game. let's get into it πŸ’…


🌸 NAME 🌸

valkey-sets β€” Sets. that's it, that's the whole name. iconic brevity.


πŸ¦‹ DESCRIPTION πŸ¦‹

so a Set is an unordered collection of unique strings (we call the strings members). no duplicates allowed, no order promised, just vibes and uniqueness.

and you can use Sets to efficiently do all of this:

  • Track unique items β€” like tracking every unique IP address that hit a given blog post. one IP, one member, no dupes, periodt.
  • Represent relations β€” like the set of all users who have a given role. slay.
  • Perform common set operations β€” intersection, unions, differences, all the classic girlboss math moves.

πŸ’– Basic commands πŸ’–

  • SADD β€” adds a new member to a set.
  • SREM β€” removes the specified member from the set.
  • SISMEMBER β€” tests a string for set membership.
  • SINTER β€” returns the set of members that two or more sets have in common (i.e., the intersection).
  • SCARD β€” returns the size (a.k.a. cardinality) of a set.

also, if you want the complete list of set commands, go peek at valkey-commands(7). she has everything, no cap.


🌈 EXAMPLES 🌈

  • Store the sets of bikes racing in France and the USA. heads up bestie: if you add a member that already exists, it will be ignored. no error, no drama, it just doesn't happen.
127.0.0.1:6379> SADD bikes:racing:france bike:1
(integer) 1
127.0.0.1:6379> SADD bikes:racing:france bike:1
(integer) 0
127.0.0.1:6379> SADD bikes:racing:france bike:2 bike:3
(integer) 2
127.0.0.1:6379> SADD bikes:racing:usa bike:1 bike:4
(integer) 2

see that second one? returned (integer) 0 because bike:1 was already in there. the set said "not me accepting duplicates" πŸ’€

  • Check whether bike:1 or bike:2 are racing in the US.
127.0.0.1:6379> SISMEMBER bikes:racing:usa bike:1
(integer) 1
127.0.0.1:6379> SISMEMBER bikes:racing:usa bike:2
(integer) 0
  • Which bikes are competing in both races?
127.0.0.1:6379> SINTER bikes:racing:france bikes:racing:usa
1) "bike:1"
  • How many bikes are racing in France?
127.0.0.1:6379> SCARD bikes:racing:france
(integer) 3

πŸŽ€ Tutorial πŸŽ€

so the SADD command adds new elements to a set. and it's also possible to do a whole bunch of other operations against sets β€” like testing if a given element already exists, performing the intersection, union or difference between multiple sets, and so forth. the range is giving versatile.

127.0.0.1:6379> SADD bikes:racing:france bike:1 bike:2 bike:3
(integer) 3
127.0.0.1:6379> SMEMBERS bikes:racing:france
1) bike:3
2) bike:1
3) bike:2

here I've added three elements to my set and told Valkey to return all the elements. and notice β€” there is no order guarantee with a set. Valkey is free to return the elements in any order at every call. so don't you dare write code that depends on the order, bestie, I'm begging πŸ™ˆ

Valkey has commands to test for set membership. and these commands can be used on single as well as multiple items:

127.0.0.1:6379> SISMEMBER bikes:racing:france bike:1
(integer) 1
127.0.0.1:6379> SMISMEMBER bikes:racing:france bike:2 bike:3 bike:4
1) (integer) 1
2) (integer) 1
3) (integer) 0

look at SMISMEMBER doing the most in one trip, we love an efficient queen πŸ‘‘

We can also find the difference between two sets. for instance, we may want to know which bikes are racing in France but not in the USA:

127.0.0.1:6379> SADD bikes:racing:usa bike:1 bike:4
(integer) 2
127.0.0.1:6379> SDIFF bikes:racing:france bikes:racing:usa
1) "bike:3"
2) "bike:2"

There are other non trivial operations that are still easy to implement using the right Valkey commands. for instance we may want a list of all the bikes racing in France, the USA, and some other races. we can do this using the SINTER command, which performs the intersection between different sets. in addition to intersection you can also perform unions, difference, and more. for example if we add a third race we can see some of these commands in action:

127.0.0.1:6379> SADD bikes:racing:france bike:1 bike:2 bike:3
(integer) 3
127.0.0.1:6379> SADD bikes:racing:usa bike:1 bike:4
(integer) 2
127.0.0.1:6379> SADD bikes:racing:italy bike:1 bike:2 bike:3 bike:4
(integer) 4
127.0.0.1:6379> SINTER bikes:racing:france bikes:racing:usa bikes:racing:italy
1) "bike:1"
127.0.0.1:6379> SUNION bikes:racing:france bikes:racing:usa bikes:racing:italy
1) "bike:2"
2) "bike:1"
3) "bike:4"
4) "bike:3"
127.0.0.1:6379> SDIFF bikes:racing:france bikes:racing:usa bikes:racing:italy
(empty array)
127.0.0.1:6379> SDIFF bikes:racing:france bikes:racing:usa
1) "bike:3"
2) "bike:2"
127.0.0.1:6379> SDIFF bikes:racing:usa bikes:racing:france
1) "bike:4"

You'll note that the SDIFF command returns an empty array when the difference between all sets is empty. and you'll also note that the order of sets passed to SDIFF matters, since the difference is not commutative. swap the arguments, get a different answer. she's picky like that πŸ’…

When you want to remove items from a set, you can use the SREM command to remove one or more items from a set, or you can use the SPOP command to remove a random item from a set. you can also return a random item from a set without removing it using the SRANDMEMBER command:

127.0.0.1:6379> SADD bikes:racing:france bike:1 bike:2 bike:3 bike:4 bike:5
(integer) 5
127.0.0.1:6379> SREM bikes:racing:france bike:1
(integer) 1
127.0.0.1:6379> SPOP bikes:racing:france
"bike:3"
127.0.0.1:6379> SMEMBERS bikes:racing:france
1) "bike:2"
2) "bike:4"
3) "bike:5"
127.0.0.1:6379> SRANDMEMBER bikes:racing:france
"bike:2"

SPOP takes one out and keeps it, SRANDMEMBER just shows you one and puts it right back. different energy, both valid 🫢


🧚 Limits 🧚

The max size of a Set is 2^32 - 1 (4,294,967,295) members. that's a lot of members. like, a lot. you're probably fine.


πŸ‘‘ Performance πŸ‘‘

Most set operations β€” including adding, removing, and checking whether an item is a set member β€” are O(1). this means they're highly efficient. lightning. effortless. main character energy ⭐

However β€” for large sets with hundreds of thousands of members or more, you should exercise caution when running the SMEMBERS command. that one is O(n) and returns the entire set in a single response. so yeah, on a huge set that's a whole lotta data coming at you at once 😭

as an alternative, consider the SSCAN, which lets you retrieve all members of a set iteratively. little bites instead of the whole cake. do that one, bestie.


and that's the whole page, fr fr. sets: unique, unordered, unreasonably good at what they do. go forth and SADD responsibly πŸ’–

this has been VALKEY-SETS(7) from the Valkey Manual, version 9.0.0, dated 2025-10-21. ty for reading, mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from