Search code examples
node.jsredisamazon-elasticacheredis-cluster

"Couldn't Get Slot Allocation" Error On AWS ElastiCach (Redis) using Node.js


I was able to connect to Amazon ElastiCache (Redis) using Node.js libraries, which are redis and redis-clustr without any error. But get couldn't get slot allocation error whenever I tried to set key and value pair in Node.js app running on Amazon EC2 instance. My Amazon ElastiCache has one primary node and one replica (that is two node).

This is the code sample used for the connection:

const redis_cluster = require('redis-clustr');
const redis_client = require('redis');

let redis = new redis_cluster({
    servers: [
        {
             host: global.gConfig.redis.redis_host,
             port: global.gConfig.redis.redis_port
        }
    ],
    createClient: (port, host) => {
        return redis_client.createClient(port, host);
    }
});

// connection error
redis.on('error', err => {
    // log the error to log file
    global.gLogger.log('error', err.message, {stack: err.stack});
});

// add to global variables
global.gRedisClient = redis;

After I have established a connection to Amazon ElastiCach, the code sample below is how I retrieve values from Redis:

gRedisClient.mget(['run:123', 'walk:123'], (err, replies) => {...});

I use Redis multi() for bach operations and mget() to retrieve values at once. Am new to using Amazon ElastiCach, any help will be appreciated. Thank you.


Solution

  • I experienced the same. My reason was that my ElastiCache was configured as Master-Slave, and therefore redis-clustr is not needed. Connect to it using e.g. ioredis:

    const Redis = require('ioredis');
    const opts = {
        host: "your_host",
        port: 6379,
        autoResubscribe: true,
        maxRetriesPerRequest: 5
    };
    const redis = new Redis(opts);