{"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":"python","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":[{"wrong":"from redlock import Redlock","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,"search_vec":"'1.2.0':42 'acquir':24 'across':28 'activ':57 'algorithm':12 'api':22 'cadenc':46 'client':3 'concurr':63 'current':39 'develop':58 'distribut':14,36,62 'ensur':33 'frequent':51 'implement':8 'instanc':31 'less':56 'librari':59 'lock':15,27,61 'multipl':29 'provid':19 'python':2,7 'redi':17,30,60 'redlock':1,4,11,64 'releas':26,45 'reliabl':34 'simpl':21 'stabl':54 'suggest':52 'system':37 'version':40","created_at":"2026-04-16T18:49:22.104552+00:00","updated_at":"2026-04-16T18:49:22.104552+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.2.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/glasslion/redlock","docs":null,"changelog":null,"pypi":"https://pypi.org/project/redlock/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":null}}