{"id":9275,"library":"redlock","title":"Redlock Python Client","description":"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.","status":"active","version":"1.2.0","language":"en","source_language":"en","source_url":"https://github.com/glasslion/redlock","tags":["redis","lock","distributed","concurrency","redlock"],"install":[{"cmd":"pip install redlock","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for Redis client communication.","package":"redis","optional":false}],"imports":[{"symbol":"Redlock","correct":"from redlock import Redlock"}],"quickstart":{"code":"import redis\nfrom redlock import Redlock\nimport os\n\n# Configure Redis instances. For production, use multiple instances\n# and ensure they are properly configured for high availability.\n# Here, we default to localhost for a runnable example.\nredis_host = os.environ.get('REDIS_HOST', 'localhost')\nredis_port = int(os.environ.get('REDIS_PORT', 6379))\n\nredis_masters = [\n    {\"host\": redis_host, \"port\": redis_port, \"db\": 0}\n    # For true distributed locking, add more Redis instances:\n    # {\"host\": \"192.168.1.30\", \"port\": 6379, \"db\": 0},\n    # {\"host\": \"192.168.1.31\", \"port\": 6379, \"db\": 0}\n]\n\n# Initialize Redlock with Redis instances\ndlm = Redlock(redis_masters=redis_masters)\n\nresource_name = \"my_critical_resource\"\nlock_ttl = 5000  # Lock validity time in milliseconds (5 seconds)\n\nprint(f\"Attempting to acquire lock for resource: {resource_name}\")\nlock_info = dlm.lock(resource_name, lock_ttl)\n\nif lock_info:\n    print(f\"Successfully acquired lock: {lock_info}\")\n    try:\n        # --- Critical section begins ---\n        print(\"Performing critical operation...\")\n        # Simulate work\n        import time\n        time.sleep(2) # Keep operation less than lock_ttl\n        # --- Critical section ends ---\n    finally:\n        print(\"Releasing lock...\")\n        dlm.unlock(lock_info)\n        print(\"Lock released.\")\nelse:\n    print(f\"Failed to acquire lock for resource: {resource_name}. It might be held by another process.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Always provide the `ttl` value in milliseconds. E.g., for a 5-second lock, use `5000`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Provide `Redlock` with a list of at least 3 Redis master instances in `redis_masters`. Ensure these instances are physically distinct and independent.","message":"To 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure your Redis server is running and accessible from the machine running your Python application. Verify host and port configurations.","cause":"The Redis server is not running or is not accessible at the specified host and port.","error":"redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused."},{"fix":"Add the `ttl` argument, specifying the lock duration in milliseconds. E.g., `dlm.lock(resource_name, 5000)`.","cause":"You called the `lock()` method without providing the `ttl` (Time-To-Live) argument, which specifies how long the lock should be valid in milliseconds.","error":"TypeError: lock() missing 1 required positional argument: 'ttl'"},{"fix":"For true distributed locking, configure Redlock with multiple Redis master instances. Increase `lock_ttl` to ensure your critical section completes before the lock expires. Regularly check server clock synchronization.","cause":"This typically happens if you are using only one Redis instance, or if your `lock_ttl` is too short for the operation, allowing the lock to expire and be re-acquired by another client. Clock drift across servers can also contribute.","error":"Acquired lock for resource: my_critical_resource ... then another process also acquires it shortly after."}]}