Search code examples
redisamazon-elasticachenode-redis

redis.createCluster() with TLS returns ECONNRESET when trying to conenct to ElastiCache for Redis


I am building a nodejs app that connects to ElastiCache Redis using node-redis. I'm trying to enable encryption-in-transit.

Here is what I have so far:

const redis = require('redis');

const socket = {
    host: redisAddress,
    port: redisPort,
    tls: true
}

const redisCluster = redis.createCluster({ rootNodes: [{ socket }] })
redisCluster.connect()

Unfortunately, I get the following error:

UnhandledPromiseRejectionWarning: Error: read ECONNRESET

Solution

  • From the node-redis documentation on clustering:

    rootNodes - an array of root nodes that are part of the cluster, which will be used to get the cluster topology. Each element in the array is a client configuration object. There is no need to specify every node in the cluster, 3 should be enough to reliably connect and obtain the cluster configuration from the server

    This means that the client configuration we specify in rootNodes applies to the root nodes, but not for each and every node in the topology. What you want to do is to specify defaults as well.

    defaults - The default configuration values for every client in the cluster. Use this for example when specifying an ACL user to connect with

    Your code will end up looking something like:

    const redis = require('redis');
    
    const socket = {
        host: redisAddress,
        port: redisPort,
        tls: true
    }
    
    const redisCluster = redis.createCluster({
        rootNodes: [{ socket }],
        defaults: { socket: { tls: true } }
    })
    redisCluster.connect()