Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended connection error handling pattern #1875

Open
nicholaswmin opened this issue Apr 4, 2024 · 2 comments
Open

Recommended connection error handling pattern #1875

nicholaswmin opened this issue Apr 4, 2024 · 2 comments

Comments

@nicholaswmin
Copy link

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')
  })
@billnbell2
Copy link

Do you need to watch things?

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

Thoughts?

@nicholaswmin
Copy link
Author

nicholaswmin commented Apr 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants