How do you use MULTI/EXEC (and WATCH) in an evented Redis driver like the em-hiredis (a Ruby driver that use EventMachine)? If I run:
redis.multi do
redis.sadd("foo", "bar") do
redis.inc("baz", "qux") do
redis.exec do
puts 'yay!'
end
end
end
end
there’s a chance that some other part of the application manages to sneak in an operation before the EXEC, if there is a lot going on (imagine, for example, that I have a timer that increments some key every second, and that the code above takes more than one second to run, then some of the increment commands will be sent as part of the MULTI/EXEC — what if I want to abort the transaction? Then any increments that happened to become part of it will disappear. It’s easy to come up with even worse scenarios).
I guess I could implement some kind of locking so that no other actions can be done while a MULTI/EXEC is in progress, but that doesn’t feel like a great solution, has anyone else found a better way?
As @balu stated in the comments to the question it cannot be done without multiple connections.