Registry / storage / redis-lock

redis-lock

JSON →
library1.0.0jsnpmunverified

Redis-based distributed locking primitive for Node.js, implementing the algorithm described in the Redis SETNX documentation. Fully non-blocking and asynchronous, designed for concurrency control in distributed systems. Current stable version: 1.0.0. Requires node-redis v4 (breaking change from earlier versions). Retry delay and timeout configurable. Lightweight with minimal dependencies. Used in production at errorception.

npm install redis-lock
INSTALL
IMPORT
SIG · REDIS-LOCK
R
redis-lock
storagejavascriptv1.0.0
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 (initializer)
const lock = require('redis-lock')(client);
const lock = require('redis-lock')(client, 10); // second param optional, not wrong per se
redis-lock exports a function that takes a redis client (and optional retryDelay) and returns the lock function.
lock (acquisition)
const done = await lock('myLock');
lock('myLock', callback); // Promises only; no callback support
Returns a promise that resolves to a done function. The done function is also async.
release lock
await done();
done(); // missing await may cause race conditions
done() must be awaited or used as a promise to ensure lock release completes.

Demonstrates acquiring a lock, performing async work, and releasing the lock with redis-lock using node-redis v4.

const client = require('redis').createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); const lock = require('redis-lock')(client); async function main() { await client.connect(); console.log('Asking for lock'); const done = await lock('myLock', 5000); console.log('Lock acquired'); // Simulate async work await new Promise(resolve => setTimeout(resolve, 1000)); console.log('Releasing lock'); await done(); console.log('Lock released'); await client.quit(); } main().catch(err => console.error(err));
Debug
Known issues
breakingRequires node-redis v4. The client must be connected (await client.connect()) before locking. Using with older node-redis (v3) will cause errors.
fix
Upgrade to node-redis v4 and ensure client.connect() is called before any lock() call.
affects: >=1.0.0
gotchaThe lock is automatically released after `timeout` ms (default 5000). Holding the lock longer is not possible; the timeout is a safety mechanism.
fix
If you need longer locks, increase the timeout value passed to lock() or ensure your work completes within the timeout.
affects: >=0.1.0
gotchaIf the Redis server connection is lost or the script crashes, the lock may not be released until the timeout expires. This can cause deadlocks in critical sections.
fix
Ensure robust error handling and consider using a more sophisticated locking mechanism like Redlock for production critical paths.
affects: >=0.1.0
deprecatedThe package's GitHub repository (errorception/redis-lock) shows no recent activity. The API is stable but the package is not actively maintained.
fix
Consider using actively maintained alternatives like redlock or ioredis-based locks.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: lock is not a function
Called require('redis-lock')(client) incorrectly; missing parentheses or wrong function reference.
fix
Ensure you call require('redis-lock') with the client: const lock = require('redis-lock')(client);
Error: The client is closed
Trying to acquire lock before connecting the redis client (node-redis v4 requirement).
fix
Add await client.connect(); before any lock() call.
TimeoutError: Lock 'myLock' could not be acquired within timeout
Another process holds the lock longer than the retry delay or the lock's timeout is too short.
fix
Increase the lock timeout or retry delay (second argument to initializer). Ensure the other process releases the lock promptly.
ReferenceError: done is not defined
Not awaiting the lock() promise correctly; const done = await lock('name') is needed.
fix
Use const done = await lock('myLock'); then await done(); to release.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
redisrequiredPeer dependency; redis-lock requires a node-redis client instance to operate
Agent activity
35 hits · last 30 days
node
30
Amazon
1
OpenAI (training)
1
Resources
redis-lock — npm install redis-lock · libregistry