Redis client for Node.js: ioredis
Redis is a high-performance in-memory data store, often used as a database, cache, and message broker. For interacting with Redis from Node.js, there are several libraries, but one of the most popular and feature-rich is ioredis. This library provides a full-featured, high-performance, and convenient API for working with Redis, supporting all modern capabilities including clustering, pub/sub, transactions, and Lua scripts.
In this article, we will take a detailed look at ioredis, its installation, main features, and provide practical code examples in JavaScript (Node.js).
Installation and connection
Installing ioredis via npm or yarn is straightforward:
npm install ioredis# oryarn add ioredisAfter installation, create a client instance:
const Redis = require('ioredis');const redis = new Redis(); // default localhost:6379
// Or with explicit parametersconst redis = new Redis({ host: 'localhost', port: 6379, password: 'your_password', // if a password is set db: 0});The library supports Promise and async/await, making the code clean and readable.
Basic data operations
ioredis allows you to execute all standard Redis commands. Let's look at examples of working with strings, lists, and hashes.
Working with strings
async function stringExample() { await redis.set('user:1:name', 'Alexey'); const name = await redis.get('user:1:name'); console.log(name); // Alexey
// Setting with expiration time (TTL) await redis.setex('temp:code', 60, '123456'); // lives for 60 seconds}Working with lists
async function listExample() { await redis.lpush('queue:tasks', 'task1', 'task2'); await redis.rpush('queue:tasks', 'task3'); const tasks = await redis.lrange('queue:tasks', 0, -1); console.log(tasks); // ['task2', 'task1', 'task3']}Working with hashes
async function hashExample() { await redis.hset('user:1', { name: 'Maria', age: 28, city: 'Moscow' }); const age = await redis.hget('user:1', 'age'); console.log(age); // 28
const allData = await redis.hgetall('user:1'); console.log(allData); // { name: 'Maria', age: '28', city: 'Moscow' }}Pub/Sub (publisher-subscriber)
Redis supports the pub/sub pattern, and ioredis provides a convenient interface for this. Important: the subscriber must use a separate connection.
// Subscriberconst subscriber = new Redis();subscriber.subscribe('news', 'updates', (err, count) => { console.log(`Subscribed to ${count} channels`);});
subscriber.on('message', (channel, message) => { console.log(`Received from ${channel}: ${message}`);});
// Publisher (can be in another process)const publisher = new Redis();publisher.publish('news', 'Hello, world!');publisher.publish('updates', 'Version 2.0 has been released');Working with Redis cluster
ioredis supports clustering excellently. You just need to pass a list of cluster nodes:
const cluster = new Redis.Cluster([ { host: 'node1.example.com', port: 6379 }, { host: 'node2.example.com', port: 6379 }, { host: 'node3.example.com', port: 6379 }]);
async function clusterExample() { await cluster.set('key', 'value'); const val = await cluster.get('key'); console.log(val); // value}The library automatically handles command redirection between nodes (MOVED, ASK) and supports slots.
Transactions and pipelines
For atomic execution of multiple commands, use transactions (MULTI/EXEC). For performance improvement, use pipelines.
Transactions
async function transaction