Skip to main content

Distributed locks with Redis

Project description

|RedLock logo|

RedLock - Distributed locks with Redis and Python

|Build Status|

This library implements the RedLock algorithm introduced by
`@antirez <>`__

Yet another ...

There are already a few redis based lock implementations in the Python
world, e.g. `retools <>`__,
`redis-lock <>`__.

However, these libraries can only work with *single-master* redis
server. When the Redis master goes down, your application has to face a
single point of failure . We can't rely on the master-slave replication,
because Redis replication is asynchronous.

This is an obvious race condition with the master-slave replication
model :

#. Client A acquires the lock into the master.
#. The master crashes before the write to the key is transmitted to
the slave.
#. The slave gets promoted to master.
#. Client B acquires the lock to the same resource A already holds a

A quick introduction to the RedLock algorithm

To resolve this problem, the Redlock algorithm assume we have ``N``
Redis masters. These nodes are totally independent (no replications). In
order to acquire the lock, the client will try to acquire the lock in
all the N instances sequentially. If and only if the client was able to
acquire the lock in the majority (``(N+1)/2``)of the instances, the lock
is considered to be acquired.

The detailed description of the RedLock algorithm can be found in the
Redis documentation: `Distributed locks with
Redis <>`__.


The ``redlock.RedLock`` class shares a similar API with the
``threading.Lock`` class in the Python Standard Library.

Basic Usage

.. code:: python

from redlock import RedLock
# By default, if no redis connection details are
# provided, RedLock uses redis://
lock = RedLock("distributed_lock")

With Statement / Context Manager

As with ``threading.Lock``, ``redlock.RedLock`` objects are context
managers thus support the `With
Statement <>`__.
This way is more pythonic and recommended.

.. code:: python

from redlock import RedLock
with RedLock("distributed_lock"):

Specify multiple Redis nodes

.. code:: python

from redlock import RedLock
with RedLock("distributed_lock",
{'host': '', 'port': 6379, 'db': 0},
{'host': '', 'port': 6379, 'db': 0},
{'host': '', 'port': 6379, 'db': 0},
{'host': '', 'port': 6379, 'db': 0},

| The ``connection_details`` parameter expects a list of keyword
arguments for initializing Redis clients.
| Other acceptable Redis client arguments can be found on the `redis-py
doc <>`__.

Reuse Redis clients with the RedLockFactory

Usually the connection details of the Redis nodes are fixed.
``RedLockFactory`` can help reuse them, create multiple RedLocks but
only initialize the clients once.

.. code:: python

from redlock import RedLockFactory
factory = RedLockFactory(
{'host': ''},
{'host': ''},
{'host': ''},
{'host': ''},

with factory.create_lock("distributed_lock"):

with factory.create_lock("another_lock"):

.. |RedLock logo| image::
.. |Build Status| image::

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redlock-1.2.0.tar.gz (7.2 kB view hashes)

Uploaded source

Built Distribution

redlock-1.2.0-py2-none-any.whl (7.3 kB view hashes)

Uploaded 2 7

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page