How do you kill a redis client when there is no connection?

The right way to have control on the client's reconnect behaviour is to use a retry_strategy.

Upon disconnection the redisClient will try to reconnect as per the default behaviour. The default behaviour can be overridden by providing a retry_strategy while creating the client.

Example usage of some fine grained control from the documentation.

var client = redis.createClient({
    retry_strategy: function (options) {
        if (options.error && options.error.code === 'ECONNREFUSED') {
            // End reconnecting on a specific error and flush all commands with
            // a individual error
            return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands
            // with a individual error
            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);
    }
});

Ref: https://www.npmjs.com/package/redis#options-object-properties

For the purpose of killing the client when the connection is lost, we could use the following retry_strategy.

var client = redis.createClient({
    retry_strategy: function (options) {
        return undefined;
    }
});

Tags:

Node.Js

Redis