rslock - Redlock for Redis in Rust
This is an implementation of Redlock, the distributed locking mechanism built on top of Redis.
Features
- Lock extending
- Smol and Tokio Redis I/O backends
- Async Redis
- Redis Cluster support
Install
[!WARNING] Before release
1.0.0, this crate will have breaking changes between minor versions. You can upgrade to patch versions without worrying about breaking changes.
# It is recommended to pin the version to a minor release, as breaking changes may be introduced between minor versions before 1.0.0.
[!NOTE] The default
async-std-compfeature uses Redis's Smol backend with Rustls. The feature name is retained for compatibility. For standalone Redis, you can select Redis's Tokio backend instead by disabling the default features:
Build
cargo build --release
Usage
The example below uses Tokio. Select the tokio-comp backend as shown above and add
Tokio to your application:
use LockManager;
use Duration;
async
More examples:
- Basic locking
- Creating a manager from Redis clients
- Sharing a lock between tasks
- Using Redis Cluster
Redis Cluster
Enable the cluster feature and pass one or more seed-node URIs for one logical
cluster. The cluster counts as one backend when rslock calculates quorum:
For authentication, TLS, address mapping, and other advanced configuration, build a
redis::cluster::ClusterClient and pass it to LockManager::from_cluster_client.
Extending Locks
Extending a lock effectively renews its duration instead of adding extra time to it. For instance, if a 1000ms lock is extended by 1000ms after 500ms pass, it will only last for a total of 1500ms, not 2000ms. This approach is consistent with the Node.js Redlock implementation. See the extend script.
Tests
The integration tests use Testcontainers and require Docker. Run both the all-features and default-feature test configurations:
Running the examples
Start the standalone Redis servers used by the first three examples:
Run the standalone Redis examples:
Stop the redis servers:
To run the Redis Cluster example, first start a Redis Cluster with seed nodes on ports 7000–7002, then run:
Contribute
If you find bugs or want to help otherwise, please open an issue.
License
BSD. See LICENSE.