Comments on: Safely running bulk operations on Redis with lua scripts https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/ My hobby... Tue, 19 Apr 2016 08:12:33 +0000 hourly 1 https://wordpress.org/?v=6.9 By: Alex Forbes https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-11936 Tue, 19 Apr 2016 08:12:33 +0000 http://blog.al4.co.nz/?p=1870#comment-11936 In reply to Tim Schimandle.

Many thanks Tim, I’m sure that will be useful to others!

]]>
By: Tim Schimandle https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-11932 Mon, 18 Apr 2016 16:34:37 +0000 http://blog.al4.co.nz/?p=1870#comment-11932 I have created a batch script that copies the algorithm in your bash script for windows users. You can find it at http://stackoverflow.com/questions/36699547/how-can-i-run-redis-lua-scripts-in-windows/36699548#36699548

]]>
By: Patrick https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-8826 Fri, 24 Apr 2015 09:48:47 +0000 http://blog.al4.co.nz/?p=1870#comment-8826 You, Sir, I appreciate your work.
Thank you very much! Your documentation saved me a lot of time and reduced the impact on our installation to nearly zero. I adapted your script to delete nearly 8M keys from a deprecated hash.

]]>
By: ☁ Itamar Haber (@itamarhaber) https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-7902 Fri, 27 Feb 2015 12:18:10 +0000 http://blog.al4.co.nz/?p=1870#comment-7902 In reply to Alex Forbes.

And thirdly – because SCAN is non-deterministic, you won’t be able to mix it with commands that write (e.g. EXPIRE) in a Lua script – Redis will throw an error to that effect :)

]]>
By: Alex Forbes https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-7901 Fri, 27 Feb 2015 12:12:30 +0000 http://blog.al4.co.nz/?p=1870#comment-7901 In reply to Lyle Hanson.

Yes, each SCAN is a round trip and incurs some overhead. I didn’t do it in lua for a couple of reasons.

Firstly, we need a way to limit the amount of work being done in each round trip to avoid blocking redis. You could reduce the number of trips to one by scanning the entire keyspace in one go, but this solution seeks to avoid doing that.

Secondly, the number of round trips for SCAN is very small compared to the EXPIRE commands, because we’re only doing one SCAN per 500 keys (in my example above). So where expiring 500 keys wold previously have required 501 round trips, it now requires 1. Thus, the benefit of doing the scans in lua is 500x less than doing the expires (depending on the scan size). If you want to do less rounds trips, you can increase the size of each scan but this means doing more work in each iteration, and thus blocking redis for longer.

I think what you might be getting at though, is whether it’s possible to do the scan in lua, while yielding to other clients on each iteration (to avoid blocking). You could implement the bash logic above in a language with a native redis client (python, ruby, lua), which would enable you to reuse the same connection and thus reduce tcp overhead. The benefit would be relatively small though, and this was done for a one-off batch operation, so that level of optimisation would have been somewhat premature.

Note that you shouldn’t do the scan within the EVAL, because, as redis is single threaded, it will be blocked for the duration of the EVAL’s execution. Ref: http://redis.io/commands/eval#atomicity-of-scripts
Also, a script can not run for longer than 5 seconds: http://redis.io/commands/eval#sandbox-and-maximum-execution-time

If you do come up with a pure lua solution or reimplement the bash logic please link to it here, I’d be very interested to see it!

]]>
By: Lyle Hanson https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-7793 Wed, 25 Feb 2015 01:22:41 +0000 http://blog.al4.co.nz/?p=1870#comment-7793 Unless I’m mistaken, this approach still incurs the round-trip overhead for the SCAN results. Is there a particular reason you don’t execute the SCAN in Lua as well?

]]>
By: Alex Forbes https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-4972 Mon, 01 Dec 2014 15:15:27 +0000 http://blog.al4.co.nz/?p=1870#comment-4972 In reply to Mike Donnelly.

Thank you Mike, that’s a subtle bug but an important one, I will update the post!

]]>
By: Mike Donnelly https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-4971 Mon, 01 Dec 2014 15:04:56 +0000 http://blog.al4.co.nz/?p=1870#comment-4971 Fantastic Alex, thanks. Seems you have a bug tho… the for loop on line 25 of your bash script should have a “<=" rather than just a "<", i.e.

keys=$(echo $reply | awk '{for (i=2; i<=NF; i++) print $i}')

I was testing it with just one key in my keyspace and nothing was happening :)

]]>
By: Alex https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-2795 Mon, 13 Oct 2014 10:46:42 +0000 http://blog.al4.co.nz/?p=1870#comment-2795 In reply to K. Konstantinidis (@kkonstan).

Cheers Kostas!

And, ouch :(

]]>
By: K. Konstantinidis (@kkonstan) https://blog.al4.co.nz/2014/08/safely-running-bulk-operations-on-redis-with-lua-scripts/#comment-2789 Mon, 13 Oct 2014 09:12:02 +0000 http://blog.al4.co.nz/?p=1870#comment-2789 Nice write up Alex.

Remembered it again today while reviewing code submitted by our dev team I found usage of keys in a key service that will frequently do that against a redis deployment used by a distributed system with several thousand clients.

You’d think the warning on the redis docs would be sufficient deterrent, but apparently it’s not.

]]>