In our previous post, we welcomed Valkey (an open source fork of Redis) to the Percona family. We also learned how to get Valkey up and running using docker, along with understanding some of the basic data types, string and list. In this post, we will continue working with Valkey/Redis and learn about sets and sorted sets.

Sets

In computer science, a set is an abstract data type that can store unique values, without any particular order.” –Wikipedia entry on Set

In Valkey/Redis, a set is just like what Wikipedia tells us: a data type that can hold string-based unique values without any order. Let’s create a few different sets of users for our application which holds their favorite foods.

We will use SADD to not only create our sets initially but also to add one or more elements to the set.

Recall from our first post that Valkey/Redis has no concept of namespaces, or any nested structure. The use of the “:” separator in the key names is purely by choice; we could have used “user 91-favfoods” as the key, and it would have worked just the same. Be consistent in your code on how you want to create keys.

The number returned after each invocation of SADD indicates the number of elements added to the set.

Why was only one element added? Recall from the definition for set that it can “store unique values”. Since ‘Pizza’ was already a member of this set, it was not added again.

We can inspect the contents of each set using SMEMBERS <key> and count how many elements with SCARD <key>:

Let’s imagine our application has a cool feature that lets you see which foods you have in common with your friends. The foods in common would be those foods in your set and your friend’s set. This would be an intersection of the two sets:

Here’s a quick visualization of the sets and their intersection using Venn diagrams:

What about foods that all three users like? SINTER <key1> <key2> [keyN …]

Everybody loves Tacos!

Foods that user:91 likes but user:87 does not like? SDIFF <key1> <key2> [keyN …]

SDIFF shows the differences between the first key and all subsequent keys.

Hopefully, now you understand how sets can handle various types of memberships and how using the different SET-based functions can generate interesting relationships!

Sorted sets

A sorted set in Valkey/Redis is similar to a set in that it contains only unique members. An additional property is used called the ‘score’ to create a sorting order. ‘Score’ is a generic term used by the sorted set. It could be the weight of packages, ages of students in class, or number of protons in an atom. You give the score an actual meaning based on the data you store.

Scores can be signed integers or floating-point numbers. If two elements have the same score, then the value is sorted lexicographically to determine the order. Since no two values can be the same, there will never be ties in the sorted set.

Let’s create a sorted set of a few cities in Argentina using their population as the score:

Hang on, why is Mar del Plata first on this list? It has the smallest population and should be sorted last in this list. Ah, yes, however sorted sets in Valkey/Redis are sorted low to high. Scores start at 0 and increase from there. (i.e., ASC by default)

Valkey/Redis provides a way to reverse the output of our set, so we get the highest score first (i.e., DESC); we can also get the scores with the option ‘WITHSCORES’ modifier.

Why are we seeing three results? Much like most computer programming, lists, sets, sorted sets, and others use 0-based indexes. So, in fact, we’ve asked to start at element 0, and go through element 2, thus three results.

Conclusion

In this post, we introduced the concept of sets and sorted sets in Valkey/Redis. We learned how to find common elements and differences between sets. With sorted sets, we introduced the ‘score’ concept and showed how to get elements returned in reverse sorted order.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments