-
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.] }
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] } ]
]
*/
});
The only differences between ioredis and redis in terms of API are constructor and transaction, so it's not hard to migrate your code from redis to ioredis. However if you are using a third-party library that replies on redis(e.g. express-session, connect-redis), I don't think it's necessary to touch them since you can use ioredis and redis together in your code.