Distributed locks for Rust with multiple backend support.
This crate provides distributed synchronization primitives (mutex locks, reader-writer locks, semaphores) that work across processes and machines. Multiple backend implementations are available: PostgreSQL, Redis, and file system.
Quick Start
use *;
use Duration;
async
Backends
File System Backend
Uses OS-level file locking. Simple and requires no external services.
use FileLockProvider;
let provider = builder
.build?;
PostgreSQL Backend
Uses PostgreSQL advisory locks. Production-ready with connection pooling.
use PostgresLockProvider;
let provider = builder
.connection_string
.build
.await?;
Redis Backend
Uses Redis with RedLock algorithm for multi-server deployments. Supports semaphores and automatic lease extension.
use RedisLockProvider;
let provider = builder
.add_server
.build
.await?;
Features
- Exclusive Locks: Mutual exclusion across processes
- Reader-Writer Locks: Multiple readers or single writer
- Semaphores: Limit concurrent access to N processes
- Async/Await: Full async support with tokio
- Backend Agnostic: Swap backends without changing application code
- Handle Loss Detection: Detect when locks are lost due to connection issues
Crate Organization
This is a meta-crate that re-exports types from:
distributed-lock-core: Core traits and typesdistributed-lock-file: File system backenddistributed-lock-postgres: PostgreSQL backenddistributed-lock-redis: Redis backend
For fine-grained control, you can depend on individual crates instead.