Node.js with Redis

Server-side Javascript with Redis

 

Redis is a great database for use with Node.js. Both Redis and Node.js share similar type conventions and threading models, which makes for a very predictable development experience. By pairing Node.js and Redis together you can achieve a scalable and productive development platform.

Node.js has two primary clients available node_redis and ioredis. Both are available through npm. Both clients have pros and cons, but we generally suggest node_redis, as it has wide support for Redis modules, it is easily extensible and widely used. Ioredis has better integrated support for the Redis Enterprise Cluster API, but this is only relevant if your specifically using the Cluster API topology instead of the more common Proxy topology. Node_redis’ support for the Cluster API is provided by an additional shim, redis-clustr. Node_redis has built-in support for Redis Enterprise features such as Active Active geo distribution, allowing you to have one database that spans multiple clusters. Node_redis also supports Redis-on-Flash which enables extending your database into Flash SSD storage for warm values and DRAM for hot values.

node_redis architecture

Node_redis has good support for using Redis modules, however, being an interpreted language, it does not support building modules with the Redis Modules SDK. The client can be extended with the redis.add_command function prior to creating a client. Bindings exist for most common modules including:

Connecting to Redis in Node.js

Install the package with npm

npm install redis

After installing the package into your project, you’ll need to require the Node.js module as well as create an instance with your configuration details:

Node_redis manages connections automatically, so there is no need to wait for connection or callback. Since Node.js and Redis are both effectively single threaded there is no need to use multiple client instances or any pooling mechanism save for a few exceptions; the most common exception is if you’re subscribing with Pub/Sub or blocking with streams or lists, then you’ll need to have dedicated clients to receive these long-running commands.

To manipulate data in node_redis, each Redis command is implemented as a method of the client object. All Redis commands are included.

Try Node.js with Redis Enterprise

14 Day Free Trial Redis Enterprise

Further Reading Related to Node.js and Redis

For more information on using Redis Labs’ products and services with Node.js please see the Howto page.