mirror of
https://github.com/lingble/safe-redis-leader.git
synced 2025-10-29 03:32:25 +00:00
4c6e730a9f635d75b492b8c591ce0079cd4f6aa0
Safe Redis Leader
Fork of: Safe Redis Leader
Goal
The Safe Redis Leader TS module is designed to provide a leader election implementation that provides tested gaurentees that there is only a single leader elected from a group of clients at one time.
The implementation is a port of the stale Redis Leader npm package that implements a solution to the known race condition. Additionally, this rewritten package:
- Only exposes public api functions that should be exposed (no more public-but-should-be-private
_electfn) - has a test suite within docker-compose using a real redis instance, which allows anyone to run the tests with no heavy dependency setup
- Has tests to assert the known race condition can no longer occur
- Replace callback-hell with async/await
- Built with typescript
Usage
Install the package:
npm install ts-safe-redis-leader
Exemple:
import * as Redis from "ioredis";
import { SafeRedisLeader } from "../src";
const redisConfig: Redis.RedisOptions = {
port: 6379,
host: "localhost",
autoResubscribe: false,
lazyConnect: true,
maxRetriesPerRequest: 0,
};
const redisClient = new Redis(redisConfig);
const leaderElectionKey = 'the-election';
const safeLeader = new SafeRedisLeader(redisClients[i], 1500, 3000, leaderElectionKey);
await safeLeader.elect();
License
MIT
Description
Languages
TypeScript
96.2%
Lua
3.8%