{"id":4234,"library":"redlock-py","title":"redlock-py","description":"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.","status":"active","version":"1.0.8","language":"en","source_language":"en","source_url":"https://github.com/SPSCommerce/identity-service","tags":["redis","distributed lock","concurrency","locking"],"install":[{"cmd":"pip install redlock-py redis","lang":"bash","label":"Install redlock-py and Redis client"}],"dependencies":[{"reason":"Requires a Redis client library (e.g., redis-py) to connect to Redis instances.","package":"redis","optional":false}],"imports":[{"symbol":"Redlock","correct":"from redlock import Redlock"}],"quickstart":{"code":"import os\nfrom redlock import Redlock\nimport redis\n\n# Configure Redis connection(s)\n# For a true Redlock setup, use multiple independent Redis instances.\n# For quickstart, a single local instance is shown.\nREDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')\nREDIS_PORT = int(os.environ.get('REDIS_PORT', 6379))\nREDIS_DB = int(os.environ.get('REDIS_DB', 0))\n\nredis_nodes = [\n    {\"host\": REDIS_HOST, \"port\": REDIS_PORT, \"db\": REDIS_DB}\n    # For production, add more independent Redis nodes, e.g.:\n    # {\"host\": \"redis2.example.com\", \"port\": 6379, \"db\": 0},\n    # {\"host\": \"redis3.example.com\", \"port\": 6379, \"db\": 0},\n]\n\n# Initialize the Redlock manager\ndlm = Redlock(redis_nodes)\n\nresource_name = \"my_critical_resource\"\nlock_validity_time_ms = 5000 # Lock will be valid for 5 seconds\n\ntry:\n    # Acquire the lock using a context manager (recommended)\n    print(f\"Attempting to acquire lock for '{resource_name}'...\")\n    with dlm.lock(resource_name, lock_validity_time_ms) as my_lock:\n        if my_lock:\n            print(f\"Lock '{resource_name}' acquired! (Validity: {my_lock.validity}ms)\")\n            # Your critical section code goes here\n            print(\"Executing critical section...\")\n            # Simulate work\n            import time\n            time.sleep(2)\n            print(\"Critical section complete.\")\n        else:\n            print(f\"Could not acquire lock for '{resource_name}'. Another process may hold it.\")\nexcept redis.exceptions.ConnectionError as e:\n    print(f\"Error connecting to Redis: {e}. Make sure Redis is running.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\nprint(\"Finished redlock-py quickstart example.\")","lang":"python","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `redis` is installed in your environment: `pip install redlock-py redis`.","message":"redlock-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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"For 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"redlock-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.","severity":"gotcha","affected_versions":"All 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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}