Registry / storage / ts-safe-redis-leader

ts-safe-redis-leader

JSON →
library0.0.5jsnpmunverified

A TypeScript library for distributed Redis leader election with race-condition-free guarantees. Version 0.0.5 is the latest stable release; the project appears to be a fork of safe-redis-leader with TypeScript support, new events (elected/notElected), and tests verifying single-leader-at-a-time. Key differentiators include async/await API, removal of race conditions from the original redis-leader package, and Jest-based testing against a real Redis instance via Docker Compose. The library is small, focused, and suitable for Node.js applications needing reliable leader election without external dependencies beyond ioredis.

npm install ts-safe-redis-leader
INSTALL
IMPORT
SIG · TS-SAFE-REDIS-LEAD
T
ts-safe-redis-leader
storagejavascriptv0.0.5
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.

SafeRedisLeader
import { SafeRedisLeader } from 'ts-safe-redis-leader';
const SafeRedisLeader = require('ts-safe-redis-leader');
Package exports only SafeRedisLeader as a named export. Default import will not work.
SafeRedisLeader
import { SafeRedisLeader } from 'ts-safe-redis-leader';
import SafeRedisLeader from 'ts-safe-redis-leader';
Default import is incorrect; use named import.
SafeRedisLeader
const { SafeRedisLeader } = require('ts-safe-redis-leader');
const SafeRedisLeader = require('ts-safe-redis-leader').default;
CommonJS destructured require works; .default property is not available.

Creates a Redis client and SafeRedisLeader instance then starts leader election with event listeners for elected/notElected.

import Redis from 'ioredis'; import { SafeRedisLeader } from 'ts-safe-redis-leader'; const redisConfig = { port: 6379, host: 'localhost', autoResubscribe: false, lazyConnect: true, maxRetriesPerRequest: 0, }; const redisClient = new Redis(redisConfig); const leaderElectionKey = 'the-election'; const safeLeader = new SafeRedisLeader(redisClient, 1500, 3000, leaderElectionKey); safeLeader.on('elected', () => { console.log('I am the leader'); }); safeLeader.on('notElected', () => { console.log('I am not the leader'); }); async function start() { await safeLeader.elect(); } start().catch(console.error);
Debug
Known issues
gotchaRedis client must be configured with `lazyConnect: true` and `maxRetriesPerRequest: 0` to avoid connection issues.
fix
Set lazyConnect: true and maxRetriesPerRequest: 0 in ioredis options.
affects: >=0.0.0
gotchaThe library uses events 'elected' and 'notElected' (not 'leader' or 'follower').
fix
Listen for 'elected' and 'notElected' events instead of custom event names.
affects: >=0.0.0
breakingPackage is a fork; API differs from original safe-redis-leader (e.g., events renamed).
fix
Refer to ts-safe-redis-leader documentation for correct API.
affects: >=0.0.0
gotchaConstructor arguments: (redisClient, ttl, interval, key) - order matters and units are in milliseconds.
fix
Pass exactly (Redis client, ttl, interval, election key string).
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: redisClient.connect is not a function
Redis client is not connected or lazyConnect is not set to true.
fix
Ensure ioredis is initialized with { lazyConnect: true } and call connect() before elect() if needed.
Error: The leader election key is required
Election key was not provided or is undefined.
fix
Pass a non-empty string as the fourth argument to SafeRedisLeader constructor.
TypeError: safeLeader.on is not a function
Attempting to use event methods on a non-event-emitter instance, possibly due to wrong import.
fix
Import SafeRedisLeader as named export and ensure it is an instance of SafeRedisLeader.
Upgrade
Version history
0.0.5latest on npm
Audit
Dependencies
ioredisrequiredRequired for Redis client instance passed to SafeRedisLeader constructor
Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
2
Resources
ts-safe-redis-leader — npm install ts-safe-redis-leader · libregistry