Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should redis zscan on sorted set return my results sorted by score order?

For an application I'm working on I'm looking at using a sorted set in Redis to store items with a timestamp as the score and an arbitrary message as the member. I was then planning on using zscan to be able to retrieve items from the sorted set in order. Order is important to the application which is why I want to use sorted set.

Things seemed to be going well until I tried to retrieve items with zscan and they appeared to be out of order. For example, I set up a test where I add 1000 items to a sorted set using the integer value as the score and the string representation as the member (initializations omitted for brevity):

    for( int i = 0; i < 1000; i++){
        jedis.zadd(CHANNEL, i, Integer.toString(i));
    }

    ScanResult<Tuple> result = jedis.zscan("TEST_CHANNEL", "0", new ScanParams().count(10));

The items that I got back seemed to be in random order. Using the redis-cli to run the zscan confirmed the same results the test found:

127.0.0.1:6379> zscan "TEST_CHANNEL" 0

Returned results 125, 588, 568, 144.....

Doing a zrange from 0 to 10 correctly returned members from "0" to "10".

I've tried looking at the documentation (http://redis.io/commands/zscan) but I can't find anything confirming whether or not zscan should or should not return results in score order and was hoping someone could clear up whether I'm seeing a bug, expected behaviour, or just plain doing it wrong.

I'm using Redis 3.0.1

like image 851
scuba_steve Avatar asked May 06 '15 20:05

scuba_steve


1 Answers

The order of reply from the [HSZ]SCAN family of commands is based on the internal data structure that Redis uses, whose order is determined by several factors but most importantly by the updates made to the data. Excluding engineered tests and random coincidences - disordered reply is the expected behavior.

like image 194
Itamar Haber Avatar answered Oct 15 '22 07:10

Itamar Haber