Feature request: allow redis to fail connecting
simplenotezy opened this issue · 2 comments
We would love to allow for the possibility for redis not being able to connect. We use redis mainly for caching purposes, and in the situation that our redis cluster should crash / be temporary down, we'd like our nest.js application not to crash.
I suggest adding an option that would allowFailedConnections
(for lack of a better word) which would simply output to the logger that it failed to boot and perhaps keep retrying in the background.
When calling the redis.get()
it would fallback to the behaviour it has, when redis did not find the key.
Thoughts?
I consider that the solution mentioned above is pointless because there are many methods for a Redis/Cluster instance. Additionally, it will cause some confusion.
You can do it by try catch
as needed, for example:
// config
RedisModule.forRoot({
readyLog: true,
config: {
host: 'localhost',
port: 6380,
enableOfflineQueue: false // throw an error immediately for the performance of application
}
})
// service
async value() {
let value: string | null;
try {
value = await this.redis.get('name');
} catch {
value = null;
}
return value;
}
thanks @liaoliaots! the enableOfflineQueue
option was just what I needed, assuming it will prevent Nest.js from throwing errors if it cannot connect.