Skip to main content

Redlock-py

PyPI version CI License: MIT

A production-grade, type-safe, and modern implementation of the Redis Redlock algorithm in Python. Supports both Synchronous and Asynchronous (asyncio) execution.


🏗 Architecture

The Redlock client manages connections to N independent Redis instances. To acquire a lock, it attempts to set a unique token on all instances sequentially. The lock is considered acquired only if:

  1. Quorum (N/2 + 1) instances allow the lock.
  2. Total time taken is less than the lock validity time.
sequenceDiagram
    participant Client
    participant R1 as Redis 1
    participant R2 as Redis 2
    participant R3 as Redis 3 (Quorum)
    participant R4 as Redis 4 
    participant R5 as Redis 5

    Client->>R1: SET resource unique_token NX PX 10000
    R1-->>Client: OK
    Client->>R2: SET resource unique_token NX PX 10000
    R2-->>Client: OK
    Client->>R3: SET resource unique_token NX PX 10000
    R3-->>Client: OK
    
    Note over Client: Quorum reached (3/5)
    
    Client-->>Client: Calculate Drift & Validity
    
    alt Success
        Client->>Client: Return Valid Lock
    else Failure (timeout or no quorum)
        Client->>R1: DEL resource (Unlock)
        Client->>R2: DEL resource (Unlock)
        Client->>R3: DEL resource (Unlock)
    end

🚀 Features

  • Dual Interface: Complete support for Redlock (blocking/sync) and AsyncRedlock (async/await).
  • Type Safe: 100% typed with mypy --strict.
  • Fault Tolerant: Resilient to single node failures.
  • Production Ready: Includes Jitter, Drift calculation, and proper error handling.

📦 Installation

pip install redlock-py
# or with poetry
poetry add redlock-py

💻 Usage

Synchronous

from redlock import Redlock, RedlockConfig

# Configure with list of Redis URIs
config = RedlockConfig(
    masters=[
        "redis://localhost:6379/0",
        "redis://localhost:6380/0",
        "redis://localhost:6381/0",
    ]
)

lock_manager = Redlock(config)

# 1. Standard usage (Non-blocking)
try:
    with lock_manager.lock("my-resource", ttl=10000) as lock:
        if lock.valid:
            print(f"Lock acquired: {lock.resource}")
            # Do critical work...
        else:
            print("Failed to acquire lock")
except Exception:
    pass

# 2. Blocking usage (Wait indefinitely until acquired)
with lock_manager.lock("my-resource", ttl=10000, blocking=True) as lock:
    print("Locked! This might have waited.")

# 3. Explicit Token Unlock (e.g. from a different process)
lock_manager.unlock("my-resource", "previous-token-uuid")

Asynchronous

import asyncio
from redlock import AsyncRedlock, RedlockConfig

async def main():
    config = RedlockConfig(
        masters=[
            "redis://localhost:6379/0",
            "redis://localhost:6380/0",
            "redis://localhost:6381/0",
        ]
    )
    
    lock_manager = AsyncRedlock(config)

    async with lock_manager.lock("my-async-resource", ttl=10000) as lock:
        if lock.valid:
            print("Async lock acquired!")
            await asyncio.sleep(1)

if __name__ == "__main__":
    asyncio.run(main())

🧪 Testing

This project uses a rigorous testing strategy including:

  1. Unit Tests: Mocked Redis interactions.
  2. Integration Tests: Docker-based tests against 5 real Redis nodes.
  3. Fault Injection: Network partition simulations using Toxiproxy.

To run tests:

docker-compose up -d
pytest

Metadata

Release files for redlock-ng 2.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redlock-ng 2.0.4
File Size Uploaded Provenance Actions
redlock_ng-2.0.4.tar.gz 6.7 kB With provenance Details for redlock_ng-2.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for redlock-ng 2.0.4
File Interpreter ABI Platform Actions
redlock_ng-2.0.4-py3-none-any.whl Python 3 none any Details for redlock_ng-2.0.4-py3-none-any.whl

Total release size: 14.6 kB

Release files / redlock_ng-2.0.4.tar.gz

Download URL redlock_ng-2.0.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
78f8db4735302009d5e3f06a9b8ce47ea261bebe271bff4c48b0e68bc6e2cb8a
BLAKE2b-256 checksum
How to use checksums
862ea622706f2f9a3f0530d413317c562b153b027f3879e2952be3b69b35c3f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 17, 2026.

Transparency log

Release files / redlock_ng-2.0.4-py3-none-any.whl

Download URL redlock_ng-2.0.4-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f7919c29acc3e1f04df83198b89e5470978022003151536fc2163b9b195b2cd
BLAKE2b-256 checksum
How to use checksums
4507bd38f2925f069e55b7d8ce573d027fb9a6d3d8bd281ce764d4e65f3d99c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 17, 2026.

Transparency log

Security for redlock-ng 2.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for redlock_ng-2.0.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.4 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page