Registry / database / node-redis-lock

node-redis-lock

JSON →
library1.0.1jsnpmunverified

This package provides a simple locking mechanism using Redis for Node.js applications. The current stable version is 1.0.1, released in 2012, and it is no longer maintained, with no releases in over a decade. Key features include acquire, renew, release, and check lock existence operations. The user must provide a custom value for the lock to ensure safe release by the owning host. It supports a configurable namespace and TTL but lacks promises or async/await support, being callback-based only. Unlike modern alternatives such as redlock (which offers distributed locks with automatic retry and fencing tokens), node-redis-lock is a minimal implementation intended for simpler use cases where strong consistency guarantees are not required.

npm install node-redis-lock
INSTALL
IMPORT
SIG · NODE-REDIS-LOCK
N
node-redis-lock
databasejavascriptv1.0.1
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.

Lock
const Lock = require('node-redis-lock');
import Lock from 'node-redis-lock';
The package uses CommonJS and does not support ES modules.
Lock
new Lock({namespace: 'myapp'}, redisClient);
new Lock(redisClient, {namespace: 'myapp'});
The options object must be the first argument, followed by the Redis client.
Lock methods (acquire, renew, release, isLocked)
lock.acquire(key, ttl, value, (err, result) => {});
lock.acquire(key, value, ttl, callback);
The argument order is (key, ttl, value, callback).

Acquires and releases a Redis lock with a custom value to ensure safe ownership.

const Lock = require('node-redis-lock'); const redis = require('redis'); const client = redis.createClient(); const lock = new Lock({namespace: 'example'}, client); const key = 'job1'; const value = 'my-host'; const ttl = 10; // seconds lock.acquire(key, ttl, value, (err, acquired) => { if (err) console.error(err); console.log(acquired ? 'Lock acquired' : 'Failed to acquire lock'); if (acquired) { lock.release(key, value, (err, released) => { if (err) console.error(err); console.log(released ? 'Lock released' : 'Failed to release'); client.quit(); }); } else { client.quit(); } });
Debug
Known issues
deprecatedThis package is unmaintained and has been deprecated. No new features or security updates are provided.
fix
Migrate to a maintained alternative like redlock.
affects: *
gotchaThe lock is not distributed-safe across multiple Redis instances or with replica failover. Using a single Redis instance can lead to split-brain or false locks.
fix
Use redlock which supports multiple Redis hosts and uses an algorithm that is safe under partial failure.
affects: *
gotchaThe acquire callback returns true even if the lock already exists? Undefined behavior: the implementation simply uses SETNX and returns the result. If lock is held, it returns false.
fix
Check the callback result (err, result) where result is false if lock not acquired.
affects: *
gotchaCallbacks do not follow Node.js error-first convention? They do follow (err, result).
fix
Always check err for connection issues, then check result for lock success.
affects: *
Errors
Common errors & fixes
TypeError: lock.acquire is not a function
Incorrect instantiation or import: Lock is a constructor, but calling Lock() without new will return undefined.
fix
Use new Lock(options, client); or ensure require returns the constructor.
Redis reply error: ERR Error running script
Lua script issues with newer Redis versions. The package uses EVALSHA and assumes a specific Redis version compatible with the script.
fix
Use an alternative package like redlock that has better Redis version compatibility.
Error: The connection is already closed.
Attempting operations after closing the Redis client.
fix
Ensure the Redis client is kept open for the duration of lock usage; close after releasing.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
redisrequiredA Redis client is required to interact with Redis.
Agent activity
5 hits · last 30 days
node
4
Resources
node-redis-lock — npm install node-redis-lock · libregistry