I've read the post about distributed locks with Redis at http://redis.io/topics/distlock. There's a lua script to describe how to do "unlock".
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
I think there is a race condition with this model:
- Client A acquires the lock with 3 seconds expiration.
SET key randomstring1 NX PX 3000
- Sleep 2.99 seconds.
- Client A releases the lock and call the above code.
- The condition is true.
if redis.call("get",KEYS[1]) == ARGV[1] then
- The origin key expires
- Client B acquires anthor lock.
SET key randomstring2 NX PX 3000
- Client A delete the key.
- Client B's lock is deleted by Client A!