Skip to content

Commit 05d7ef8

Browse files
DOC-4942 added more items and separated tables
1 parent 635ac84 commit 05d7ef8

File tree

2 files changed

+147
-6
lines changed

2 files changed

+147
-6
lines changed

content/develop/clients/nodejs/connect.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ createClient({
6767
```
6868
To check if the client is connected and ready to send commands, use `client.isReady`, which returns a Boolean. `client.isOpen` is also available. This returns `true` when the client's underlying socket is open, and `false` when it isn't (for example, when the client is still connecting or reconnecting after a network error).
6969

70-
### Connect to a Redis cluster
70+
## Connect to a Redis cluster
7171

7272
To connect to a Redis cluster, use `createCluster`.
7373

@@ -97,7 +97,7 @@ console.log(value); // returns 'bar'
9797
await cluster.quit();
9898
```
9999

100-
### Connect to your production Redis with TLS
100+
## Connect to your production Redis with TLS
101101

102102
When you deploy your application, use TLS and follow the [Redis security]({{< relref "/operate/oss_and_stack/management/security/" >}}) guidelines.
103103

@@ -127,3 +127,77 @@ await client.disconnect();
127127
```
128128

129129
You can also use discrete parameters and UNIX sockets. Details can be found in the [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md).
130+
131+
## Reconnect after disconnection
132+
133+
By default, `node-redis` doesn't attempt to reconnect automatically when
134+
the connection to the server is lost. However, you can set the
135+
`socket.reconnectionStrategy` field in the configuration to decide
136+
whether to try to reconnect and how to approach it. Choose one of the following values for
137+
`socket.reconnectionStrategy`:
138+
139+
- `false`: (Default) Don't attempt to reconnect.
140+
- `number`: Wait for this number of milliseconds and then attempt to reconnect.
141+
- `<function>`: Use a custom
142+
function to decide how to handle reconnection.
143+
144+
The custom function has the following signature:
145+
146+
```js
147+
(retries: number, cause: Error) => false | number | Error
148+
```
149+
150+
It is called before each attempt to reconnect, with the `retries`
151+
indicating how many attempts have been made so far. The `cause` parameter is an
152+
[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
153+
object with information about how the connection was lost. The return value
154+
from the function can be any of the following:
155+
156+
- `false`: Don't attempt to reconnect.
157+
- `number`: Wait this number of milliseconds and then try again.
158+
- `Error`: Same as `false`, but lets you supply extra information about why
159+
no attempt was made to reconnect.
160+
161+
The example below shows a `reconnectionStrategy` function that implements a
162+
custom [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
163+
strategy:
164+
165+
```js
166+
createClient({
167+
socket: {
168+
reconnectStrategy: retries => {
169+
// Generate a random jitter between 0 – 200 ms:
170+
const jitter = Math.floor(Math.random() * 200);
171+
172+
// Delay is an exponential back off, (times^2) * 50 ms, with a
173+
// maximum value of 2000 ms:
174+
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
175+
176+
return delay + jitter;
177+
}
178+
}
179+
});
180+
```
181+
182+
## Connection events
183+
184+
The client object emits the following
185+
[events](https://developer.mozilla.org/en-US/docs/Web/API/Event) that are
186+
related to connection:
187+
188+
- `connect`: (No parameters) The client is about to start connecting to the server.
189+
- `ready`: (No parameters) The client has connected and is ready to use.
190+
- `end`: (No parameters) The client has been intentionally closed using `client.quit()`.
191+
- `error`: An error has occurred, which is described by the
192+
[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
193+
parameter. This is usually a network issue such as "Socket closed unexpectedly".
194+
- `reconnecting`: (No parameters) The client is about to try reconnecting after the
195+
connection was lost due to an error.
196+
197+
Use code like the following to respond to these events:
198+
199+
```js
200+
client.on('error', error => {
201+
console.error(`Redis client error:`, error);
202+
});
203+
```

content/develop/clients/nodejs/migration.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,85 @@ outlines the main similarities and differences between the two libraries.
2323
You may find this information useful if you are an `ioredis` user and you want to
2424
start a new Node.js project or migrate an existing `ioredis` project to `node-redis`
2525

26+
## Comparison of `ioredis` and `node-redis`
27+
2628
The table below summarizes how `ioredis` and `node-redis` implement some
2729
key features of Redis. See the following sections for more information about
2830
each feature.
2931

32+
### Connection
33+
34+
| Feature | `ioredis` | `node-redis` |
35+
| :-- | :-- | :-- |
36+
| [Initial connection](#initial-connection) | Happens when you create a client instance | Requires you to call a method on the client instance |
37+
| [Reconnection after a connection is lost](#reconnection) | Automatic by default | Manual by default |
38+
| [Connection events](#connection-events) | Emits `connect`, `ready`, `error`, and `close` events. | Emits `connect`, `ready`, `error`, `end`, and `reconnecting` events. |
39+
40+
### Command handling
41+
3042
| Feature | `ioredis` | `node-redis` |
3143
| :-- | :-- | :-- |
3244
| [Command case](#command-case) | Lowercase only (eg, `hset`) | Uppercase or camel case (eg, `HSET` or `hSet`) |
3345
| [Command argument handling](#command-argument-handling) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list |
3446
| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises only |
47+
48+
### Specific commands
49+
50+
| Feature | `ioredis` | `node-redis` |
51+
| :-- | :-- | :-- |
52+
53+
54+
### Techniques
55+
56+
| Feature | `ioredis` | `node-redis` |
57+
| :-- | :-- | :-- |
3558
| [Pipelining](#pipelining) | Automatic, or with `pipeline()` command | Automatic, or with `multi()` command |
3659

37-
## Command case
60+
## Details
61+
62+
The sections below explain the points of comparison between `ioredis` and
63+
`node-redis` in more detail.
64+
65+
### Initial connection
66+
67+
`ioredis` makes the connection to the Redis server when you create an instance
68+
of the client object:
69+
70+
```js
71+
const Redis = require('ioredis');
72+
73+
// Connects to localhost:6379 on instantiation.
74+
const redis = new Redis();
75+
```
76+
77+
`node-redis` requires you to call the `connect()` method on the client object
78+
to make the connection:
79+
80+
```js
81+
import { createClient } from 'redis';
82+
83+
const client = await createClient();
84+
await client.connect(); // Requires explicit connection.
85+
```
86+
87+
### Reconnection after a connection is lost {#reconnection}
88+
89+
`ioredis` automatically attempts to reconnect if the connection
90+
was lost due to an error. By default, `node-redis` doesn't attempt
91+
to reconnect, but you can enable a custom reconnection strategy
92+
when you create the client object. See
93+
[Reconnect after disconnection]({{< relref "/develop/clients/nodejs/connect#reconnect-after-disconnection" >}})
94+
for more information.
95+
96+
### Connection events
97+
98+
The `connect`, `ready`, `error`, and `close` events that `ioredis` emits
99+
are equivalent to the `connect`, `ready`, `error`, and `end` events
100+
in `node-redis`, but `node-redis` also emits a `reconnecting` event.
101+
See []({{< relref "/develop/clients/nodejs/connect#connection-events" >}})
102+
for more information.
103+
104+
### Command case
38105

39106
Command methods in `ioredis` are always lowercase. With `node-redis`, you can
40107
use uppercase or camel case versions of the method names.
@@ -50,7 +117,7 @@ redis.HSET("key", "field", "value");
50117
redis.hSet("key", "field", "value");
51118
```
52119

53-
## Command argument handling
120+
### Command argument handling
54121

55122
`ioredis` parses command arguments to strings and then passes them to
56123
the server, in a similar way to [`redis-cli`]({{< relref "/develop/tools/cli" >}}).
@@ -86,7 +153,7 @@ the method arguments you pass:
86153
redis.set("bike:5", "bike", {EX: 10});
87154
```
88155

89-
## Asynchronous command result handling {#async-result}
156+
### Asynchronous command result handling {#async-result}
90157

91158
All commands for both `ioredis` and `node-redis` are executed
92159
asynchronously. `ioredis` supports both callbacks and
@@ -119,7 +186,7 @@ you must always use a `then()` handler or the
119186
[`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
120187
operator to receive them.
121188

122-
## Pipelining
189+
### Pipelining
123190

124191
Both `ioredis` and `node-redis` will pipeline commands automatically if
125192
they are executed in the same "tick" of the

0 commit comments

Comments
 (0)