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!