Registry / database / redlock

redlock

JSON →
library1.2.0pypypi✓ verified 85d ago

redlock is a Python implementation of the Redlock algorithm for distributed locks with Redis. It provides a simple API to acquire and release locks across multiple Redis instances to ensure reliability in distributed systems. The current version is 1.2.0, with a release cadence that is not very frequent, suggesting a stable but less actively developed library.

pip install redlock
INSTALL
IMPORT
SIG · REDLOCK
R
redlock
databasepythonv1.2.0
Install
2.7s avg
Import
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.2.0 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 24MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.7s · import 0.000s · 24MB
22MB installed
● package 22MB
Code
Verified usage

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

RedLock
from redlock import RedLock
from redlock import Redlock

This quickstart demonstrates how to initialize Redlock with a single Redis instance (for demonstration) and acquire/release a distributed lock. In a real-world scenario, you should configure multiple independent Redis master instances to leverage the Redlock algorithm's fault tolerance. The `lock_ttl` is specified in milliseconds.

import redis from redlock import Redlock import os # Configure Redis instances. For production, use multiple instances # and ensure they are properly configured for high availability. # Here, we default to localhost for a runnable example. redis_host = os.environ.get('REDIS_HOST', 'localhost') redis_port = int(os.environ.get('REDIS_PORT', 6379)) redis_masters = [ {"host": redis_host, "port": redis_port, "db": 0} # For true distributed locking, add more Redis instances: # {"host": "192.168.1.30", "port": 6379, "db": 0}, # {"host": "192.168.1.31", "port": 6379, "db": 0} ] # Initialize Redlock with Redis instances dlm = Redlock(redis_masters=redis_masters) resource_name = "my_critical_resource" lock_ttl = 5000 # Lock validity time in milliseconds (5 seconds) print(f"Attempting to acquire lock for resource: {resource_name}") lock_info = dlm.lock(resource_name, lock_ttl) if lock_info: print(f"Successfully acquired lock: {lock_info}") try: # --- Critical section begins --- print("Performing critical operation...") # Simulate work import time time.sleep(2) # Keep operation less than lock_ttl # --- Critical section ends --- finally: print("Releasing lock...") dlm.unlock(lock_info) print("Lock released.") else: print(f"Failed to acquire lock for resource: {resource_name}. It might be held by another process.")
Debug
Known issues
gotchaThe `ttl` (Time-To-Live) parameter for `lock()` is specified in milliseconds, not seconds. Forgetting this common Redis API convention can lead to locks expiring much faster or slower than intended.
fix
Always provide the `ttl` value in milliseconds. E.g., for a 5-second lock, use `5000`.
affects: All versions
gotchaTo ensure true distributed locking and fault tolerance as per the Redlock algorithm, you must configure `Redlock` with multiple independent Redis master instances (N/2 + 1 quorum). Using a single Redis instance defeats the purpose of distributed locking and makes your system susceptible to a single point of failure.
fix
Provide `Redlock` with a list of at least 3 Redis master instances in `redis_masters`. Ensure these instances are physically distinct and independent.
affects: All versions
gotchaThe Redlock algorithm, and thus this library, does not guarantee that a lock holder will finish its critical section before the lock expires. It's crucial to ensure your critical operations complete well within the `lock_ttl` or implement a mechanism to periodically extend the lock if necessary (though Redlock doesn't natively support easy extensions without re-acquiring).
fix
Estimate the maximum time your critical section will take and set `lock_ttl` accordingly, with a safety margin. If operations can exceed this, consider breaking them down or finding alternative coordination mechanisms.
affects: All versions
Upgrade
Version history
1.2.0latest on PyPI · released Dec 21, 2015
Audit
Dependencies
redisrequiredRequired for Redis client communication.
Agent activity
6 hits · last 30 days
node
6
Resources
redlock — pip install redlock · libregistry