Pessimistic locking for ruby redis
Switch branches/tags
Nothing to show
Latest commit 3886b97 Jun 4, 2014 @PatrickTulskie Version 0.2.0


Requires the redis gem. Including this in your project will give you additional locking abilities on any instance of a redis connection you create.


gem install redis-lock


require 'redis'
require 'redis-lock # This will automatically include Lock into the Redis class.

Here's a little example of what you can do with it:

timeout = 10 # measured in seconds
max_attempts = 100 # number of times the action will attempt to lock the key before raising an exception

$redis =

$redis.lock('beers_on_the_wall', timeout, max_attempts)
# Now no one can acquire a lock on 'beers_on_the_wall'

# Other processes can now acquire a lock on 'beers_on_the_wall'

For convenience, there is also a lock_with_update function that accepts a block. It handles the locking and unlocking for you.

$redis.lock_for_update('beers_on_the_wall') do
	$redis.multi do
		$redis.set('sing', 'take one down, pass it around.')

Additional Notes

This gem basically implements the algorithm described here:


Patrick Tulskie;