Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect client disconnect from redis in nodejs?

I am using the library node_redis as the client for a micro-service message client that I am writing. Clients get messages from their application in their outbox that they need to send to other services. Everything is working great but I am trying to build some resilience on the part of the application that uses the redis client to communicate with the redis-server.

My idea is that the redis client-server connection status should be highly available to the clients. I mean that if a connection goes down, I would like to know within the second instead of the default timeout of 300 seconds. Currently I am using the free redislabs tier hosted on AWS, but I should be moving this to run in its own container on my kubernetes cluster.

I need to know the state of client connections in the network because I would like to not send messages when the network conditions are not right and not rely on the error handling to handle this sort of event. Knowing how often and when these high latency events occur will also help me diagnose and improve my network and my microservices.

Note: I wanted to set the connect_timeout value in the client options but this is listed as deprecated.

like image 583
tensai Avatar asked Nov 12 '17 20:11

tensai


People also ask

How do I close Redis client connection?

Due to the single-threaded nature of Redis, it is not possible to kill a client connection while it is executing a command. From the client point of view, the connection can never be closed in the middle of the execution of a command.

What is legacy mode in Redis?

Legacy Mode Use legacy mode to preserve the backwards compatibility of commands while still getting access to the updated experience: const client = createClient({ legacyMode: true }); // legacy mode client. set('key', 'value', 'NX', (err, reply) => { // ... }); // version 4 interface is still accessible await client.

How does Redis integrate with node js?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.

Why we use Redis in node JS?

Redis, an in-memory database that stores data in the server memory, is a popular tool to cache data. You can connect to Redis in Node. js using the node-redis module, which gives you methods to retrieve and store data in Redis.


1 Answers

Something like that?

var redis = require('redis');
var client = redis.createClient();
var reconnectAfter = 15000;

client.on( 'error', function () {
  console.log( (new Date()) + " Redis: disconnect");
  setTimeout( connect, reconnectAfter);
});

connect = function(){
  client = redis.createClient();
}
like image 152
Николай Лубышев Avatar answered Sep 21 '22 06:09

Николай Лубышев