Redis Compass (The ZSET Datastructure) –Part 8

ZSET fully known as Sorted Set is a data-structure which has some of the attributes of a SET in that it can only contain unique elements and also some attributes of a Hash in that each element is associated with a key-like structure called a score which is used to determine the order in which the Sorted Set should be sorted.  In a scenario where two elements have the same score (key-like structure), the SET is sorted lexicographically ( the way you would sort a normal English dictionary).

One more cool thing about ZSET is that because each element is basically tied to it’s score, it has a O(1) (meaning the speed is constant no matter how big the SET is) complexity analysis for operations such as add and remove.

ZADD

If you’ve read the SET post you could probably guess what this command does.  This command is utilized when you want to initialize or add an element to a Sorted Set.

The key-like values “1” and “20” in the above example are what we call score.

ZRANGE

Say we had a long sorted list of java-spring resources, something like:

To get the values between an index of 1 and 20, we simply do

ZRANGEBYSCORE

This works the same way as ZRANGE except that instead of index it returns elements between a score range.

 

Conclusion

We’ve covered the major part of what we would be using in ZSET, to learn more please check-out the redis documentation.

 

One comment on “Redis Compass (The ZSET Datastructure) –Part 8

Leave a Reply

Your email address will not be published. Required fields are marked *