Connection to Redis throwing connection timeout from NodeJS - node.js

I have a redis server in an azure linux vm running one master, slave and sentinel in the same VM(A). When i try to connect to the redis sentinal from another VM(B) using redis-cli, i am able to connect and set and get values. But when i try to connect to the redis sentinel using ioredis module in nodeJS from VM(B), it is throwing a connection timeout error. I use the following code snippet to connect to the sentinel from node application
var Redis = require('ioredis');
var redis = new Redis({
sentinels: [{ host: 'x.x.x.x', port: 26379}],
name: 'mymaster'
});
The confusing part is, when i run the redis master, slave and sentinel in the same vm(A) and using '127.0.0.1' instead of 'x.x.x.x' the code works fine.
Any help is much appreciated.

Related

Socket.io using Using Node.JS Cluster with PM2

I have tried, https://socket.io/docs/using-multiple-nodes/
const io = require('socket.io')(3000);
const redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
but didn't work, with multiple core of server. Can anyone expert here will guilde me will appreciated by me.
I using PM2 for node process clustering.
and i am facing issue, user in different thread could no connect with socket.IO but all users in same Socket.IO thread connection is working fine.
In short I want to cluster multiple Socket.IO server for Load balancing.

Why does connecting to a cluster constantly loop in IoRedis?

I am currently trying to connect to my Redis cluster stored on another instance from a server running my application. I am using IoRedis to interface between my application and my Redis instance and it worked fine when there was only a single Redis node running. However, after trying to setup the cluster connection in my Node application, it constantly loops on the connection. My cluster setup works correctly.
As of now, I have tried the following configuration in my application to connect to the cluster. The issue is that the 'connect' even constantly loops printing out 'Connected to Redis!'. The events for 'ready' and 'error' are never fired.
const cache: Cluster = new Cluster([{
port: 8000,
host: REDIS_HOST
}, {
port: 8001,
host: REDIS_HOST
}, {
port: 8002,
host: REDIS_HOST
}]);
cache.on('connect', () => {
console.log('Connected to Redis!');
});
In the end, the 'connect' event should only fire once. Does anyone have any thoughts on this?
This kind of error, as I discovered it today, is not related to ioredis but to the redis instance setup. In my case, the problem I had with p3x-redis-ui, which use ioredis, it was the cluster that was not initialized.
See https://github.com/patrikx3/redis-ui/issues/48
maybe you'll find any clues to help you resolve your bug.

Identifying Redis Master using sentinal from nodeJS

I have 2 Redis servers one master and the other slave(replication). Once the Master is down due to some reasons the slave will become Master and it continues to act as Master till something wrong happens to that server.
I have a nodeJS server from which i want to push data to the Redis which currently running as Master. I have a sentinel which monitors the Redis servers but my question is how to fetch master information from sentinel using nodeJS?
and if there is a way, does it automatically push data to alternative redis server without any service restart?
The ioredis supports the sentinel. like this:
var redis = new Redis({
sentinels: [{ host: 'localhost', port: 26379 }, { host: 'localhost', port: 26380 }],
name: 'mymaster'
});
redis.set('foo', 'bar');
The name identifies a redis cluster, which you should also specified in redis sentinel.conf. You could refer this page on how to configure sentinel.
About your second question, see below:
ioredis guarantees that the node you connected to is always a master even after a failover. When a failover happens, instead of trying to reconnect to the failed node (which will be demoted to slave when it's available again), ioredis will ask sentinels for the new master node and connect to it. All commands sent during the failover are queued and will be executed when the new connection is established so that none of the commands will be lost.
And node-redis does not support it now.

Connect AWS redis to node using node-redis

I am using node-redis and having a hard time connecting to external redis instance. I tried with redis-cli and it worked. However with node I am not able to figure out how to properly give the url and port.
With Redis-cli-
redis-cli -h mydomain.something.something.cache.amazonaws.com -p 6379
However with nodejs
Below didn't work
var client = redis.createClient('redis://mydomain.something.something.cache.amazonaws.com:6379'),
neither
var client = redis.createClient({host:'redis://mydomain.something.something.cache.amazonaws.com', port: 6379});
How do I configure it. Please help.
Following should work with node.js -
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
Also, make sure that your security group on AWS allows you to access the database.
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
With the above code, it was always trying to connect with localhost,
Below code worked for me.
var client = require('redis').createClient(
{
url: `redis://${elasticCacheConnectionString}`,
}
);
Please note, i have appended redis:// as communication protocol before actual connection string.
FYI: I am using redis#4.1.0 version.

how to check with ioredis if the a connection was established to the redis server?

I'm writing a nodejs 5.10.1 application, and i connect to a redis-server.
i'm using the ioredis nodejs module from https://github.com/luin/ioredis.
I can't seem to figure out how to check if a connection was established to the server.
the code to connect to a redis server is pretty simple:
var Redis = require('ioredis');
var redis = new Redis();
the documentation states the following under Connection Events
You can also check out the Redis#status property to get the current connection status.
don't really understand what it means or how to work with it. any ideas ?
One of the properties of the redis object you started is the status:
console.log(redis.status) will give you the current , in my case it says : connecting

Resources