fastify-redis: how to handle connection error

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

what is the most correct way to handle redis connection errors, to have a simple human error log?

...
const fastify = Fastify({logger});

fastify.ready(err => {
  if (err) {
    fastify.log.debug(`Redis Connection Error ${err.code}`)
  }
})

fastify.register(require('@fastify/redis'), {
  host: config.redis.hosts,
  password: config.redis.password
});

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@stefanocudini - I’m having the same issue. Did you figure this one out yet? If you need help, I’m happy to pair up and do a PR on this one.

@stefanocudini - I’m having the same issue. Did you figure this one out yet? If you need help, I’m happy to pair up and do a PR on this one.

@agjs a partial solution for my case is it:

fastify.register(require('@fastify/redis'), {
  host: config.redis.hosts,
  password: config.redis.password,
  maxRetriesPerRequest: 100,
  retryDelayOnFailover: 500,
  closeClient: true, // force to close the redis connection when app stopped
})
.after(err => {
  const {redis} = fastify;
  if (err) {
    fastify.log.error('Redis connection error')
  }
  redis.on('error', function(err) {
    fastify.log.error('Redis connection error')
  })
})

@Eomm thank you very much for your suggestion. I think solved this way with the latest version of the plugin and using some additional parameters

You might need to add some additional logging in https://github.com/fastify/fastify-redis/blob/master/index.js#L61-L99.

Currently, this plugin assumes it can connect to the Redis instance. There is no way to “try again” if the connection fails or the time it tasks is over the time allotted.