Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Set Default Order

Tags:

redis

In a Redis Set (not sorted set). I noticed the values are in the order they are added with the newest being first. I didn't find in the docs if the set will always maintain this order. So my question, can I trust they will always be ordered like this?

like image 450
Matthew Scragg Avatar asked Feb 20 '23 10:02

Matthew Scragg


1 Answers

In general, you cannot trust the order of the elements. If you want it in a particular order, you either use a list or a sortedset.

A Set is built using a HashTable. There are absolutely no guarantees to the order of the element.

However, if you are set only contains integers, and the number of elements is less than the directive set-max-intset-entries (by default 512) - then the set is built on a structure called IntSet, or Integer Set. An Integer Set is a sorted integer array. The iterator for such a set will return elements in ascending order.

So, more specifically, if you can program to an integer set, you can assume the elements will be in sorted order.

like image 79
Sripathi Krishnan Avatar answered Feb 26 '23 20:02

Sripathi Krishnan