Skip to main content

Simple distributed lock daemon over TCP

Project description

distlockd

distlockd is a lightweight, zero-dependency distributed lock server implemented in Python. It enables multiple clients to coordinate access to shared resources using named locks over a simple TCP protocol. Built for simplicity, stability, and ease of integration, distlockd is ideal for teams who need distributed locking without the operational overhead of Redis or other heavyweight systems.

Architecture & Protocol

  • Server: Asyncio-based TCP server, in-memory lock management, auto-timeout for stale locks (default: 10s), minimal resource usage.
  • Client: Synchronous Python client, context manager support, unique client IDs, retry and timeout mechanisms.
  • Protocol: Fast custom binary protocol inspired by RESP (used in Redis). This protocol minimizes parsing overhead and enables sub-millisecond lock operations, making it ideal for high-throughput, low-latency distributed coordination.
  • Design Philosophy: Minimalism, reliability, and maintainability. No persistence, no external databases, no complex configuration.

Features

  • Named distributed locks (multiple resources, independent lock names)
  • Simple TCP-based binary protocol
  • Auto-timeout of stale locks (prevents deadlocks)
  • Asyncio-based scalable server
  • Synchronous, easy-to-use Python client
  • Context-manager support for safe lock handling
  • Health check endpoint for monitoring
  • Configurable timeouts and retry logic
  • No external dependencies (no Redis, no databases)
  • Structured error handling and logging
  • Lightweight and easy to deploy
  • Connection pool for efficient connection management

Installation

pip install distlockd

Targeted Use Cases

  • Distributed cron jobs: Ensure only one worker runs a scheduled task at a time across multiple hosts.
  • Leader election: Elect a leader in a cluster for coordination tasks.
  • Resource coordination: Prevent race conditions when accessing shared files, APIs, or other resources.
  • Testing and CI: Synchronize test runners or deployment steps in distributed pipelines.
  • Lightweight alternatives to Redis locks: When you want distributed locking but find Redis too heavy or complex.
  • Temporary critical sections: Where lock persistence is not required and simplicity is key.

Usage

Starting the Server

# Basic usage (default host: 127.0.0.1, port: 9999)
distlockd server

# With custom host and port
distlockd server --host 127.0.0.1 --port 9999

# Enable verbose logging
distlockd server -v

Running the Benchmark

distlockd test distlockd|redis

# With custom host and port
distlockd test distlockd|redis --host 127.0.0.1 --port 9999

# With custom iterations, num_clients, num_locks, and throughput_seconds
distlockd test distlockd|redis --iterations 1000 --num_clients 1000 --num_locks 100 --throughput_seconds 10

Client Examples

Basic Usage

from distlockd.client import Client

# Create a Basic client
client = Client() # default host: 127.0.0.1, port: 9999, specified host and port: Client(host="192.xx.xx.xx", port=9999)

# Create a Client with custom timeout and retry logic
client = Client(timeout=5.0, retry=3)

# Check server health
if client.check_server_health():
    print("Server is healthy!")

Manual Lock Management

# Manual lock acquisition and release
if client.acquire("my-resource", timeout=5.0):
    try:
        print("Lock acquired successfully")
        # Do critical work here...
    finally:
        if client.release("my-resource"):
            print("Lock released successfully")

Using Context Manager

# Using context manager for automatic lock release
try:
    with client.lock("shared-resource", timeout=3.0):
        print("Lock acquired via context manager")
        # Do critical work here...
    # Lock is automatically released when the block exits
    print("Lock automatically released")
except Exception as e:
    print(f"Failed to acquire lock: {e}")

Error Handling

Handle common exceptions:

from distlockd.exceptions import LockAcquisitionTimeout, LockReleaseError, ConnectionError

try:
    with client.lock("resource"):
        # Critical section
        pass
except LockAcquisitionTimeout:
    print("Failed to acquire lock: timeout")
except LockReleaseError as e:
    print(f"Error releasing lock: {e}")
except ConnectionError as e:
    print(f"Connection error: {e}")

Benchmark Comparison Report

Date: 2025-05-28 11:10:51

System Info:

  • Platform: Linux
  • Platform-Release: 4.18.0-553.5.1.el8_10.x86_64
  • Platform-Version: #1 SMP Tue May 21 03:13:04 EDT 2024
  • Machine: x86_64
  • Processor: x86_64
  • CPU Count: 8
  • RAM (GB): 15.39
  • Uname:
    • system: Linux
    • release: 4.18.0-553.5.1.el8_10.x86_64
    • version: #1 SMP Tue May 21 03:13:04 EDT 2024
    • machine: x86_64
    • processor: x86_64

Test Configuration:

  • iterations: 1000
  • num_clients: 1000
  • num_locks: 100
  • throughput_seconds: 10

Methodology

  • Both servers were tested using equivalent client logic and lock contention scenarios.
  • Each test reports average, median, and worst-case latency, as well as throughput.
  • Tests were run on the same hardware/network for fairness.

Results (Summary)

  • The following results were obtained using the configuration below:

    • iterations: 1000
    • num_clients: 1000
    • num_locks: 100
    • throughput_seconds: 10
    Metric distlockd Redis
    Latency Min (ms) 0.07Runner-up 0.05 🏆 Winner
    Latency Max (ms) 1.01 🏆 Winner 4.66Runner-up
    Latency Avg (ms) 0.08Runner-up 0.07 🏆 Winner
    95th Percentile (ms) 0.10Runner-up 0.08 🏆 Winner
    99th Percentile (ms) 0.12Runner-up 0.11 🏆 Winner
    Throughput (ops/sec) 5200.00Runner-up 5710.00 🏆 Winner
    Concurrency Success Rate (%) 100.00 100.00
  • distlockd achieves sub-millisecond lock operations in most cases, with competitive or superior maximum latency compared to Redis. Throughput and concurrency success rates are robust even under heavy load.

Acknowledgements

Thanks to Windsurf for providing the benchmarking automation and comparison summary in this project.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

distlockd-1.0.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

distlockd-1.0.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file distlockd-1.0.0.tar.gz.

File metadata

  • Download URL: distlockd-1.0.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for distlockd-1.0.0.tar.gz
Algorithm Hash digest
SHA256 68c6fe6b15bec873d10d8ff35a8e56316cede13f6265a429cce5d97843d6c8a4
MD5 b2e6d2727c80e6bf3901754d73d6d7dd
BLAKE2b-256 0e4bbe6dca04d6b5c0a89c84128cd38e446ab5e96ecf93644dac305dc2856bea

See more details on using hashes here.

Provenance

The following attestation bundles were made for distlockd-1.0.0.tar.gz:

Publisher: python-publish.yml on anandan-bs/distlockd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file distlockd-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: distlockd-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for distlockd-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1c486c70f1b31dbbcdb29f235143ef86227c17862f77d6b4d14312781cd6c8a
MD5 9f306b8f5e4975d495b427682e4a184a
BLAKE2b-256 a15755788fab6fa44fba8ab7e91aed18026111920f79e1eb223646e27119ccca

See more details on using hashes here.

Provenance

The following attestation bundles were made for distlockd-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on anandan-bs/distlockd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page