Redlock in Ruby

This came in useful today. Setting up a distributed lock with multiple consensus nodes.

require 'ddtrace'
require 'redlock'
require 'redis'

urls = ['redis://localhost:6379/1', 'redis://localhost:6379/2']
redii = do |url| url).tap do |redis|
        Datadog.configure(redis, service_name: "local/redis-redlock")

rl = redii

rl.lock "fourseconds", 4000

rl.lock("fourseconds", 4000) do |lockstate|
    puts lockstate

This includes
– multiple local instances of redis to test against
– datadog APM tracing
– acquiring a one-time lock
– acquiring a lock, and releasing it after executing a block

You can run an instance of redis with

docker run -d --name redis -p 6379:6379 circleci/redis:5.0.5