Skip to main content

Redis Distributed Lock

Project description

redilock : Redis Distributed Lock

Introduction - what is a Lock / Mutex?

In multithreaded/asynchronous programs, multiple "tasks" run in parallel. One challenge with such parallel tasks is that sometimes there is a need to make sure that only one task will call a function or access a resource.

A lock (a.k.a Mutex) is a code facility that acts as a gatekeeper and allows only one task to access a resource.

Python provides threading.Lock() and asyncio.Lock() exactly for this purpose.

Distributed Locks

When working with multiple processes or multiple services/hosts - we also need a lock but now we need a “distributed lock” which is similar to the standard lock except that it is available to other programs/services/hosts.

As Redis is a storage/caching system, we can use it to act as a distributed lock.

Redilock is a simple python package that acts as a simple distributed lock and allows you to add locking capabilites to any cloud/distributed environment.

Redilock main features:

  • Simple to use:
    • Context manager (with statement)
    • Only 2 api calls lock() and unlock()
  • Supports both synchronous implementation and an async implementation
  • Safe:
    • Caller must specify the lock-expiration (TTL - time to lock) so even if the program/host crashes - the lock will be eventually released
    • Unlocking the lock can be performed only by the party who put the lock

Installation

# pip install python-redilock

Usage & Examples

(for synchronous code, async is identical and straightforward. check out the examples directory for more examples):

The easiest way to use a lock (whether async or not) is using the with statement

import redilock.sync_redilock as redilock
mylock = redilock.DistributedLock(ttl=30)  # auto-release after 30s

with mylock("my_lock"):
  print("I've got the lock !!")

Once creating the lock (my_lock variable) it can be used to lock different resources (or different lock, if you prefer). In the example above, we use a simple with-statement to lock the "my_lock" lock, print something and unlock. When using this approach, the lock is always blocking - the code will wait until the lock is available. Note that we're using ttl=30 which means that if our code fails or the program crashes - the lock will expire after 30 seconds.

If you do not want to rely on the with-statement or you need better control over the lock - you can directly using lock and unlock

import redilock.sync_redilock as redilock

lock = redilock.DistributedLock(ttl=300)  # lock for maximum 5min

unlock_secret_token = lock.lock("my_lock")  # Acquire the lock
lock.unlock(unlock_secret_token)  # Release the lock

By default, if you try to acquire a lock - your program will be blocked until the lock is acquired. you can specify non-blocking mode which can be useful in many cases, for example:

import redilock.sync_redilock as redilock

lock = redilock.DistributedLock(ttl=10)  # lock for 10s

lock.lock("my_lock")  
if not lock.lock("my_lock", block=False):  # try to lock again but do't block  
  print("Couldnt acquire the lock")

Note that in the example above we lock for 10s and then we try to lock without blocking and that's why we see the print immediately. If you run the example twice - the second time will have to wait 10s until the lock (from the first run) is released .

Good to know and best practices

  • The TTL is super important. it dictates when to auto-release the lock if your code doesnt release it (in case of a bug or a crash). You should not rely on it for unlocking as your code should either unlock using the unlock function or via with statement. As so, a large value (e.g 30-60 seconds) is probably fine.
  • you can specify TTL when instantiating the class or when performing the lock operation itself.
  • When using blocking lock there is a background loop that checks redis periodically if the lock is still acquired. The system uses check-interval of 0.25. You can modify this value if needed via the interval parameter.
mylock = redilock.DistributedLock(interval=2)
  • The lock is not re-entrant. it means that if a task (thread/coroutine) owns it and tries to lock again - it will be blocked until the lock expires (ttl). For example
with mylock("my_lock", ttl=5):
  print("I've got the lock, let's lock again")
  with mylock("my_lock", ttl=5):  # <------------- will block for 5s
    print("I've got the lock again")

Technically, it is possible to create a re-entrant distributed lock but i tend to believe that if you need such facility - you're probably using the wrong architecture or you don't need this redilock :) .

  • using a with-statement for locking is indeed the easiest way however there is one big tricky "gotcha" with this approach. if your TTL is too short - the lock will expire while you're still in the "with" Consider the following code:
import time
import redilock.sync_redilock as redilock

mylock = redilock.DistributedLock(ttl=2)  # lock that will autoexpire after 2s

with mylock("my_lock"):
    print("I've got the lock !!")
    time.sleep(3)
    print("Hmm...i dont have the lock anymore :( ")

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

python_redilock-0.1.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

python_redilock-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file python_redilock-0.1.1.tar.gz.

File metadata

  • Download URL: python_redilock-0.1.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.4

File hashes

Hashes for python_redilock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 880121f0113aa582283742abb8fff03116660721769388d76b304c60cf10b2ed
MD5 0fe4da48e1b488dad55faa6afd68702e
BLAKE2b-256 40440f399149bc2c075a655022a7bf2ee61d293d3a2ad2aa08985efc4c1e73a9

See more details on using hashes here.

File details

Details for the file python_redilock-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_redilock-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69971c1fbf82a5246432670889e292cbac2832e273f2f43950bc2724b66f00c9
MD5 33cae4fa2081b25610a5da65c5ba30ac
BLAKE2b-256 cd15b3a9793eeff0a54cab7eac1fe24d700fe76c2b1ddbb55b758d913b9ae71e

See more details on using hashes here.

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