@@ -9,7 +9,7 @@ categories:
9
9
- oss
10
10
- kubernetes
11
11
- clients
12
- description : Learn the differences between `ioredis` and `node-redis`
12
+ description : Discover the differences between `ioredis` and `node-redis`.
13
13
linkTitle : Migrate from ioredis
14
14
title : Migrate from ioredis
15
15
weight : 6
@@ -21,11 +21,11 @@ but this library is now deprecated in favor of
21
21
[ ` node-redis ` ] ({{< relref "/develop/clients/nodejs" >}}). This guide
22
22
outlines the main similarities and differences between the two libraries.
23
23
You may find this information useful if you are an ` ioredis ` user and you want to
24
- start a new Node.js project or migrate an existing ` ioredis ` project to ` node-redis `
24
+ start a new Node.js project or migrate an existing ` ioredis ` project to ` node-redis ` .
25
25
26
26
## Comparison of ` ioredis ` and ` node-redis `
27
27
28
- The table below summarizes how ` ioredis ` and ` node-redis ` implement some
28
+ The tables below summarize how ` ioredis ` and ` node-redis ` implement some
29
29
key features of Redis. See the following sections for more information about
30
30
each feature.
31
31
@@ -35,7 +35,7 @@ each feature.
35
35
| :-- | :-- | :-- |
36
36
| [ Initial connection] ( #initial-connection ) | Happens when you create a client instance | Requires you to call a method on the client instance |
37
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. |
38
+ | [ Connection events] ( #connection-events ) | Emits ` connect ` , ` ready ` , ` error ` , and ` close ` events | Emits ` connect ` , ` ready ` , ` error ` , ` end ` , and ` reconnecting ` events |
39
39
40
40
### Command handling
41
41
@@ -44,20 +44,23 @@ each feature.
44
44
| [ Command case] ( #command-case ) | Lowercase only (eg, ` hset ` ) | Uppercase or camel case (eg, ` HSET ` or ` hSet ` ) |
45
45
| [ Command argument handling] ( #command-argument-handling ) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list |
46
46
| [ Asynchronous command result handling] ( #async-result ) | Callbacks and Promises | Promises only |
47
+ | [ Arbitrary command execution] ( #arbitrary-command-execution ) | Uses the ` call() ` method | Uses the ` sendCommand() ` method |
47
48
48
49
### Techniques
49
50
50
51
| Feature | ` ioredis ` | ` node-redis ` |
51
52
| :-- | :-- | :-- |
52
53
| [ Pipelining] ( #pipelining ) | Automatic, or with ` pipeline() ` command | Automatic, or with ` multi() ` command |
53
- | [ Scan iteration] ( #scan-iteration ) | Uses ` scanStream() ` , etc. | Uses ` scanIterator() ` , etc |
54
+ | [ Scan iteration] ( #scan-iteration ) | Uses ` scanStream() ` , etc | Uses ` scanIterator() ` , etc |
54
55
| [ Subscribing to channels] ( #subscribing-to-channels ) | Uses ` client.on('message', ...) ` event | Uses ` subscribe(...) ` command |
55
56
56
57
### Specific commands
57
58
58
59
| Command | ` ioredis ` | ` node-redis ` |
59
60
| :-- | :-- | :-- |
60
61
| [ ` SETNX ` ] ( #setnx-command ) | Supported explicitly | Supported as an option for ` SET ` |
62
+ | [ ` HMSET ` ] ( #hmset-command ) | Supported explicitly | Supported with standard ` HSET ` functionality |
63
+ | [ ` CONFIG ` ] ( #config-command ) | Supported explicitly | Supported with separate ` configGet() ` , ` configSet() ` , etc |co
61
64
62
65
## Details
63
66
@@ -70,10 +73,10 @@ The sections below explain the points of comparison between `ioredis` and
70
73
of the client object:
71
74
72
75
``` js
73
- const Redis = require (' ioredis' );
76
+ const client = require (' ioredis' );
74
77
75
78
// Connects to localhost:6379 on instantiation.
76
- const redis = new Redis ();
79
+ const client = new Redis ();
77
80
```
78
81
79
82
` node-redis ` requires you to call the ` connect() ` method on the client object
@@ -100,7 +103,7 @@ for more information.
100
103
The ` connect ` , ` ready ` , ` error ` , and ` close ` events that ` ioredis ` emits
101
104
are equivalent to the ` connect ` , ` ready ` , ` error ` , and ` end ` events
102
105
in ` node-redis ` , but ` node-redis ` also emits a ` reconnecting ` event.
103
- See [ ] ({{< relref "/develop/clients/nodejs/connect#connection-events" >}})
106
+ See [ Connection events ] ({{< relref "/develop/clients/nodejs/connect#connection-events" >}})
104
107
for more information.
105
108
106
109
### Command case
@@ -110,13 +113,13 @@ use uppercase or camel case versions of the method names.
110
113
111
114
``` js
112
115
// ioredis
113
- redis .hset (" key" , " field" , " value" );
116
+ client .hset (' key' , ' field' , ' value' );
114
117
115
118
// node-redis
116
- redis .HSET (" key" , " field" , " value" );
119
+ client .HSET (' key' , ' field' , ' value' );
117
120
118
121
// ...or
119
- redis .hSet (" key" , " field" , " value" );
122
+ client .hSet (' key' , ' field' , ' value' );
120
123
```
121
124
122
125
### Command argument handling
@@ -126,23 +129,23 @@ the server, in a similar way to [`redis-cli`]({{< relref "/develop/tools/cli" >}
126
129
127
130
``` js
128
131
// Equivalent to the command line `SET key 100 EX 10`.
129
- redis .set (" key" , 100 , " EX " , 10 );
132
+ client .set (' key' , 100 , ' EX ' , 10 );
130
133
```
131
134
132
135
Arrays passed as arguments are flattened into individual elements and
133
136
objects are flattened into sequential key-value pairs:
134
137
135
138
``` js
136
139
// These commands are all equivalent.
137
- redis .hset (" user" {
138
- name: " Bob" ,
140
+ client .hset (' user' {
141
+ name: ' Bob' ,
139
142
age: 20 ,
140
- description: " I am a programmer" ,
143
+ description: ' I am a programmer' ,
141
144
});
142
145
143
- redis .hset (" user" , [" name" , " Bob" , " age" , 20 , " description" , " I am a programmer" ]);
146
+ client .hset (' user' , [' name' , ' Bob' , ' age' , 20 , ' description' , ' I am a programmer' ]);
144
147
145
- redis .hset (" user" , " name" , " Bob" , " age" , 20 , " description" , " I am a programmer" );
148
+ client .hset (' user' , ' name' , ' Bob' , ' age' , 20 , ' description' , ' I am a programmer' );
146
149
```
147
150
148
151
` node-redis ` uses predefined formats for command arguments. These include specific
@@ -151,8 +154,8 @@ of the CLI command. Internally, `node-redis` constructs the correct command usin
151
154
the method arguments you pass:
152
155
153
156
``` js
154
- // Equivalent to the command line `SET key 100 EX 10`.
155
- redis .set (" bike:5" , " bike" , {EX : 10 });
157
+ // Equivalent to the command line `SET bike:5 bike EX 10`.
158
+ client .set (' bike:5' , ' bike' , {EX : 10 });
156
159
```
157
160
158
161
### Asynchronous command result handling {#async-result}
@@ -164,7 +167,7 @@ return values to respond to command results:
164
167
165
168
``` js
166
169
// Callback
167
- redis .get (" mykey" , (err , result ) => {
170
+ client .get (' mykey' , (err , result ) => {
168
171
if (err) {
169
172
console .error (err);
170
173
} else {
@@ -173,7 +176,7 @@ redis.get("mykey", (err, result) => {
173
176
});
174
177
175
178
// Promise
176
- redis .get (" mykey" ).then (
179
+ client .get (' mykey' ).then (
177
180
(result ) => {
178
181
console .log (result);
179
182
},
@@ -188,27 +191,54 @@ you must always use a `then()` handler or the
188
191
[ ` await ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await )
189
192
operator to receive them.
190
193
194
+ ### Arbitrary command execution
195
+
196
+ ` ioredis ` lets you issue arbitrary commands in a similar format to
197
+ [ ` redis-cli ` ] ({{< relref "/develop/tools/cli" >}}) using the ` call() `
198
+ command:
199
+
200
+ ``` js
201
+ await client .call (' JSON.SET' , ' doc' , " $" , ' {"f1": {"a":1}, "f2":{"a":2}}' );
202
+ ```
203
+
204
+ In ` node-redis ` , you can get the same effect outside a transaction using ` sendCommand() ` :
205
+
206
+ ``` js
207
+ await client .sendCommand ([' hset' , ' hash2' , ' number' , ' 3' ]);
208
+ ```
209
+
210
+ Within a transaction, use ` addCommand() ` to include arbitrary commands. Note that
211
+ you can freely mix ` addCommand() ` calls with standard commands in the same
212
+ transaction:
213
+
214
+ ``` js
215
+ const responses = await client .multi ()
216
+ .addCommand ([' hset' , ' hash3' , ' number' , ' 4' ])
217
+ .hGet (' hash3' , ' number' )
218
+ .exec ();
219
+ ```
220
+
191
221
### Pipelining
192
222
193
223
Both ` ioredis ` and ` node-redis ` will pipeline commands automatically if
194
224
they are executed in the same "tick" of the
195
225
[ event loop] ( https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#what-is-the-event-loop )
196
226
(see
197
- [ Pipelines and transactions ] ({{< relref "/develop/clients/nodejs/transpipe" >}})
227
+ [ Execute a pipeline ] ({{< relref "/develop/clients/nodejs/transpipe#execute-a-pipeline " >}})
198
228
for more information).
199
229
200
230
You can also create a pipeline with explicit commands in both clients.
201
- For ` ioredis ` , you use the ` pipeline() ` command with a chain of
231
+ With ` ioredis ` , you use the ` pipeline() ` command with a chain of
202
232
commands, ending with ` exec() ` to run the pipeline:
203
233
204
234
``` js
205
235
// ioredis example
206
- redis .pipeline ()
207
- .set (" foo" , " 1 " )
208
- .get (" foo" )
209
- .set (" foo" , " 2 " )
210
- .incr (" foo" )
211
- .get (" foo" )
236
+ client .pipeline ()
237
+ .set (' foo' , ' 1 ' )
238
+ .get (' foo' )
239
+ .set (' foo' , ' 2 ' )
240
+ .incr (' foo' )
241
+ .get (' foo' )
212
242
.exec (function (err , results ) {
213
243
// Handle results or errors.
214
244
});
@@ -218,7 +248,7 @@ For `node-redis`, the approach is similar, except that you call the `multi()`
218
248
command to start the pipeline and ` execAsPipeline() ` to run it:
219
249
220
250
``` js
221
- redis .multi ()
251
+ client .multi ()
222
252
.set (' seat:3' , ' #3' )
223
253
.set (' seat:4' , ' #4' )
224
254
.set (' seat:5' , ' #5' )
@@ -238,19 +268,19 @@ from the set of keys returned by the [`SCAN`]({{< relref "/commands/scan" >}})
238
268
command:
239
269
240
270
``` js
241
- const redis = new Redis ();
271
+ const client = new Redis ();
242
272
// Create a readable stream (object mode)
243
- const stream = redis .scanStream ();
244
- stream .on (" data" , (resultKeys ) => {
273
+ const stream = client .scanStream ();
274
+ stream .on (' data' , (resultKeys ) => {
245
275
// `resultKeys` is an array of strings representing key names.
246
276
// Note that resultKeys may contain 0 keys, and that it will sometimes
247
277
// contain duplicates due to SCAN's implementation in Redis.
248
278
for (let i = 0 ; i < resultKeys .length ; i++ ) {
249
279
console .log (resultKeys[i]);
250
280
}
251
281
});
252
- stream .on (" end" , () => {
253
- console .log (" all keys have been visited" );
282
+ stream .on (' end' , () => {
283
+ console .log (' all keys have been visited' );
254
284
});
255
285
```
256
286
@@ -268,6 +298,7 @@ other multi-key commands):
268
298
``` js
269
299
for await (const keys of client .scanIterator ()) {
270
300
const values = await client .mGet (keys);
301
+ // Process values...
271
302
}
272
303
```
273
304
@@ -285,7 +316,7 @@ client.on('message', (channel, message) => {
285
316
```
286
317
287
318
With ` node-redis ` , you use the ` subscribe() ` command to register the
288
- message callback. Also, when you use a connection to subscribe, the
319
+ message callback. Also, when you use a connection to subscribe, that
289
320
connection can't issue any other commands, so you must create a
290
321
dedicated connection for the subscription. Use the ` client.duplicate() `
291
322
method to create a new connection with the same settings as the original:
@@ -305,13 +336,40 @@ await subscriber.subscribe('channel', (message) => {
305
336
command with an explicit method:
306
337
307
338
``` js
308
- client .setnx (" bike:1" , " bike" );
339
+ client .setnx (' bike:1' , ' bike' );
309
340
```
310
341
311
342
` node-redis ` doesn't provide a ` SETNX ` method but implements the same
312
343
functionality with the ` NX ` option to the [ ` SET ` ] ({{< relref "/commands/set" >}})
313
344
command:
314
345
315
346
``` js
316
- await client .set (" bike:1" , " bike" , {' NX' : true });
347
+ await client .set (' bike:1' , ' bike' , {' NX' : true });
348
+ ```
349
+
350
+ ### ` HMSET ` command
351
+
352
+ The [ ` HMSET ` ] ({{< relref "/commands/hmset" >}}) command has been deprecated
353
+ since Redis v4.0.0, but it is still supported by ` ioredis ` . With ` node-redis `
354
+ you should use the [ ` HSET ` ] ({{< relref "/commands/hset" >}}) command with
355
+ multiple key-value pairs. See the [ ` HSET ` ] ({{< relref "/commands/hset" >}})
356
+ command page for more information.
357
+
358
+ ### ` CONFIG ` command
359
+
360
+ ` ioredis ` supports a ` config() ` method to set or get server configuration
361
+ options:
362
+
363
+ ``` js
364
+ client .config (' SET' , ' notify-keyspace-events' , ' KEA' );
365
+ ```
366
+
367
+ ` node-redis ` doesn't have a ` config() ` method, but instead supports the
368
+ standard commands [ ` configSet() ` ] ({{< relref "/commands/config-set" >}}),
369
+ [ ` configGet() ` ] ({{< relref "/commands/config-get" >}}),
370
+ [ ` configResetStat() ` ] ({{< relref "/commands/config-resetstat" >}}), and
371
+ [ ` configRewrite ` ] ({{< relref "/commands/config-rewrite" >}}):
372
+
373
+ ``` js
374
+ await client .configSet (' maxclients' , ' 2000' );
317
375
```
0 commit comments