A safe leader election implementation for Node.js using Redis, designed to eliminate race conditions present in the original 'redis-leader' package (v0.0.6, latest). It uses async/await, removes CommonJS callback patterns, and provides a factory function 'createSafeRedisLeader' instead of requiring 'new'. The library is a rewrite that fixes known bugs in prior implementations, includes a test suite with Docker Compose, and focuses on correctness without external dependencies beyond ioredis. It offers events like 'elected' and methods 'elect' and 'stop', suitable for distributed systems needing reliable single-leader guarantees.
npm install safe-redis-leaderNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a safe Redis leader election instance and starts the election process.
Use await createSafeRedisLeader(...) or .then() to get the leader instance.
Set TTL (e.g., 1500ms) less than wait (e.g., 3000ms) to allow re-election.
Upgrade to 0.0.6 or later.
Call elect() to begin the election process.
Implement retry logic externally or ensure Redis connection is stable.
Use named import: import { createSafeRedisLeader } from 'safe-redis-leader';Include 'key' in options object.
Use ioredis as the Redis client.
Ensure TTL < wait and Redis connection is valid.