redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recommended connection error handling pattern

nicholaswmin opened this issue · comments

Hi people,

What's the recommended strategy for error handling?

So my understanding is that there's no need for me to do anything.
The default reconnection strategy of Redis is enough.

My question would be, do I need to throw in some handler? Do I want to fail-fast and crash the server process on end? Not entirely sure. Any advice welcome.

  redis.on('end', () => {
    // Wont attempt reconnect
    console.log('redis:end')
  })

Here's the whole of my conn. code. Just wanna make sure I set this up as robustly as possible.

const redis = new Redis(url)

redis.on('ready', () => {
  // ready to accept connections
  console.log('redis:ready')
})

redis.on('connect', () => {
  // connected
  console.log('redis:connected')
})

redis.on('reconnecting', () => {
  // https://github.com/redis/ioredis?tab=readme-ov-file#auto-reconnect
  //
  // > By default, all pending commands will be flushed with an error
  // every 20 retry attempts.
  // That makes sure commands won't wait forever when the connection is down.
  // You can change this behavior by setting maxRetriesPerRequest:
  console.log('redis:reconnecting')
})
  
  redis.on('error', err => {
    // Will emit 20 times before giving up,
    // then have to manually reconnect
    console.log('redis:error', err)
  })
  
  redis.on('end', () => {
    // Wont attempt reconnect
    console.log('redis:end')
  })

Do you need to watch things?

Why not just connect, and then run your set/get and that is it?

Thoughts?

Hah good catch. I left half my question behind.

We use it as a pub/sub mechanism so it needs to stay up.

To be fair, I don't ever recall us having any issues on prod. with Redis going down but I wonder if there's a "standard" way of handling those conditions.

On the one hand I'm thinking of attempting manual reconnects, on the other hand I'm tempted to just crash the whole process on end. The default reconn settings should be more than enough I suppose.

If the reconn attempts are exhausted, then the process should just crash, see: https://en.wikipedia.org/wiki/Crash-only_software

How "highly available" is the Redis server? Does it make an effort to avoid crashing? If that's the case, then there's 0 need to go overboard with attempting manual reconnections. If it's gone, it's gone, my service is down and someone needs to tend to it.

For reference we run Redis on Heroku.