@@ -94,7 +94,7 @@ await cluster.set('foo', 'bar');
94
94
const value = await cluster .get (' foo' );
95
95
console .log (value); // returns 'bar'
96
96
97
- await cluster .quit ();
97
+ await cluster .close ();
98
98
```
99
99
100
100
## Connect to your production Redis with TLS
@@ -123,15 +123,21 @@ await client.set('foo', 'bar');
123
123
const value = await client .get (' foo' );
124
124
console .log (value) // returns 'bar'
125
125
126
- await client .disconnect ();
126
+ await client .destroy ();
127
127
```
128
128
129
129
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
130
131
131
## Reconnect after disconnection
132
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
133
+ ` node-redis ` can attempt to reconnect automatically when
134
+ the connection to the server is lost. By default, it will retry
135
+ the connection using an
136
+ [ exponential backoff] ( https://en.wikipedia.org/wiki/Exponential_backoff )
137
+ strategy with some random "jitter" added to avoid multiple
138
+ clients retrying in sync with each other.
139
+
140
+ You can also set the
135
141
` socket.reconnectionStrategy ` field in the configuration to decide
136
142
whether to try to reconnect and how to approach it. Choose one of the following values for
137
143
` socket.reconnectionStrategy ` :
@@ -159,19 +165,18 @@ from the function can be any of the following:
159
165
no attempt was made to reconnect.
160
166
161
167
The example below shows a ` reconnectionStrategy ` function that implements a
162
- custom [ exponential backoff] ( https://en.wikipedia.org/wiki/Exponential_backoff )
163
- strategy:
168
+ custom exponential backoff strategy:
164
169
165
170
``` js
166
171
createClient ({
167
172
socket: {
168
173
reconnectStrategy : retries => {
169
- // Generate a random jitter between 0 – 200 ms:
170
- const jitter = Math .floor (Math .random () * 200 );
174
+ // Generate a random jitter between 0 – 100 ms:
175
+ const jitter = Math .floor (Math .random () * 100 );
171
176
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 );
177
+ // Delay is an exponential backoff , (2^retries ) * 50 ms, with a
178
+ // maximum value of 3000 ms:
179
+ const delay = Math .min (Math .pow (2 , retries) * 50 , 3000 );
175
180
176
181
return delay + jitter;
177
182
}
@@ -193,6 +198,12 @@ related to connection:
193
198
parameter. This is usually a network issue such as "Socket closed unexpectedly".
194
199
- ` reconnecting ` : (No parameters) The client is about to try reconnecting after the
195
200
connection was lost due to an error.
201
+ - ` sharded-channel-moved ` : The cluster slot of a subscribed
202
+ [ sharded pub/sub channel] ({{< relref "/develop/interact/pubsub#sharded-pubsub" >}})
203
+ has been moved to another shard. Note that when you use a
204
+ [ ` RedisCluster ` ] ( #connect-to-a-redis-cluster ) connection, this event is automatically
205
+ handled for you. See
206
+ [ ` sharded-channel-moved ` event] ( https://github.com/redis/node-redis/blob/master/docs/pub-sub.md#sharded-channel-moved-event ) for more information.
196
207
197
208
Use code like the following to respond to these events:
198
209
0 commit comments