How to connect to ElastiCache cluster using node.js
Asked Answered
J

3

25

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?

Jacks answered 9/5, 2017 at 14:41 Comment(0)
J
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);
});
Jacks answered 11/5, 2017 at 16:50 Comment(11)
Thanks for sharing this but it is not working for me. What configuration of elasticache redis did you use?Gavingavini
@Gavingavini please check the link again! I have updated itJacks
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.Gavingavini
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 internetMountain
@SalimShamim please post a new question!Jacks
@ZameerAnsari I am not able to connect to cluster using the code you provided. It is giving timeout errors.Gamete
@AvaniKhabiya Check if you've provided the correct host and portJacks
@ZameerAnsari It's the right host and port I have provided. What could be the issue in this case?Gamete
@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 problemJacks
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 nodeSolidify
@Solidify 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??Jacks
H
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);
});
Hackathorn answered 5/7, 2022 at 5:31 Comment(0)
S
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

Solidify answered 12/7, 2022 at 16:9 Comment(1)
which npm package you have imported ? I have imported "redis" package. I got error: TypeError: Redis.Cluster is not a constructorLucarne

© 2022 - 2024 — McMap. All rights reserved.