A Redis-based binary semaphore implementation using pub/sub for fast async wait, unlike polling-based alternatives. Version 2.2.2 is the latest stable release. Key differentiator: uses Redis pub/sub to notify waiters when a lock is released, making it much faster than polling-based locks. Supports Node >=7.6.0. Ideal for coordinating access to shared resources (e.g., token refresh) across multiple processes or servers.
npm install redfourNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to create a Lock instance, acquire a lock with a TTL, perform work, and release it.
Reuse an existing Redis client instance by passing it to the constructor.
Ensure TTL is in milliseconds (e.g., 60000 for 60 seconds).
Pass timeout in milliseconds. For 10 seconds, use 10000.
Upgrade Node.js to >=7.6.0.
Implement application-level timeouts or use Redis connection retry strategies.
const lock = new Lock({ redis: '...', namespace: 'myapp' });Use import Lock from 'redfour'; (default import) or const Lock = require('redfour') in CommonJS.Ensure lock is still valid before releasing, or handle the error gracefully.
Increase timeout value or ensure locks are released promptly.