Redis (ioredis) for JavaScript (Node.js): Complete Guide
\n\nRedis (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\nWhy 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\nThe 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.
Installation
\n\nInstalling 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\nnpm install ioredis\n\nIf you are using yarn, the command will be:
\nyarn add ioredis\n\nAfter 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:
\ndocker run -d --name redis-stack -p 6379:6379 redis/redis-stack-server:latest\n\nQuick Start
\n\nCreate a file app.js and write the following minimal code that connects to Redis, saves a value, and reads it.
// 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\nRun 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.
Main Methods and Classes
\n\nioredis provides dozens of methods corresponding to Redis commands. Below are 8 of the most frequently used methods with detailed descriptions and examples.
\n\n1. set(key, value, ...args)
\nSignature:
Also in library
Recommendations