ioredis-lock v4.0.0 provides distributed locking for Node.js using Redis and the ioredis client. It uses Lua scripts for atomic acquire (SET key uuid PX timeout NX) and release operations, avoiding race conditions inherent in SETNX or WATCH/MULTI strategies. Requires Redis >= 2.6.12 and peer dependency ioredis ~4.x.x. Key features: configurable timeout, retries, delay; supports promises via bluebird; includes custom error types for acquisition, release, and extension failures. Compared to alternatives like redis-redlock or node-redlock, ioredis-lock is lighter and tightly coupled with ioredis, but does not implement the full Redlock algorithm for multi-master setups.
npm install ioredis-lockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates creating a distributed lock with ioredis-lock, acquiring, performing critical work, and releasing with error handling for lock acquisition and release failures.
Use ioredis v4.x with ioredis-lock v4. For ioredis v5, consider using an alternative like redlock.
Always set an appropriate timeout matching your critical section duration. Use lock.extend(time) to prolong if needed.
Handle extend failures manually: catch LockExtendError and decide whether to retry or abort.
Use async/await syntax with try/catch. The library returns native promises when used with async functions.
Always release.lock on the same lock object instance that acquired it. Do not share lock objects across connections.
Increase timeout, retries, or delay in createLock options. Check for stale locks previously not released.
Use: import { createLock } from 'ioredis-lock'; then call createLock(client, options).Ensure you only release locks you own. Use try/catch around release() and handle expiration gracefully.
Run npm install ioredis-lock. For ESM, ensure package.json has "type": "module" or use .mjs extension.