node-redis
has fantastic functionality for handling any Redis disconnections once everything is up and running. If the Redis instance becomes unavailable, use of an appropriate retry_strategy
means the client can be set up to try to reconnect until Redis is available again.
Is it possible to get the client to enter this state at start-up, even if Redis is down?
My scenario is this: I am using Redis as a primary data-store, with a fallback secondary data-store which is not Redis-based. When my application starts, if Redis is not available, attempts to retrieve data will use the secondary data-store instead.
However, when Redis does become available, I'd like my application to start using the Redis primary data-store instead. As the Redis connection did not succeed on start-up, the retry_strategy
, which would handle this for a previously established connection, does not work.
I could write code which will retry the initial Redis connection until it is successful, but it strikes me that the functionality available out-of-the-box is already very close to what I need, if I can persuade it to come into play from start-up even though Redis is down.
By default a client will try reconnecting until connected. You can use the retry_strategy available in the options object properties if you want to customise it. you can know when the redis is connected since it would emit an event
client.on("connect", function (){
});
you can later decide how to handle once connected