Question

In redis docs, it is stated that keys command should not be used in production, since it blocks other processes while executing, it is better to use scan iteration over all keys with some batch size. I've read in docs that redis use hash index, so I assume it can't use it for range queries like scan and keys.

But our system is done in such a way that we need to use scans extensively. Is it ok, could it decrease the performance of hash queries significantly?

Was it helpful?

Solution

Note Redis is single-thread, so any operation could block another.

(It tried to address this issue in its recent release by delegating the time spent reading and writing to I/O sockets over to other threads, so the Redis process can devote more cycles to manipulating, storing, and retrieving data—boosting overall performance. You need benchmark by yourself on your own use case though.)

I've read in docs that redis use hash index, so I assume it can't use it for range queries

There are multiple data types in Redis, and if you are using sorted set, zrange returns the specified range of elements.

Is it ok, could it decrease the performance of hash queries significantly?

Not sure about your use case, but I think scan is better used for a matching, for example in Java

KeyScanCursor<String> cursor = sync.scan(ScanArgs.Builder.limit(1000).match(wildMatch));

and yes scan could be very slow and returns very few keys per cursor. So as above example shows, it's better to set a max key number to avoid it taking very long time and/or triggering the request resource limit.

Therefore, you can do something like

while (!cursor.isFinished() && fetchedResult.size() < keyNumber) 
{
    fetchedResult.addAll(cursor.getKeys());
    cursor = sync.scan(cursor, ScanArgs.Builder.matches(wildMatch));
}
Licensed under: CC-BY-SA with attribution
scroll top