Git Product home page Git Product logo

redis-lock's Introduction

Redis::Lock

Build Status

This gem implements a pessimistic lock using Redis. It correctly handles timeouts and vanishing lock owners (such as machine failures)

This uses setnx, but not the setnx algorithm described in the redis cookbook, which is not robust.

Installation

Add this line to your application's Gemfile:

gem 'mlanett-redis-lock', require: 'redis-lock'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mlanett-redis-lock

Background

A lock needs an expected lifetime. If the owner of a lock disappears (due to machine failure, network failure, process death), you want the lock to expire and another owner to be able to acquire the lock. At the same time, the owner of a lock should be able to extend its lifetime. Thus, you can acquire a lock with a conservative estimate on lifetime, and extend it as necessary, rather than acquiring the lock with a very long lifetime which will result in long waits in the event of failures.

A lock needs an owner. Redis::Lock defaults to using an owner id of HOSTNAME:PID.

A lock may need more than one attempt to acquire it. Redis::Lock offers an acquisition timeout; this defaults to 10 seconds.

There are two lock methods: Redis#lock, which is more convenient, and Redis::Lock#lock. Notice there are two timeouts: the lock's lifetime (:life option) and the acquisition timeout, which is less important. The acquisition timeout is set via the :acquire option to Redis#lock or passed directly to Redis::Lock#lock.

Usage

This gem adds lock(), locked?() and unlock() to Redis instances.

lock() takes a block and is safer than using lock() and unlock() separately. lock() takes a key and lifetime and optionally an acquisition timeout (defaulting to 10 seconds).

redis = ::Redis.connect
redis.lock("test") { |lock| do_something }

redis.lock("test", life: 2*60, acquire: 2) do |lock|
  array.each do |entry|
    do_something(entry)
    lock.extend_life(60)
  end
end

locked?() checks if lock is already acquired. It takes key argument, which you have used with lock() function earlier.

redis = ::Redis.connect
redis.lock("test") { |lock| do_something }

if redis.locked?("test")
  puts "work in progress"
end

Goals

I wrote this when noticing that other lock gems were using non-robust approaches.

You need to be able to handle race conditions while acquiring the lock. You need to be able to handle the owner of the lock failing to release it. You need to be able to detect stale locks. You need to handle race conditions while cleaning the stale lock and acquiring a new one. The code which cleans the stale lock may not be able to assume it gets the new one. The code which cleans the stale lock must not interfere with a different owner acquiring the lock.

Contributors

Alexander Lang (langalex), Jonathan Hyman (jonhyman), Jamie Cobbett (jamiecobbett), Ravil Bayramgalin (brainopia), and Tom Mornini (tmornini) have contributed to Redis Lock.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

redis-lock's People

Contributors

0x000000 avatar barmstrong avatar jamiecobbett avatar jonhyman avatar kieranklaassen avatar langalex avatar mkdynamic avatar mlanett avatar nthx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.