#redis #distributed-lock #locks #lock-files #instance #set #multiple

rdlock

A library for distributed redis locks written in rust

4 stable releases

1.0.3 Oct 25, 2024
1.0.1 Oct 24, 2024
0.1.0 Oct 24, 2024

#371 in Database interfaces

Download history 393/week @ 2024-10-20 33/week @ 2024-10-27

426 downloads per month

MIT license

25KB
363 lines

rdlock

Crates.io License: MIT

A library for distributed redis locks written in rust.

These are some useful synchronization primitives which work among multiple separate processes, servers and clusters, as long as they communicate with the same redis server.

The redis instance must have notify-keyspace-events enabled with at least KA flags. If this is too much overhead for your current instance, consider running a separate redis instance just for the distributed locks.

Usage and examples on docs.rs

Supported primitives

  • BasicLock - a basic type of distributed lock with no protected data. This is the most basic synchronization primitive.

TLDR

It uses redis SET with the NX option enabled for creating a "lockfile".

Any lock function that waits for the lock to become available (e.g: .lock() or .lock_timeout()) will use redis pubsub in order to monitor for changes and try to acquire the "lockfile" once it is available.

Known issues

  • The local TTL check, which checks if the TTL of the specific lock expired before doing any extra actions, is not perfect, since it's only a local sanity TTL check, and doesn't verify TTL in any way with the redis instance.
  • If the pubsub drops while waiting for a lock and/or we miss a message, we will never acquire it, since redis pubsub is fire and forget.

TODO

  • Move generic lock functions that will be used in all locks out of src/lock/mod.rs

Types of locks we want implemented:

  • Mutex
  • RwLock

Footnotes

Please note this is a work in progress and for starters I will be implementing only what I need or want to have fun with. If there is a certain synchronization primitive that is not available and you need it or you want to implement it for fun, contributions are always welcome :)

Dependencies

~8–17MB
~224K SLoC