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.

databasedevops
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.

ESM-only since v3; CommonJS require will fail with ERR_REQUIRE_ESM.

import { doWithRedisLock } from 'do-with-redis-lock'

This package only exports a named function; default import is undefined.

import { doWithRedisLock } from 'do-with-redis-lock'

Type exports are available; import type for compile-time only.

import type { RedisLockOptions } from 'do-with-redis-lock'

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 footguns
breakingv3 drops Node <18 and switches to ESM-only.
deprecatedOptions `timeout` and `maxTries` renamed to `ttl` and `retryCount` in v3.
gotchaIf the lock is not acquired within retries, it throws an error (no fallback). The library does not handle renewals or crash recovery.
gotchaThe Redis client must be connected before calling doWithRedisLock; no auto-connect.
deprecatedPackage previously exported `acquireLock` and `releaseLock` helpers in v2; removed in v3.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
8 hits · last 30 days
gptbot
3
ahrefsbot
1
Resources