Redis (ioredis) for JavaScript (Node.js): Complete Guide with Examples

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 (ioredis) for JavaScript (Node.js): Complete Guide

\n\n

Redis (Remote Dictionary Server) is a high-performance in-memory key-value data store, often used as a database, cache, and message broker. To work with Redis from Node.js, there are several clients, and ioredis is one of the most powerful, performant, and popular. This library provides a full set of functions for interacting with Redis, including support for clusters, Pub/Sub, pipelining, transactions, and Lua scripts.

\n\n

Why is this library needed? In modern web development, Node.js often requires fast data access. Redis, working in memory, provides read/write speeds unattainable by traditional relational databases. ioredis allows developers to easily integrate this power into their applications: cache complex query results, manage user sessions, implement task queues, rating systems, and much more. The library is designed with the asynchronous nature of Node.js in mind, fully supporting Promises and async/await, making code clean and predictable.

\n\n

The key advantage of ioredis over other clients (e.g., the outdated node_redis) is its built-in support for Redis clusters and Sentinel, as well as automatic reconnection. The library is actively maintained by the community, has detailed documentation, and is used in production by thousands of companies worldwide. It is suitable for both simple scripts and high-load distributed systems.

\n\n

Installation

\n\n

Installing the ioredis library is done via the npm (Node Package Manager) package manager. Make sure you have Node.js (version 12 or higher) and npm installed.

\n\n
npm install ioredis
\n\n

If you are using yarn, the command will be:

\n
yarn add ioredis
\n\n

After installation, the library will be available for import into your project. You also need a running Redis server. If you don't have one, you can install it locally or use Docker:

\n
docker run -d --name redis-stack -p 6379:6379 redis/redis-stack-server:latest
\n\n

Quick Start

\n\n

Create a file app.js and write the following minimal code that connects to Redis, saves a value, and reads it.

\n\n
// Import the ioredis library\nconst Redis = require('ioredis');\n\n// Create a new client. By default, connects to localhost:6379\nconst redis = new Redis();\n\n// Async function to demonstrate the work\nasync function quickStart() {\n  try {\n    // Set the key 'name' with the value 'PythonLib User'\n    await redis.set('name', 'PythonLib User');\n    console.log('Key "name" set.');\n\n    // Get the value by key 'name'\n    const result = await redis.get('name');\n    console.log(`Value of key "name": ${result}`);\n\n    // Check if the key exists\n    const exists = await redis.exists('name');\n    console.log(`Key "name" exists: ${exists === 1}`);\n\n    // Close the connection\n    redis.quit();\n  } catch (error) {\n    console.error('Error:', error);\n    redis.quit();\n  }\n}\n\nquickStart();
\n\n

Run the script: node app.js. You should see messages in the console about successfully setting and reading the key. This is a basic example demonstrating how easy it is to get started with ioredis.

\n\n

Main Methods and Classes

\n\n

ioredis provides dozens of methods corresponding to Redis commands. Below are 8 of the most frequently used methods with detailed descriptions and examples.

\n\n

1. set(key, value, ...args)

\n

Signature:

Recommendations