Class: RedisMutexDriver
The Redis driver ({type: 'redis'}): the locks are Redis keys holding their holder's token, shared by every instance connected to the same server (or cluster) with the same prefix. It needs the optional peer dependency redis (4.6 or 5); without it, the first acquisition fails with MutexDriverNotFoundException.
The keys expire after their ttl unless renewed (PEXPIRE), and are released and renewed only for their holder's token. Each acquisition increments a fencing token kept in a second key (<key>:fence, never expiring); the lock's name is the key's hash tag, so both keys stay in the same slot of a cluster. Configure the server with maxmemory-policy noeviction: an evicted key is a lock lost. The locks of one primary are lost if it fails over before replicating them.
The client connects at the first use. While the server cannot be reached, the calls wait for it to come back instead of failing (the client retries without limit): an acquisition does not end at its timeout, and the renewals do not complete, so the held locks are not marked lost while their keys may expire in Redis.
Example
import {RedisMutexDriver, type DriverLock} from 'lakutata/com/mutex'
const driver: RedisMutexDriver = new RedisMutexDriver({url: 'redis://127.0.0.1:6379'}, 'my-app:')
const lock: DriverLock | undefined = await driver.acquire('invoice:7', 30000)
if (lock) {
//Increases with each acquisition: written along with the data to refuse an older holder's writes
console.log(lock.fence)
await driver.release('invoice:7', lock)
}
await driver.close()Extends
Constructors
Constructor
new RedisMutexDriver(
connection,prefix):RedisMutexDriver
Create a driver on a Redis server; it connects at its first use.
Parameters
connection
The connection to the server.
prefix
string
The prefix of the keys.
Returns
RedisMutexDriver
Overrides
Properties
expiring
readonlyexpiring:boolean
true: the locks expire after their ttl unless renewed.
Overrides
onLost
onLost: (
names) =>void
Called with the names of the locks the driver lost at once, when the connection holding advisory locks closes or fails. A Mutex sets it; it does nothing by default.
Parameters
names
string[]
Returns
void
Inherited from
Methods
acquire()
acquire(
name,ttl):Promise<DriverLock|undefined>
Try to acquire a lock once: set its key when it does not exist, with a fencing token incremented.
Parameters
name
string
The lock name.
ttl
number
How long the lock stays held without a renewal, in milliseconds (rounded up).
Returns
Promise<DriverLock | undefined>
The lock with its fencing token, or undefined when another holder has it.
Throws
When the package redis is not installed.
Overrides
close()
close():
Promise<void>
Close the connection (QUIT, or a disconnection when it fails); the keys stay until they expire.
Returns
Promise<void>
Overrides
extend()
extend(
name,lock,ttl):Promise<boolean>
Renew a lock whose key still holds this holder's token, for ttl milliseconds from now.
Parameters
name
string
The lock name.
lock
The lock acquired.
ttl
number
How long the lock stays held without another renewal, in milliseconds (rounded up).
Returns
Promise<boolean>
Whether it was renewed (false when the key expired or was taken).
Overrides
release()
release(
name,lock):Promise<boolean>
Release a lock by deleting its key, when it still holds this holder's token.
Parameters
name
string
The lock name.
lock
The lock acquired.
Returns
Promise<boolean>
Whether it was released (false when the key expired or was taken).