4
votes

I need to remove 10 000 keys.

  1. What is better way: to exec this kind of script

    EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 "ROOT"

  2. May be better is to set expiration time and Redis will remove them? But how to do it in console using Lua script?

The script (see above) works because del comman dhas a format:

del key1 key2 ...

But Expire works only for 1 key.

Is it possible to do it in lua script?

For example: my application creates some search results cache and set for every page ttl = 3600. But user wants to clear cache immediately, i.e. delete all matching keys or set smaller expiration for them.

2
Before recommending on an approach for doing this, do you need all keys delete in one atomic batch or are your requirements more lenient?Itamar Haber
I'd like to execute this command (batch?) in c# application which has GUI.ZedZip
So you are aware that an atomic operation will block the Redis server while it is running and that's inline with the requirements (i.e. don't run it in an environment where Redis is needs to remain available to server requests), right?Itamar Haber
Yes, you are right. But I do not want to run loop in my client application, because it will be not optimal and long operation. I need to set expiration for multiple keys matching some pattern. I see that using 'keys' in my example is not a good solution too. What else?ZedZip

2 Answers

3
votes

Whether you DEL or EXPIRE, once Lua script runs it will block other clients and if it runs for too long it the lua-time-limit timeout. Despite your reluctance for looping, I strongly recommend you do.

Expiry vs deletion may lessen some of the immediate load (yet to be empirically proven), so feel free to go with one or the other. In either case, use a client-side loop on a SCAN operation to invoke the command for each key. If you have a server/worker process somewhere in your architecture, you can consider delegating this task to it so the client will not be kept busy.

EDIT per comment: Variadic commands such as DEL are generally more performant than non-variadic commands, however here you're comparing two different operations so there are no assurances. The DEL approach is potentially more blocking because Redis will actually go ahead and delete the keys immediately - if you have a lot of keys to delete and/or their values are big, this could take more time. The EXPIRE approach tries to avoid this by leveraging on Redis' lazy expiry mechanism (it uses idle time to do so when possible), so the deletion-due-to-expiry load is theoretically better distributed. The best way to determine which works better for you is by testing both and comparing - I'd love to learn of your results!

5
votes

You can either use (from redis cli) to delete all keys:

flushall

or run this from your command line (bash)

redis-cli --scan --pattern aihello_user* | xargs redis-cli del