Skip to main content

Distributed locks with Redis

Project description


DistLock
Distributed locks with Redis and Python

Why DistLock ?

This library implements the DistLock algorithm introduced by @antirez, Due to the origin fork from redlock has been inactive since 2015, Then this repo was born with additional implementations:

  • Bug fix
  • Python3 syntax improvements
  • New tweak for adaptation

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 :

  1. Client A acquires the lock into the master.
  2. The master crashes before the write to the key is transmitted to the slave.
  3. The slave gets promoted to master.
  4. Client B acquires the lock to the same resource A already holds a lock for. SAFETY VIOLATION!

A quick introduction to the DistLock algorithm

To resolve this problem, the Distlock 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 DistLock algorithm can be found in the Redis documentation: Distributed locks with Redis.

APIs

The distlock.DistLock class shares a similar API with the threading.Lock class in the Python Standard Library.

Basic Usage

from distlock import DistLock
# By default, if no redis connection details are
# provided, DistLock uses redis://127.0.0.1:6379/0
lock =  DistLock("distributed_lock")
lock.acquire()
do_something()
lock.release()

With Statement / Context Manager

As with threading.Lock, distlock.DistLock objects are context managers thus support the With Statement. This way is more pythonic and recommended.

from distlock import DistLock
with DistLock("distributed_lock"):
    do_something()

Specify multiple Redis nodes

from distlock import DistLock
with DistLock("distributed_lock",
              connection_details=[
                {'host': 'xxx.xxx.xxx.xxx', 'port': 6379, 'db': 0},
                {'host': 'xxx.xxx.xxx.xxx', 'port': 6379, 'db': 0},
                {'host': 'xxx.xxx.xxx.xxx', 'port': 6379, 'db': 0},
                {'host': 'xxx.xxx.xxx.xxx', 'port': 6379, 'db': 0},
              ]
            ):
    do_something()

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 DistLockFactory

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

from distlock import DistLockFactory
factory = DistLockFactory(
    connection_details=[
        {'host': 'xxx.xxx.xxx.xxx'},
        {'host': 'xxx.xxx.xxx.xxx'},
        {'host': 'xxx.xxx.xxx.xxx'},
        {'host': 'xxx.xxx.xxx.xxx'},
    ])

with factory.create_lock("distributed_lock"):
    do_something()

with factory.create_lock("another_lock"):
    do_something()

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

distlock-0.0.1.dev1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

distlock-0.0.1.dev1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file distlock-0.0.1.dev1.tar.gz.

File metadata

  • Download URL: distlock-0.0.1.dev1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

File hashes

Hashes for distlock-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 f25ff69753d23e53f5900592f8687b266d37e755ec7ed13b74494f0006dc8e24
MD5 c5c42a1a5ff1c30d1e881c283a3e1c4f
BLAKE2b-256 7beee03f58910e4f4faa5890d95dcdebb9e5e368e579a5a039732fdb093a58f5

See more details on using hashes here.

File details

Details for the file distlock-0.0.1.dev1-py3-none-any.whl.

File metadata

  • Download URL: distlock-0.0.1.dev1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

File hashes

Hashes for distlock-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 fec0d176e7913d21e1366cacc55e1cae95edf61b3d409b97da436cf3db4b4559
MD5 195190ce48fb15e4f6190ceb5e648d12
BLAKE2b-256 5e64e918be70b985eda68f10fbfa35432a742666d7a9c4a552a6e916175382be

See more details on using hashes here.

Supported by

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