Registry / database / do-with-redis-lock

do-with-redis-lock

JSON →
library3.2.0jsnpmunverified

This is do-with-redis-lock version 3.2.0, a minimal, promisified library for distributed locking with Redis. It provides a simple `doWithRedisLock` function that acquires a lock via Redis SET NX PX and releases it after the callback resolves. Unlike heavyweight options (e.g., Redlock), this package has zero peer dependencies, no external lock manager, and focuses on a single atomic operation. Releases are infrequent but stable; the current major version (v3) dropped support for Node <18 and switched to ESM-only exports. Key differentiator: simplicity — no retry strategies, no lock extension — just a promise-based lock around an async function.

npm install do-with-redis-lock
INSTALL
IMPORT
SIG · DO-WITH-REDIS-LOCK
D
do-with-redis-lock
databasejavascriptv3.2.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.

doWithRedisLock
import { doWithRedisLock } from 'do-with-redis-lock'
const doWithRedisLock = require('do-with-redis-lock')
ESM-only since v3; CommonJS require will fail with ERR_REQUIRE_ESM.
default import (not available)
import { doWithRedisLock } from 'do-with-redis-lock'
import doWithRedisLock from 'do-with-redis-lock'
This package only exports a named function; default import is undefined.
TypeScript types
import type { RedisLockOptions } from 'do-with-redis-lock'
Type exports are available; import type for compile-time only.

Connects to Redis, acquires a distributed lock for `criticalTask`, and releases after completion. Demonstrates options: ttl, retryDelay, retryCount.

import { createClient } from 'redis'; import { doWithRedisLock } from 'do-with-redis-lock'; const client = createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); await client.connect(); async function criticalTask() { // This function will execute only if lock is acquired console.log('Doing critical work...'); await new Promise(resolve => setTimeout(resolve, 1000)); return 'result'; } // Use a unique lock key try { const result = await doWithRedisLock(client, 'my-lock-key', criticalTask, { ttl: 5000, // lock TTL in ms retryDelay: 200, // retry interval retryCount: 10 // max retries (0 = no retry) }); console.log('Result:', result); } catch (err) { console.error('Lock acquisition failed or task threw:', err); } finally { await client.quit(); }
Debug
Known issues
breakingv3 drops Node <18 and switches to ESM-only.
fix
Upgrade Node to >=18 and use ESM imports (import syntax). If you need CommonJS, stay on v2.
affects: >=3.0.0
deprecatedOptions `timeout` and `maxTries` renamed to `ttl` and `retryCount` in v3.
fix
Use `ttl` instead of `timeout`, and `retryCount` instead of `maxTries`.
affects: >=3.0.0
gotchaIf the lock is not acquired within retries, it throws an error (no fallback). The library does not handle renewals or crash recovery.
fix
Implement your own retry/fallback logic around the call if the lock acquisition fails.
affects: >=1.0.0
gotchaThe Redis client must be connected before calling doWithRedisLock; no auto-connect.
fix
Call `client.connect()` before using the function.
affects: >=1.0.0
deprecatedPackage previously exported `acquireLock` and `releaseLock` helpers in v2; removed in v3.
fix
Use `doWithRedisLock` for all locking operations. If you need lower-level control, stay on v2.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: doWithRedisLock is not a function
Using default import instead of named import (common mistake).
fix
Use `import { doWithRedisLock } from 'do-with-redis-lock'` (curly braces).
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Using CommonJS require() with this ESM-only package (v3+).
fix
Switch to `import` syntax or use dynamic import: `const { doWithRedisLock } = await import('do-with-redis-lock')`.
ReplyError: ERR wrong number of arguments for 'set' command
Using an older Redis server that doesn't support SET with NX and PX (Redis <2.6.12).
fix
Upgrade Redis server to >=2.6.12.
Error: Lock not acquired after 10 retries
The lock key is held by another process; retryCount exhausted.
fix
Increase retryCount or ttl, or implement a backoff strategy externally.
Upgrade
Version history
3.2.0latest on npm
Audit
Dependencies
redisrequiredRedis client instance required to run commands; the user must pass a connected client when calling the function.
Agent activity
7 hits · last 30 days
node
6
Resources
do-with-redis-lock — npm install do-with-redis-lock · libregistry