Reputation: 31335
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?
Upvotes: 25
Views: 26232
Reputation: 297
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
Upvotes: 1
Reputation: 122
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);
});
Upvotes: 1
Reputation: 31335
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);
});
Upvotes: 33