# ts-safe-redis-leader

> Redis leader election implementation that does not have any race conditions in Typescript

Latest version **0.0.5** (published 2023-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-safe-redis-leader
pnpm add ts-safe-redis-leader
yarn add ts-safe-redis-leader
bun add ts-safe-redis-leader
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2023-01-30 |
| First published | 2021-07-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 35.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Marc Gagnon |
| Maintainers | nesci |
| Keywords | redis, leader, elections, distributed |

## Links

- npm: https://www.npmjs.com/package/ts-safe-redis-leader
- Repository: https://github.com/Nesci28/safe-redis-leader
- Homepage: https://github.com/Nesci28/safe-redis-leader/blob/master/README.md
- Issues: https://github.com/Nesci28/safe-redis-leader/issues
- npm.io page: https://npm.io/package/ts-safe-redis-leader

## Dependencies (1)

- [ioredis](https://npm.io/package/ioredis.md) ^4.27.6

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2023-01-30
- 0.0.4 — 2021-07-15
- 0.0.3 — 2021-07-15
- 0.0.2 — 2021-07-15
- 0.0.1 — 2021-07-15

## README

# Safe Redis Leader

Fork of: [Safe Redis Leader](https://www.npmjs.com/package/safe-redis-leader)

1. Built with typescript
2. New tests running with Jest and testing multiple instances at the same time to make sure there is only 1 leader at all time
3. New event (main reason of this fork) 
  - After an election, all the instances will send an event either ```elected``` or ```notElected```

## 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](https://github.com/pierreinglebert/redis-leader) that implements a solution to the [known race condition](https://github.com/pierreinglebert/redis-leader/blob/c3b4db5df9802908728ad0ae4310a52e74acb462/index.js#L81). Additionally, this rewritten package:

1. Only exposes public api functions that should be exposed (no more public-but-should-be-private `_elect` fn)
2. has a test suite within docker-compose using a real redis instance, which allows anyone to run the tests with no heavy dependency setup
3. Replace callback-hell with async/await

## Usage

Install the package:

```bash
  npm install ts-safe-redis-leader
```

Exemple:
```typescript
  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

---
_Source: https://npm.io/package/ts-safe-redis-leader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
