-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Migrating from node_redis
It's not hard to migrate from node_redis to ioredis since ioredis is compatible with node_redis for most APIs. However there are some exceptions:
-
constructor. node_redis use
var client = redis.createClient()
to create a new RedisClient instance, while ioredis prefer tovar redis = new Redis()
. For compatibility, ioredis supportsvar redis = Redis.createClient()
as well. -
The options passed to the constructor are different. See API#new_Redis.
-
ioredis considers
null
/undefined
as empty strings, while node_redis will convert them to"null"
/"undefined"
. For instance:// node_redis client.set('foo', null); client.get('foo', function(_, res) { console.log(typeof res, res); // "string", "null" }); // ioredis redis.set('foo', null); redis.get('foo', function(_, res) { console.log(typeof res, res); // "string", "" });
-
Transaction. The result of transaction in node_redis and ioredis are different, for instance:
// node_redis client.multi().set('foo').get('foo').exec(function (err, res) { /* err is: [ [Error: Error: ERR wrong number of arguments for 'set' command], [Error: Error: EXECABORT Transaction discarded because of previous errors.] ] res is undefined */ }); // ioredis redis.multi().set('foo').get('foo').exec(function (err, res) { /* err is: { [ReplyError: EXECABORT Transaction discarded because of previous errors.] name: 'ReplyError', message: 'EXECABORT Transaction discarded because of previous errors.', command: { name: 'exec', args: [] }, previousErrors: [ { [ReplyError: ERR wrong number of arguments for 'set' command] name: 'ReplyError', message: 'ERR wrong number of arguments for \'set\' command', command: [Object] } ] } res is undefined */ }); // node_redis client.multi().set('foo', 'bar').lpush('foo', 1).exec(function (err, res) { /* err is: null res is [ 'OK', 'WRONGTYPE Operation against a key holding the wrong kind of value' ] */ }); // ioredis redis.multi().set('foo', 'bar').lpush('foo', 1).exec(function (err, res) { /* err is: null res is [ [ null, 'OK' ], [ { [ReplyError: WRONGTYPE Operation against a key holding the wrong kind of value] } ] ] */ });
-
Pub/Sub. When you subscribe multiple channels in node_redis, only the first response is included in the callback. For instance:
client.subscribe('foo', 'bar', function (err, channel) { // channel is 'foo' // `bar` isn't subscribed now });
However, in ioredis, callback function will be invoked only when all the channels are subscribed, and the result is the channel count that the client is subscribed:
redis.subscribe('foo', 'bar', function (err, count) { // count is 2 // both `foo` and `bar` are subscribed now });