23

We know that ElastiCache is not recommended to be accessed outside Amazon instances, so we're trying below stuff inside Amazon EC2 instances only.

We've got a ElastiCache Redis Cluster with 9 nodes. When we try to connect to it using normal redis implementation, it throws some Moved errors

Have tried the retry strategy method as per @Miller. Have also tried RedisCluster with unstable and stable (poor man) implementations.

None of these implementations are working. Any suggestions please?

Community
  • 1
  • 1
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219

3 Answers3

34

Sharing the code for future readers:

var RedisClustr = require('redis-clustr');
var RedisClient = require('redis');
var config = require("./config.json");

var redis = new RedisClustr({
    servers: [
        {
            host: config.redisClusterHost,
            port: config.redisClusterPort
        }
    ],
    createClient: function (port, host) {
        // this is the default behaviour
        return RedisClient.createClient(port, host);
    }
});

//connect to redis
redis.on("connect", function () {
  console.log("connected");
});

//check the functioning
redis.set("framework", "AngularJS", function (err, reply) {
  console.log("redis.set " , reply);
});

redis.get("framework", function (err, reply) {
  console.log("redis.get ", reply);
});
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 1
    Thanks for sharing this but it is not working for me. What configuration of elasticache redis did you use? – Ultrablendz Jun 08 '20 at 10:38
  • @Ultrablendz please check the link again! I have updated it – Zameer Ansari Jun 08 '20 at 16:03
  • 1
    Thanks for updating the link, however, it is not working with AWS Elastiredis with cluster mode enabled. I am also trying to it out from a lamda instance and not ec2 instance, though that shouldn't matter. – Ultrablendz Jun 09 '20 at 08:45
  • Can you please suggest way for including auth, I have encryption in transit on elasticache instance, which I am not able to authenticate using client.auth, code just dies there, I am sure I am missing something and couldn't find anything over internet – Salim Shamim Jul 15 '20 at 11:31
  • @SalimShamim please post a new question! – Zameer Ansari Jul 15 '20 at 18:53
  • @ZameerAnsari I am not able to connect to cluster using the code you provided. It is giving timeout errors. – Avani Khabiya Dec 16 '20 at 06:26
  • @AvaniKhabiya Check if you've provided the correct host and port – Zameer Ansari Dec 17 '20 at 05:20
  • @ZameerAnsari It's the right host and port I have provided. What could be the issue in this case? – Avani Khabiya Dec 17 '20 at 06:04
  • @AvaniKhabiya If all the details provided in my solution match your problem then it's possible that this answer is outdated. I would suggest asking a new question with the steps to reproduce the problem – Zameer Ansari Dec 17 '20 at 07:36
  • I added this code it worked with me, however, I got an error with elasticache cluster mode, some case it makes the website return a 404 page because Redis redirect node – tomnyson Jul 11 '22 at 00:59
  • @tomnyson I no longer work on this project so I'm not sure how to fix it. Can you please figure out the solution and update this answer?? – Zameer Ansari Jul 12 '22 at 11:04
1

You can try to connect using ioredis.

var Redis = require('ioredis');
var config = require("./config.json");

const redis = new Redis({
  host: config.host,
  port: config.port,
  password: config.password, // If you have any.
  tls: {}, // Add this empty tls field.
});

redis.on('connect', () => {
  console.log('Redis client is initiating a connection to the server.');
});

redis.on('ready', () => {
  console.log('Redis client successfully initiated connection to the server.');
});

redis.on('reconnecting', () => {
  console.log('Redis client is trying to reconnect to the server...');
});

redis.on('error', (err) => console.log('Redis Client Error', err));

//check the functioning
redis.set("framework", "AngularJS", function(err, reply) {
  console.log("redis.set ", reply);
});

redis.get("framework", function(err, reply) {
  console.log("redis.get ", reply);
});
abhikedia_
  • 102
  • 1
  • 4
1
const redisClient = process.env.NODE_ENV === 'production'
  ? new Redis.Cluster(
    [
      {
        host: 'node-production-0001-001.b2tyw0.0001.use2.cache.amazonaws.com',
        port: 6379,
        flags: 'master'
      },
      {
        host: 'node-production-0001-002.b2tyw0.0001.use2.cache.amazonaws.com',
        port: 6379,
        flags: 'slave'
      },
      {
        host: 'node-production-0001-003.b2tyw0.0001.use2.cache.amazonaws.com',
        port: 6379,
        flags: 'slave'
      },
    ], {
      scaleReads: 'slave'
    }
  )
  : new Redis({
    host: process.env.REDIS_HOST || 'localhost',
    port: process.env.REDIS_PORT || 6379,
  });

It's work for me with enable cluster mode

tomnyson
  • 189
  • 2
  • 7