Skip to content

Redlock Redis-based distributed locks implementation in Clojure

License

Notifications You must be signed in to change notification settings

lantiga/redlock-clj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redlock-clj

Redlock is an algorithm for distributed locks on top of a cluster of uncoordinated Redis instances. redlock-clj is a redlock implementation in Clojure leveraging on the Carmine Redis client.

Algorithm and reference implementation by Salvatore Sanfilippo.

Installation

Leiningen: add the following to the :dependencies vector in project.clj

[redlock-clj "0.1.0"]

Usage

Require redlock-clj

(require '[redlock-clj.core :as redlock])

Define a cluster as a vector of connection options (see Carmine for details, in particular the docs for wcar):

(def cluster [{:pool {<opts1>} :spec {<opts1>}}
              {:pool {<opts2>} :spec {<opts2>}}
              {:pool {<opts3>} :spec {<opts3>}}])

e.g.

(def cluster [{:spec {:host "http://127.0.0.1" :port 6379}}
              {:spec {:host "http://127.0.0.1" :port 6380}}
              {:spec {:host "http://127.0.0.1" :port 6381}}])

The API consists of two functions, used for locking and unlocking a resource:

(defn lock! [cluster resource ttl & {:keys [retry-count retry-delay clock-drift-factor]}])

returning either a lock map {:validity <validity-time> :resource <resource> :value <value>}, where value is a random uuid created by the client acquiring the lock, or nil in case the lock can't be acquired;

(defn unlock! [cluster lock])

which unlocks a previously acquired lock.

Look at the test for an example of a distributed lock around a file-based counter.

Run the test with lein test after starting at least two out of three redis-server instances with ports 6379, 6380, 6381.

License

Copyright © 2014 Luca Antiga

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

Redlock Redis-based distributed locks implementation in Clojure

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published