2

I am new to Redis Cache in azure. I am trying to check the connection of the client variable before I write data to it. How can I achieve this in Nodejs?

I tried to fetch the client.connected status but when I do negative tests - such as shutdown the redis server, the client.connected variable isn't receiving anything, hence my code doesn't go any further to fetch eh data from my original server, by passing the cache server.

How can I do this in nodejs?

Shiva Nara
  • 81
  • 1
  • 2
  • 10
  • **See Also**: [Check if redis is running in node js](https://stackoverflow.com/q/24231963/1366033) – KyleMit Nov 20 '20 at 17:52

3 Answers3

6

redis has a PING command. you can try redis.ping() to check if the connection to redis server is ok or not

From redis docs:

Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk. This command is often used to test if a connection is still alive, or to measure latency.

If the client is subscribed to a channel or a pattern, it will instead return a multi-bulk with a "pong" in the first position and an empty bulk in the second position, unless an argument is provided in which case it returns a copy of the argument.

Community
  • 1
  • 1
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
5

For robust connection checking when using the redis npm package, you should use retry logic, handle the ready event, and ping to gut check if necessary.

Retry

When creating the client, there is automatically a default retry strategy, however you can pass in options object with retry_strategy to customize it like this:

const client = require('redis').createClient({

  retry_strategy: function(options) {
    if (options.error && options.error.code === "ECONNREFUSED") {
      // End reconnecting on a specific error
      return new Error("The server refused the connection");
    }
    if (options.total_retry_time > 1000 * 60 * 60) {
      // End reconnecting after a specific timeout
      return new Error("Retry time exhausted");
    }
    if (options.attempt > 10) {
      // End reconnecting with built in error
      return undefined;
    }

    // reconnect after
    return Math.min(options.attempt * 100, 3000);
  },

});

Ready

After creating a client, you should listen for connection and other events before proceeding like this:

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

client.on('connect'     , () => console.log('connect'));
client.on('ready'       , () => console.log('ready'));
client.on('reconnecting', () => console.log('reconnecting'));
client.on('error'       , () => console.log('error'));
client.on('end'         , () => console.log('end'));

Ping

As mentioned in the redis package docs, there is a 1 to 1 mapping of commands between the node client and the official redis commands, so you can do a final smoke test by calling ping like this:

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

client.on('ready', () => {
    let response = client.ping()
    console.log(response)
    // do other stuff
});

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
0

From the npm docs:

"ready"

client will emit ready once a connection is established. Commands issued before the ready event are queued, then replayed just before this event is emitted.

So this could be achieved by the snippet below:

const redis = require("redis");  
const client = redis.createClient();

client.on("ready", function() {  
  console.log("Connected to Redis server successfully");  
});
Rinwa
  • 11
  • 1
  • 2