Registry / database / redlock-py

redlock-py

JSON →
library1.0.8pypypi✓ verified 25d ago

redlock-py is a Python library that implements the Redis-based distributed lock manager algorithm (Redlock). It provides a mechanism for acquiring and releasing distributed locks across multiple independent Redis instances to ensure mutual exclusion in a distributed system. The library is currently at version 1.0.8, with a relatively low release cadence, as its PyPI project description was last updated in 2016 and the linked GitHub repository in 2021, suggesting a maintenance-level status.

pip install redlock-py redis
INSTALL
IMPORT
SIG · REDLOCK-PY
R
redlock-py
databasepythonv1.0.8
Install
2.8s avg
Import
446ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.472s · 24.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.420s · 25MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Redlock
from redlock import Redlock

This quickstart demonstrates how to acquire and release a distributed lock using `redlock-py` with a context manager. It initializes the Redlock manager with Redis connection details and attempts to lock a resource. For a robust Redlock implementation, it's crucial to configure multiple independent Redis instances; the example uses a single instance for simplicity.

import os from redlock import Redlock import redis # Configure Redis connection(s) # For a true Redlock setup, use multiple independent Redis instances. # For quickstart, a single local instance is shown. REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost') REDIS_PORT = int(os.environ.get('REDIS_PORT', 6379)) REDIS_DB = int(os.environ.get('REDIS_DB', 0)) redis_nodes = [ {"host": REDIS_HOST, "port": REDIS_PORT, "db": REDIS_DB} # For production, add more independent Redis nodes, e.g.: # {"host": "redis2.example.com", "port": 6379, "db": 0}, # {"host": "redis3.example.com", "port": 6379, "db": 0}, ] # Initialize the Redlock manager dlm = Redlock(redis_nodes) resource_name = "my_critical_resource" lock_validity_time_ms = 5000 # Lock will be valid for 5 seconds try: # Acquire the lock using a context manager (recommended) print(f"Attempting to acquire lock for '{resource_name}'...") with dlm.lock(resource_name, lock_validity_time_ms) as my_lock: if my_lock: print(f"Lock '{resource_name}' acquired! (Validity: {my_lock.validity}ms)") # Your critical section code goes here print("Executing critical section...") # Simulate work import time time.sleep(2) print("Critical section complete.") else: print(f"Could not acquire lock for '{resource_name}'. Another process may hold it.") except redis.exceptions.ConnectionError as e: print(f"Error connecting to Redis: {e}. Make sure Redis is running.") except Exception as e: print(f"An unexpected error occurred: {e}") print("Finished redlock-py quickstart example.")
Debug
Known issues
gotchaThe underlying Redlock algorithm has been subject to significant academic and practical critique regarding its safety guarantees, particularly concerning system clock skew, long garbage collection pauses, and the absence of fencing tokens. These issues can potentially lead to violations of mutual exclusion under certain failure conditions.
fix
Understand the limitations of Redlock and assess if its 'correctness vs. efficiency' trade-offs are acceptable for your use case. For applications requiring strong correctness guarantees, consider alternative distributed consensus systems like ZooKeeper or etcd, or implement application-level fencing.
affects: All versions
gotcharedlock-py requires a Redis client library (e.g., `redis-py`) to communicate with Redis. This dependency is not automatically installed with `pip install redlock-py` and must be installed separately.
fix
Ensure `redis` is installed in your environment: `pip install redlock-py redis`.
affects: All versions
gotchaFor the Redlock algorithm to provide its intended fault tolerance and mutual exclusion properties, it *must* be configured with multiple independent Redis instances (an odd number like 3 or 5 is common). Using a single Redis instance or a master-replica setup defeats its purpose and can lead to race conditions due to asynchronous replication.
fix
Deploy and configure `redlock-py` with a list of connection details for at least three independent Redis masters. Do not rely on master-replica replication for Redlock's fault tolerance.
affects: All versions
gotcharedlock-py does not natively implement 'fencing tokens,' which are crucial for preventing 'stale' clients (those that experience a pause and mistakenly believe they still hold a lock after it has expired) from writing to shared resources. This can lead to data corruption.
fix
If correctness is paramount, consider augmenting your system with an external mechanism for fencing tokens, or use a distributed lock system that inherently provides this feature.
affects: All versions
gotchaThe project exhibits low maintenance activity. The PyPI description was last updated in 2016, and the linked GitHub repository shows the last commit in 2021. This might indicate a lack of ongoing support, bug fixes, or compatibility updates for newer Python or Redis versions.
fix
Evaluate the project's suitability for long-term production use. Be prepared to maintain the library internally or consider more actively maintained alternatives if regular updates and support are critical.
affects: All versions
Upgrade
Version history
1.0.8latest on PyPI · released Mar 8, 2016
Audit
Dependencies
redisrequiredRequires a Redis client library (e.g., redis-py) to connect to Redis instances.
Agent activity
15 hits · last 30 days
node
14
Resources
redlock-py — pip install redlock-py · libregistry