redis-sp
v1.3.2
Published
Redis synchronization primitives based on redlock algorithm
Downloads
619
Readme
redis-sp
Redis SP (SP stands for Synchronization Primitives) is a set of synchronization primitives based on the Redlock algorithm.
Installation
npm install redis-sp
Usage
Mutex
Mutex implementation based on the Redlock algorithm
import RedisClient from 'ioredis';
import { RedisMutex } from 'redis-sp';
async function main() {
const client = new RedisClient();
const mutex = new RedisMutex([client], getResourceIdSomehow());
await mutex.lock();
try {
// critical section of your code
await maybeFails();
} finally {
await mutex.unlock();
}
}
Counting Semaphore
Implementation of a counting semaphore according to the Fair semaphores | Redis with a slight difference (no race conditions due to the atomicity of Redis Lua scripts).
import RedisClient from 'ioredis';
import { RedisCountingSemaphore } from 'redis-sp';
async function main() {
const client = new RedisClient();
const semaphore = new RedisCountingSemaphore(
[client],
getSharedResourceIdSomehow(),
getMaxSharedResourceOwnersSomehow(),
);
await semaphore.acquire();
try {
// critical section of your code
await maybeFails();
} finally {
await semaphore.release();
}
}
Stay tuned for RW lock in the next major release!