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?

asked May 9, 2017 at 14:41

3 Answers 3

33

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);
});
answered May 11, 2017 at 16:50
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for sharing this but it is not working for me. What configuration of elasticache redis did you use?
@Ultrablendz please check the link again! I have updated it
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.
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
@SalimShamim please post a new question!
|
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);
});

answered Jul 5, 2022 at 5:31

Comments

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

answered Jul 12, 2022 at 16:09

1 Comment

which npm package you have imported ? I have imported "redis" package. I got error: TypeError: Redis.Cluster is not a constructor

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.