BDENTHUS
BDENTHUS

Reputation: 59

How to Scan the Redis Keyspace sequentially in the order keys are stored?

In Redis,using SCAN/COUNT command combination we can retrieve the keys from a key space but in random order. For example if a key space has 100 keys and the keys are stored as 001,002,003...100. If we use the command 'scan 0 count 50', the results would contain 50 keys listed in random order (for example 002, 003, 050...).

Is there anyway we can Scan the key space and fetch keys in the order it is stored in Redis? Expected key scan results should be like 001, 002, 003,...050...

In addition, is there a way to point to the particular key in Redis key space and scan next 'n' keys? For example, go to key - 010 and scan from 11 to 30.

Please advice.

Upvotes: 2

Views: 2944

Answers (1)

Not_a_Golfer
Not_a_Golfer

Reputation: 49195

The short answer to both questions is: No.

It's possible to do the keyspace iteration manually, by indexing the keys in a sorted set responsible for that only, and then using ZRANGE and ZRANGEBYLEX to scan the keyspace in an ordered way and to start scanning from a prefix, respectively.

Let's say you create keys foo, bar and baz, you also need to index them. We perform this in transactions to make sure the index is consistent

> MULTI
> SET foo 1
> ZADD __index__ 0 foo
> EXEC
>
> MULTI
> SET bar 2
> ZADD __index__ 0 bar
> EXEC
>
> MULTI
> SET baz whatever
> ZADD __index__ 0 baz
> EXEC
... and so on 

Notice that we've added all the keys to a sorted set with a score of 0. This means that we can take advantage of lexical ranges.

To iterate the index now we simply perform ZRANGE or ZREVRANGE. Because all the elements are with a score of 0, the order will be lexicographical. So paging the first 10 elements:

ZRANGE __index__ 0 10

To iterate 10 keys from a specific entry "foo":

ZRANGEBYLEX __index__ [foo + LIMIT 0 10

This requires active stuff in your code, but not that hard to do. But anyway, long answer - this is the only way to achieve this at the moment. There are modules that will allow you to automate it a bit, but it's not fully automated yet.

Upvotes: 4

Related Questions