Connecting to Render Redis with ioredis

This guide will give examples of how to connect to Render Redis with ioredis.

ioredis

ioredis is a popular Redis client for Node.js. It is a high-performance client written in Typescript and has support for TLS connections and Redis 6.

We recommend using the latest version of ioredis. The guide assumes a minimum version of 5.0.0.

Setup

First, you will need to create your Render Redis service. By default, you can only connect to it from other Render services. Use the Internal Redis URL provided in the Render Dashboard.

If you want to connect to your Redis instance from outside Render (for testing, dev environments, etc), you will need to enable external connections and add the IP addresses you want to connect from. Once this is setup, you will be provided an External Redis URL.

Configure ioredis

Next, we will show how to use the Redis URL we provide, then present examples of detailed configurations.

We strongly recommend storing the Redis connection details in environment variables.

Redis URL

We recommend configuring ioredis by passing in the provided internal or external Redis URL. When used in Blueprints, the Redis URL can automatically be passed to your services using the fromService syntax (docs).

const Redis = require("ioredis");

const { REDIS_URL } = process.env;
// Internal Redis URL example:
// "redis://red-xxxxxxxxxxxxxxxxxxxx:6379"
// External Redis URL will be slightly different:
// "rediss://red-xxxxxxxxxxxxxxxxxxxx:PASSWORD@HOST:6379"

const renderRedis = new Redis(REDIS_URL);

renderRedis.set("animal", "cat");

renderRedis.get("animal").then((result) => {
  console.log(result); // Prints "cat"
});

Detailed connection config

If you want to explicitly configure ioredis you can use the following examples.

Internal connection

const Redis = require("ioredis");

// Internal Redis URL, extract the details into environment variables.
// "redis://red-xxxxxxxxxxxxxxxxxxxx:6379"

const renderRedis = new Redis({
  host: process.env.REDIS_SERVICE_NAME, // Render Redis service name, red-xxxxxxxxxxxxxxxxxxxx
  port: process.env.REDIS_PORT || 6379, // Redis port
});

External connection

const Redis = require("ioredis");

// External Redis URL, extract the details into environment variables.
// "rediss://red-xxxxxxxxxxxxxxxxxxxx:PASSWORD@HOST:6379"

const renderRedis = new Redis({
  username: process.env.REDIS_SERVICE_NAME, // Render Redis name, red-xxxxxxxxxxxxxxxxxxxx
  host: process.env.REDIS_HOST,             // Render Redis hostname, REGION-redis.render.com
  password: process.env.REDIS_PASSWORD,     // Provided password
  port: process.env.REDIS_PORT || 6379,     // Connection port
  tls: true, // TLS required when externally connecting to Render Redis
});

Code examples

Full examples for ioredis of the above are available in our Render Examples repo.