Redis client for Node.js: ioredis — complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Redis client for JavaScript (Node.js) — ioredis



Redis is a high-performance key-value data store, often used for caching, session management, message queues, and Pub/Sub systems. There are several libraries for working with Redis from Node.js, but one of the most powerful and popular is ioredis. This library is designed for modern applications requiring high performance, support for clustering, transactions, and streaming data processing.



What is ioredis?

ioredis is a full-featured Redis client for Node.js, written in JavaScript. It supports all Redis features, including clustering, Sentinel modes, pipelining, transactions (MULTI/EXEC), Lua scripts, Pub/Sub, as well as working with MapReduce-like structures. The library is actively developed and used in production by many companies.



Installing ioredis

Installation is done via npm:

npm install ioredis

After installation, you can connect to a Redis server:

const Redis = require('ioredis');const redis = new Redis(); // defaults to localhost:6379


Key features of ioredis

  • Ease of use — intuitive API similar to Redis commands.
  • Promise and async/await support — all asynchronous operations return promises.
  • Clustering — automatic distribution of requests across cluster nodes.
  • Sentinel — automatic failover to a backup server.
  • Pipelining — sending multiple commands in one request for better performance.
  • Pub/Sub — subscribing to channels and real-time message exchange.
  • Lua script support — executing atomic operations on the server side.
  • Monitoring and events — tracking connection status and errors.


Usage examples for ioredis



Basic key operations

Example of setting and getting a value:

const Redis = require('ioredis');const redis = new Redis();

async function basicExample() { await redis.set('user:1', 'Alice'); const value = await redis.get('user:1'); console.log(value); // Alice}basicExample();


Working with hashes

Hashes allow storing multiple fields under one key:

async function hashExample() {    await redis.hset('user:2', 'name', 'Bob', 'age', 30);    const name = await redis.hget('user:2', 'name');    console.log(name); // Bob}hashExample();


Pipelining

Pipelining significantly speeds up executing multiple commands:

async function pipelineExample() {    const pipeline = redis.pipeline();    pipeline.set('key1', 'value1');    pipeline.set('key2', 'value2');    pipeline.get('key1');    const results = await pipeline.exec();    console.log(results); // array of results}pipelineExample();


Pub/Sub (Publisher-Subscriber)

Example of subscribing to a channel and sending a message:

const subscriber = new Redis();const publisher = new Redis();

subscriber.subscribe('news', (err, count) => { console.log(`Subscribed to ${count} channel(s)`);});

subscriber.on('message', (channel, message) => { console.log(`Received message from channel ${channel}: ${message}`);});

// Publishing a messageasync function publishMessage() { await publisher.publish('news', 'Hello, world!');}publishMessage();


Working with Redis cluster

To connect to a cluster, use Redis.Cluster:

const Redis =                    

Recommendations