'ioredis - Ignore request if redis is down

I'm using ioredis. In order to prevent a huge buffer that can crash my app, I want to ignore requests when redis is down, and to catch those requests. Is there any way to achieve that?



Solution 1:[1]

You may use circuit breaker design pattern.

Circuit breaker is a design pattern used in modern software development. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.

Generally Circuit Breaker can be used to check the availability of an external service. An external service can be a database server or a web service used by the application.

Martin fowler's blog post has a good explanation and a basic implementation about how to do it.

Solution 2:[2]

For other people finding this who want a bit more help. This intercepts commends to ioredis and just returns false if it's not in a ready state.

// Proxify commands to redis client.
// If redis is unavailable then return false.
// This stops everything dying if redis is down.
module.exports.redis = new Proxy(client, {
  get(target, property, receiver) {
    if (client.status !== 'ready') {
      return (() => false);
    }
    return Reflect.get(target, property, receiver);
  },
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 OzTheGreat