Registry / storage / redis-sp

redis-sp

JSON →
library1.3.2jsnpmunverified

redis-sp (Synchronization Primitives) is a TypeScript library providing distributed mutex and counting semaphore implementations based on the Redlock algorithm. Version 1.3.2, released in 2021, is the latest stable release with no active development observed. It relies on ioredis clients and uses Redis Lua scripts for atomicity, avoiding race conditions common in other implementations. Differentiates from alternatives like 'redlock' by offering both mutex and semaphore primitives with TypeScript support.

npm install redis-sp
INSTALL
IMPORT
SIG · REDIS-SP
R
redis-sp
storagejavascriptv1.3.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RedisMutex
✓ import { RedisMutex } from 'redis-sp'
✗ const RedisMutex = require('redis-sp').RedisMutex
Library is ESM-native since v1; require() works in CommonJS but TypeScript users should use import.
RedisCountingSemaphore
✓ import { RedisCountingSemaphore } from 'redis-sp'
✗ import RedisCountingSemaphore from 'redis-sp'
Only named export; no default export exists.
LockAcquisitionError
✓ import { LockAcquisitionError } from 'redis-sp'
✗ import { LockError } from 'redis-sp'
Error class is named LockAcquisitionError, not LockError.

Demonstrates creating a RedisMutex, acquiring a lock, executing critical code, and releasing the lock with proper error handling.

import RedisClient from 'ioredis'; import { RedisMutex, LockAcquisitionError } from 'redis-sp'; async function main() { const client = new RedisClient({ host: process.env.REDIS_HOST ?? 'localhost', port: parseInt(process.env.REDIS_PORT ?? '6379'), }); const mutex = new RedisMutex([client], 'resource-lock-key'); try { await mutex.lock(); // critical section await new Promise(resolve => setTimeout(resolve, 1000)); } catch (err) { if (err instanceof LockAcquisitionError) { console.error('Could not acquire lock'); } else { throw err; } } finally { await mutex.unlock(); } client.quit(); } main().catch(console.error);
Debug
Known issues
breakingThe lock() method can throw LockAcquisitionError if lock acquisition fails (e.g., resource already locked).
fix
Wrap lock() in try-catch and handle LockAcquisitionError explicitly; do not assume immediate success.
affects: >=1.0.0
deprecatedNo new releases since 2021; library may not be actively maintained for newer Redis or Node.js versions.
fix
Consider alternative actively maintained libraries like 'redlock' for Redlock algorithm.
affects: >=1.0.0
gotchaThe mutex key must be unique per resource; reusing keys across unrelated sections leads to unintended contention.
fix
Ensure each critical section uses a distinct, namespaced resource identifier.
affects: >=1.0.0
gotchaRedisMutex requires an array of Redis clients even if using a single instance; passing a single client without array wrapper causes runtime error.
fix
Always pass clients as an array: new RedisMutex([client], key).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: client.sendCommand is not a function
Passed a non-ioredis client (e.g., redis v4 client) which does not have sendCommand method.
fix
Use ioredis client only; the library is not compatible with 'redis' v4+ client.
Error: LockAcquisitionError: lock not acquired
Cannot acquire lock within default retry attempts; resource held by another process.
fix
Increase retry options or ensure previous lock is released; use tryLock() with timeout.
TypeError: mutex.lock is not a function
Trying to use an older version (<1.0.0) where API was different, or using require() incorrectly.
fix
Update to v1.3.2 and use import { RedisMutex } from 'redis-sp'.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies
ioredisrequiredRequired to provide Redis clients for lock/semaphore operations.
Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources