Updated on 2025-09-02 GMT+08:00

Connecting to Redis on ioredis (Node.js)

This section describes how to access a Redis instance on ioredis. For more information about how to use other Redis clients, visit the Redis official website.

The following operations are based on an example of accessing a Redis instance on a client on an elastic cloud server (ECS).

Notes and Constraints

The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To access a Redis Cluster instance on ioredis, see Node.js Redis client description.

Prerequisites

  • A Redis instance is created, and is in the Running state. To create a Redis instance, see Buying a DCS Redis Instance.
  • An ECS has been created. For details about how to create an ECS, see Purchasing a Custom ECS
  • The Linux ECS must have GNU Compiler Collection (GCC) installed. To query the GCC version, run the gcc --version command.

    Run the following command to install GCC on the ECS if needed:

    yum install -y make
    yum install -y pcre-devel
    yum install -y zlib-devel
    yum install -y libevent-devel
    yum install -y openssl-devel
    yum install -y gcc-c++
  • The client and the Redis instance must be interconnected before connecting to the instance. For details, see Network Conditions for Accessing DCS Redis.

Connecting to Redis on ioredis

  • For a client server running Ubuntu (Debian series), see Client Server Running Ubuntu (Debian Series).
  • For a client server running CentOS (Red Hat series), see Client Server Running CentOS (Red Hat Series).
  1. View the IP address/domain name and port of the DCS Redis instance to be accessed.

    For details, see Viewing and Modifying Basic Settings of a DCS Instance.

  2. Log in to the ECS.
  3. Install Node.js.

    apt install nodejs-legacy

    If the preceding command does not work, run the following commands:

    wget https://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz --no-check-certificate
    tar -xvf node-v4.28.5.tar.gz
    cd node-v4.28.5
    ./configure
    make
    make install

    After the installation is complete, run the node --version command to query the Node.js version to check whether the installation is successful.

  4. Install the node package manager (npm).

    apt install npm

  5. Install the Redis client ioredis.

    npm install ioredis

  6. Edit the sample script for connecting to a DCS Redis instance.

    Add the following content to the ioredisdemo.js script, including information about connection and data reading.

    var Redis = require('ioredis');
    var redis = new Redis({
      port: 6379,          // Redis port
      host: '192.168.0.196',   // Redis host
      family: 4,           // 4 (IPv4) or 6 (IPv6)
      password: '******',
      db: 0
    }); 
    redis.set('foo', 'bar');
    redis.get('foo', function (err, result) {
      console.log(result);
    }); 
    // Or using a promise if the last argument isn't a function
    redis.get('foo').then(function (result) {
      console.log(result);
    });
    // Arguments to commands are flattened, so the following are the same:
    redis.sadd('set', 1, 3, 5, 7);
    redis.sadd('set', [1, 3, 5, 7]);
    // All arguments are passed directly to the redis server:
    redis.set('key', 100, 'EX', 10);

    host indicates the example IP address/domain name of the DCS instance and port indicates the port of the DCS instance. For details about how to obtain the IP address/domain name and port, see 1. Change them as required. ****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. To connect to an instance using an ACL user, configure the instance password to username:password. For details about how to create or view an ACL user, see Configuring DCS Redis ACL Users. Omit the password setting in the command for a password-free instance.

  7. Run the sample script to access the chosen DCS Redis instance.

    node ioredisdemo.js

  1. View the IP address/domain name and port of the DCS Redis instance to be accessed.

    For details, see Viewing and Modifying Basic Settings of a DCS Instance.

  2. Log in to the ECS.
  3. Install Node.js.

    yum install nodejs

    If the preceding command does not work, run the following commands:

    wget https://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz --no-check-certificate
    tar -xvf node-v0.12.4.tar.gz
    cd node-v0.12.4
    ./configure
    make
    make install

    After the installation is complete, run the node -v command to query the Node.js version to check whether the installation is successful.

  4. Install the node package manager (npm).

    yum install npm

  5. Install the Redis client ioredis.

    npm install ioredis

  6. Edit the sample script for connecting to a DCS Redis instance.

    Add the following content to the ioredisdemo.js script, including information about connection and data reading.

    var Redis = require('ioredis');
    var redis = new Redis({
      port: 6379,          // Redis port
      host: '192.168.0.196',   // Redis host
      family: 4,           // 4 (IPv4) or 6 (IPv6)
      password: '******',
      db: 0
    });
    redis.set('foo', 'bar');
    redis.get('foo', function (err, result) {
      console.log(result);
    });
    // Or using a promise if the last argument isn't a function
    redis.get('foo').then(function (result) {
      console.log(result);
    }); 
    // Arguments to commands are flattened, so the following are the same:
    redis.sadd('set', 1, 3, 5, 7);
    redis.sadd('set', [1, 3, 5, 7]); 
    // All arguments are passed directly to the redis server:
    redis.set('key', 100, 'EX', 10);

    host indicates the example IP address/domain name of the DCS instance and port indicates the port of the DCS instance. For details about how to obtain the IP address/domain name and port, see 1. Change them as required. ****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. To connect to an instance using an ACL user, configure the instance password to username:password. For details about how to create or view an ACL user, see Configuring DCS Redis ACL Users. Omit the password setting in the command for a password-free instance.

  7. Run the sample script to access the chosen DCS Redis instance.

    node ioredisdemo.js

Related Document

When accessing Redis fails, see Troubleshooting Redis Connection Failures.