Redis::DistLock - Distributed lock manager using Redis
use Redis::DistLock; my $rd = Redis::DistLock->new( servers => [qw[ localhost:6379 ]] ); my $mutex = $rd->lock( "foo", 10 ); die( "failed to get a lock" ) if ! $mutex; # ... critical section ... $rd->release( $mutex );
This is an implementation of the Redlock algorithm using Redis for distributed lock management. It enables lightweight distributed locks in order to prevent cronjob overruns, help with queue processing, many workers of which only one should run at a time, and similar situations.
NOTE: This needs at least Redis version 2.6.12 which adds new options
SET command making this implementation possible.
new( ... )
Takes a hash or hash reference with below arguments and returns a lock manager
instance. Since this module currently does not repair initially failed
connections it checks for the majority of connections or
Array reference with servers to connect to or Redis objects to use.
Maximum number of times to try to acquire the lock. Defaults to
Maximum delay between retries in seconds. Defaults to
Flag to check redis server version(s) in the constructor to ensure compatibility. Defaults to
Optional subroutine that will be called with errors as parameter, should any occur. By default, errors are currently just warnings. To disable pass
Flag to enable automatic release of all locks when the lock manager instance goes out of scope. Defaults to
CAVEAT: Ctrl-C'ing a running Perl script does not call DESTROY(). This means you will have to wait for Redis to expire your locks for you if the script is killed manually. Even if you do implement a signal handler, it can be quite unreliable in Perl and does not guarantee the timeliness of your locks being released.
lock( $resource, $ttl )
Acquire the lock for the resource with the given time to live (in seconds) until the lock expires. Without a value generates a 32 character base64 string based on 24 random input bytes.
lock( $resource, $ttl, $value )
Same as lock() but with a known value instead of a random string.
lock( $resource, $ttl, $value, $extend )
Same as lock(), but given
$extend is true it extends an existing
lock or creates a new one instead of having to unlock first.
NOTE: This option is EXPERIMENTAL and might change without warning!
release( $lock )
Release the previously acquired lock.
release( $resource, $value )
Version of release() that allows to maintain state solely in Redis when the value is known, e.g. a hostname.
This code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in production environments.
This module was originally developed at Booking.com. With approval from Booking.com, this module was released as open source, for which the authors would like to express their gratitude.
COPYRIGHT AND LICENSE
Copyright (C) 2014 by Simon Bertrang, Ryan Bastic
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.