redis-sp
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

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!

Package Sidebar

Install

npm i redis-sp

Weekly Downloads

88

Version

1.3.2

License

MIT

Unpacked Size

73.4 kB

Total Files

81

Last publish

Collaborators

  • format-x