An open API service indexing awesome lists of open source software.

https://github.com/siarheidudko/redux-cluster

A modern TypeScript library for synchronizing Redux stores across multiple processes and machines using TCP, Unix Domain Sockets, and IPC.
https://github.com/siarheidudko/redux-cluster

cluster ipc node-cluster nodejs nodejs-server redux

Last synced: 29 days ago
JSON representation

A modern TypeScript library for synchronizing Redux stores across multiple processes and machines using TCP, Unix Domain Sockets, and IPC.

Awesome Lists containing this project

README

          

# Redux Cluster 2.0

[![npm](https://img.shields.io/npm/v/redux-cluster.svg)](https://www.npmjs.com/package/redux-cluster)
[![npm](https://img.shields.io/npm/dy/redux-cluster.svg)](https://www.npmjs.com/package/redux-cluster)
[![NpmLicense](https://img.shields.io/npm/l/redux-cluster.svg)](https://www.npmjs.com/package/redux-cluster)
![GitHub last commit](https://img.shields.io/github/last-commit/siarheidudko/redux-cluster.svg)
![GitHub release](https://img.shields.io/github/release/siarheidudko/redux-cluster.svg)

A modern TypeScript library for synchronizing Redux stores across multiple processes and machines using TCP, Unix Domain Sockets, and IPC.

> ๐ŸŒ **Need WebSocket support for browsers?** Check out [redux-cluster-ws](https://www.npmjs.com/package/redux-cluster-ws) - our companion package that extends Redux Cluster with WebSocket transport for browser clients.

## ๐ŸŒŸ Key Features

- ๐Ÿ”„ **Real-time State Synchronization** across multiple processes/machines
- ๐ŸŒ **Multiple Transport Options**: TCP, Unix Domain Sockets, IPC
- ๐ŸŒ **WebSocket Support**: Available via [redux-cluster-ws](https://www.npmjs.com/package/redux-cluster-ws)
- ๐Ÿ“ก **Bidirectional Communication** - any node can dispatch actions
- ๐Ÿ”’ **Built-in Security** with authentication and IP banning
- โšก **High Performance** with optimized networking and compression
- ๐Ÿ—๏ธ **Master-Slave Architecture** with automatic leader election
- ๐Ÿ”ง **TypeScript First** with comprehensive type definitions
- ๐ŸŽฏ **Redux Compatible** - works with existing Redux ecosystem

## ๐Ÿ›๏ธ Architecture Overview

Redux Cluster implements a master-slave architecture where one server manages the authoritative state and distributes updates to all connected clients:

```ascii
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Redux Cluster Network โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” TCP/Socket โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Client A โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ Server โ”‚ โ”‚
โ”‚ โ”‚ (Worker) โ”‚ โ”‚ (Master) โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” TCP/Socket โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ Client B โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ โ”‚ โ”‚
โ”‚ โ”‚ (Worker) โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” TCP/Socket โ”‚ โ”‚
โ”‚ โ”‚ Client C โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ (Worker) โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Data Flow

```ascii
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 1. Action โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 2. Process โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Client โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ Server โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ Redux โ”‚
โ”‚ โ”‚ โ”‚ (Master) โ”‚ โ”‚ Store โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ฒ โ”‚ โ”‚
โ”‚ โ–ผ โ”‚
โ”‚ 4. State Update โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 3. State Changed โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚ Broadcast โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ Engine โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ All Clients โ”‚
โ”‚ (Auto-sync) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿš€ Quick Start

### 1. Installation

```bash
npm install redux-cluster redux
```

### 2. Basic TCP Example

**Server (Master):**

```javascript
const { createStore } = require('redux-cluster');

// Simple counter reducer
const counterReducer = (state = { counter: 0 }, action) => {
switch (action.type) {
case 'INCREMENT': return { counter: state.counter + 1 };
case 'DECREMENT': return { counter: state.counter - 1 };
default: return state;
}
};

// Create store and server
const store = createStore(counterReducer);
const server = store.createServer({ port: 8080 });

console.log('Server started on port 8080');

// Server can dispatch actions
store.dispatch({ type: 'INCREMENT' });
```

**Client (Worker):**

```javascript
const { createStore } = require('redux-cluster');

const store = createStore(counterReducer);
const client = store.createClient({
host: 'localhost',
port: 8080
});

// Client receives all state updates automatically
store.subscribe(() => {
console.log('New state:', store.getState());
});

// Client can also dispatch actions
store.dispatch({ type: 'INCREMENT' });
```

## ๐ŸŒ Transport Options

Redux Cluster supports multiple transport mechanisms:

### TCP (Network)

```javascript
// Server
const server = store.createServer({
host: 'localhost',
port: 8080
});

// Client
const client = store.createClient({
host: 'localhost',
port: 8080
});
```

### Unix Domain Sockets (Local)

```javascript
// Server
const server = store.createServer({
path: '/tmp/redux-cluster.sock'
});

// Client
const client = store.createClient({
path: '/tmp/redux-cluster.sock'
});
```

### IPC (Node.js Cluster)

```javascript
import cluster from 'cluster';

if (cluster.isMaster) {
const store = createStore(reducer);
cluster.fork(); // Start worker
} else {
const store = createStore(reducer);
// IPC automatically enabled in cluster workers
}
```

## ๐Ÿ”ง Configuration Options

### Server Configuration

```typescript
const server = store.createServer({
host: 'localhost', // TCP host
port: 8080, // TCP port
path: '/tmp/app.sock', // Unix socket path
logins: { // Authentication
'user1': 'password1',
'user2': 'password2'
}
});
```

### Client Configuration

```typescript
const client = store.createClient({
host: 'localhost', // TCP host
port: 8080, // TCP port
path: '/tmp/app.sock', // Unix socket path
login: 'user1', // Authentication
password: 'password1'
});
```

### Store Configuration

```typescript
const store = createStore(reducer, {
mode: 'action', // 'action' | 'snapshot'
serializationMode: 'json', // 'json' | 'protoobject'
debug: false, // Enable debug logging
resync: 30000 // Resync interval (ms)
});
```

## ๐Ÿ“Š Synchronization Modes

### Action Mode (Default)

Actions are distributed and replayed on all nodes:

```ascii
Client A: dispatch(ACTION) โ”€โ”€โ–บ Server โ”€โ”€โ–บ broadcast(ACTION) โ”€โ”€โ–บ All Clients
โ”‚
โ–ผ
Apply ACTION to master state
```

### Snapshot Mode

Complete state snapshots are distributed:

```ascii
Client A: dispatch(ACTION) โ”€โ”€โ–บ Server โ”€โ”€โ–บ calculate new state โ”€โ”€โ–บ broadcast(STATE) โ”€โ”€โ–บ All Clients
```

## ๐Ÿ”’ Security Features

### Authentication

```javascript
const server = store.createServer({
logins: {
'api-service': 'secret-key-123',
'worker-pool': 'another-secret'
}
});

const client = store.createClient({
login: 'api-service',
password: 'secret-key-123'
});
```

### IP Banning

Automatic IP banning after failed authentication attempts:

- 5+ failed attempts = 3 hour ban
- Automatic cleanup of expired bans
- Configurable ban policies

## ๐ŸŽฎ Examples

See the [examples/](./examples/) directory for complete working examples:

- **[TCP Transport](./examples/tcp/)** - Network communication
- **[File Socket](./examples/file-socket/)** - Local IPC via Unix sockets
- **[Basic Store](./examples/basic/)** - Local Redux store without networking

> ๐ŸŒ **WebSocket Examples**: For browser integration examples with WebSocket transport, visit the [redux-cluster-ws examples](https://github.com/siarheidudko/redux-cluster-ws/tree/main/examples).

Each example includes a README with step-by-step instructions.

## ๐Ÿ“ฆ Related Packages

### redux-cluster-ws

WebSocket transport layer for Redux Cluster, enabling browser client support:

```bash
npm install redux-cluster-ws
```

**Features:**

- ๐ŸŒ WebSocket server and client
- ๐Ÿ”— Seamless integration with Redux Cluster
- ๐Ÿ–ฅ๏ธ Browser support for web applications
- ๐Ÿ“ฑ Real-time state synchronization to browsers
- ๐Ÿ”’ Same security features as core package

**Links:**

- ๐Ÿ“‹ [NPM Package](https://www.npmjs.com/package/redux-cluster-ws)
- ๐Ÿ“– [Documentation](https://github.com/siarheidudko/redux-cluster-ws)
- ๐ŸŽฏ [Examples](https://github.com/siarheidudko/redux-cluster-ws/tree/main/examples)

## ๐Ÿงช Testing

```bash
# Run all tests
npm test

# Run specific test suites
npm run test:unit # Unit tests
npm run test:transport # Transport integration tests

# Build and test
npm run build
npm run lint

# Run full integration tests (includes Docker)
npm run test:integration-full
```

## ๐Ÿ“ˆ Performance

Redux Cluster is optimized for high-throughput scenarios:

- **Compression**: gzip compression for all network traffic
- **Binary Protocol**: Efficient binary serialization options
- **Connection Pooling**: Reuse connections where possible
- **Minimal Overhead**: < 1ms latency for local sockets

Benchmark results:

- TCP: ~10,000 actions/sec
- Unix Sockets: ~50,000 actions/sec
- IPC: ~100,000 actions/sec

## ๐Ÿ—บ๏ธ Roadmap

- [ ] **Redis Transport** - Redis pub/sub for clustering
- [x] **WebSocket Transport** - Available in [redux-cluster-ws](https://www.npmjs.com/package/redux-cluster-ws)
- [ ] **Conflict Resolution** - CRDT-based conflict resolution
- [ ] **Persistence Layer** - Automatic state persistence
- [ ] **Monitoring Dashboard** - Real-time cluster monitoring
- [ ] **Load Balancing** - Multiple master support

## ๐Ÿค Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

### Development Setup

```bash
git clone https://github.com/siarheidudko/redux-cluster.git
cd redux-cluster
npm install
npm run build
npm test
```

## ๐Ÿ“„ License

MIT License - see [LICENSE](./LICENSE) file for details.

## ๐Ÿ†˜ Support

- ๐Ÿ“ **Issues**: [GitHub Issues](https://github.com/siarheidudko/redux-cluster/issues)
- ๐Ÿ’ฌ **Discussions**: [GitHub Discussions](https://github.com/siarheidudko/redux-cluster/discussions)
- ๐Ÿ“ง **Email**: [siarhei@dudko.dev](mailto:siarhei@dudko.dev)

## ๐Ÿ’ Support This Project

If Redux Cluster helps you build amazing applications, consider supporting its development:

- โ˜• **[Buy me a coffee](https://www.buymeacoffee.com/dudko.dev)**
- ๐Ÿ’ณ **[PayPal](https://paypal.me/dudkodev)**
- ๐ŸŽฏ **[Patreon](https://patreon.com/dudko_dev)**
- ๐ŸŒ **[More options](http://dudko.dev/donate)**

Your support helps maintain and improve Redux Cluster for the entire community!

---

**Made with โค๏ธ by [Siarhei Dudko](https://github.com/siarheidudko)**