Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis scan returns empty results but nonzero cursor

Tags:

redis

I have a redis database with a few million keys. Sometimes I need to query keys by the pattern e.g. 2016-04-28:* for which I use scan. First call should be

scan 0 match 2016-04-28:*

it then would return a bunch of keys and next cursor or 0 if the search is complete.

However, if I run a query and there are no matching keys, scan still returns non-zero cursor but an empty set of keys. This keeps happening to every successive query, so the search does not seem to end for a really long time.

Redis docs say that

SCAN family functions do not guarantee that the number of elements returned per call are in a given range. The commands are also allowed to return zero elements, and the client should not consider the iteration complete as long as the returned cursor is not zero.

So I can't just stop when I get empty set of keys.

Is there a way I can speed things up?

like image 217
xaxa Avatar asked Dec 25 '22 06:12

xaxa


1 Answers

You'll always need to complete the scan (i.e. get cursor == 0) to be sure there no no matched. You can, however, use the COUNT option to reduce the number of iterations. The default value of 10 is fast If this is a common scenario with your match pattern - start increasing it (e.g. double or powers of two but put a max cap just in case) with every empty reply, to make Redis "search harder" for keys. By doing so, you'll be saving on network round trips so it should "speed things up".

like image 141
Itamar Haber Avatar answered Jan 14 '23 03:01

Itamar Haber