You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+98-26Lines changed: 98 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,28 @@
1
-
# Redis Cluster Pipeline Library
1
+
# Redis Cluster Pipeline Library with <imgsrc="./images/pw.jpg"alt="drawing"width="30"/>
2
2
3
-
## Overview
3
+
This is a custom wrapper around the [ioredis](https://www.npmjs.com/package/ioredis) that adds a new method `clusterPipeline` to handle Redis Cluster pipelines more efficiently.
4
4
5
-
This is a custom wrapper library around the popular ioredis client that adds a new method called `clusterPipeline` to handle Redis Cluster pipelines more efficiently. This library was developed by the PhysicsWallah Private Limited tech team to solve issues with executing multiple Redis commands across Redis Cluster nodes.
5
+
## Why This Library?
6
+
7
+
### Problem at PhysicsWallah
8
+
9
+
At PhysicsWallah, we needed an efficient way to handle Redis Cluster commands when multiple Redis nodes were involved. The standard Redis pipeline approach didn't take slot distribution into account, leading to inefficiencies and complexity. We developed this library to:
10
+
11
+
- Simplify the execution of multiple Redis commands across a Redis Cluster.
12
+
- Maintain the order of results while executing commands in parallel.
13
+
- Make our Redis Cluster usage more efficient and less error-prone.
6
14
7
-
## Problem Statement
15
+
## Limitations of ioredis in Clustered Redis Topology
8
16
9
-
While using Redis Cluster with the ioredis client, handling multiple commands in a way that correctly distributes them across nodes while maintaining the order of execution was challenging. To address this, we developed this library that adds the `clusterPipeline` method, making it easy to manage Redis commands in a Redis Cluster environment.
17
+
As stated in the official [ioredis documentation](https://github.com/redis/ioredis?tab=readme-ov-file#transaction-and-pipeline-in-cluster-mode):
10
18
11
-
This library is simply a wrapper around ioredis, extending it with an additional method that enables Redis Cluster commands to be executed in parallel, with the results returned in the correct order.
19
+
> "All keys in a pipeline should belong to slots served by the same node, since ioredis sends all commands in a pipeline to the same node."
20
+
21
+
This means that when executing multiple commands in a pipeline, all keys must belong to the same Redis slot, as `ioredis` sends the pipeline commands to a single node. If the keys interact with different shards, pipeline commands would fail with error `All the keys in a pipeline command should belong to the same slot`
22
+
23
+
Due to this limitation, pipelining cannot be reliably used in Redis clusters with ioredis, leading to increased network latency as each command is executed individually instead of being batched together.
24
+
25
+
Our custom library resolves this issue by enabling efficient and reliable pipelining support in clustered Redis environments, improving performance and reducing latency.
12
26
13
27
## Features
14
28
@@ -22,40 +36,40 @@ This library is simply a wrapper around ioredis, extending it with an additional
22
36
To install the library, use npm or yarn to add it to your project.
23
37
24
38
```bash
25
-
npm install @pw-tech/ioredis
39
+
npm install @pw-tech/pw-redis
26
40
```
27
41
28
42
Or with yarn:
29
43
30
44
```bash
31
-
yarn add @pw-tech/ioredis
45
+
yarn add @pw-tech/pw-redis
32
46
```
33
47
34
48
## Usage
35
49
36
50
After installing, you can use the Cluster class just as you would with ioredis, but with the added functionality of the `clusterPipeline` method for Redis Cluster commands.
-`commands`: An array of Redis commands, where each command is represented as an array with the command name as the first element, followed by its respective arguments.
`clusterPipeline` takes an array of Redis commands (e.g., [['set', 'key', 'value'], ['get', 'key']]).
77
-
It automatically divides the commands across the Redis Cluster nodes based on the slot of the key.
78
-
It executes the commands in parallel and ensures the results are returned in the same order as the original commands.
108
+
The `clusterPipeline` method takes an array of Redis commands (e.g., [['set', 'key', 'value'], ['get', 'key']]) and automatically distributes them across the appropriate Redis Cluster nodes based on key slots. It executes the commands in parallel, ensuring that the results are returned in the same order as the input commands.
79
109
80
110
## How it Works
81
111
112
+
-**Slot Calculation**: Redis Cluster distributes keys across multiple nodes using hash slots. The library automatically determines the correct node for each key and groups commands accordingly, using the Redis `SHARDS` method to map and store the slot-to-node mapping in memory. In case of any slot changes in Redis, detected through error handling, the slot ranges are refreshed dynamically.
113
+
-**Pipeline Execution**: Once the commands are grouped by node, they are executed in parallel, improving performance when processing large sets of commands.
114
+
-**Result Handling**: The results from each node are collected and returned in the same order as the original set of commands, ensuring consistency and ease of use.
115
+
116
+
82
117
-**Slot Calculation**: Redis Cluster splits keys across multiple nodes using slots. This library automatically calculates which node each key belongs to and groups the commands accordingly.
83
118
-**Pipeline Execution**: Once the commands are grouped by node, they are executed in parallel on each node, ensuring better performance when dealing with large pipelines.
84
119
-**Result Handling**: Results from each node are merged and returned in the same order as the original commands.
@@ -115,6 +150,43 @@ At PhysicsWallah Private Limited, we needed an efficient way to handle Redis Clu
115
150
116
151
By adding the `clusterPipeline` method, we ensure commands are distributed to the appropriate Redis node, executed in parallel, and results are returned in the correct order. This approach eliminates the need to manually manage slot calculations and node selection.
117
152
153
+
## Performance Improvement in Production
154
+
155
+
After implementing partial pipeline optimization, we observed significant API response time improvements in production. Some queries had to remain sequential due to business logic dependencies, but wherever possible, parallel and pipeline execution were combined.
0 commit comments