Cluster: Failed to refresh slots cache. when options.redisOptions.enableOfflineQueue = false
Problem
If I pass `options` to new Cluster with `options.redisOptions.enableOfflineQueue = false`, ioredis (3.2.2) fails to connect to the cluster. If I remove setting that specific option, then everything is good (went through them one-by-one). Below is sample code with the output when `options.redisOptions.enableOfflineQueue = false`. Note that I also have set `options.enableOfflineQueue = false` - it is not entirely clear if both need to be set. In my usage, I do NOT want commands to be queued when the cluster or the intended node is not available. [code block] [code block] If I comment out the one line, then all is good: [code block]
Error Output
error event: Error: Failed to refresh slots cache.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Adjust Redis Cluster Options for Successful Connection
The error occurs because setting `options.redisOptions.enableOfflineQueue` to false prevents ioredis from queuing commands when the cluster or intended node is unavailable. This can lead to a failure in refreshing the slots cache, which is critical for maintaining a connection to the Redis cluster. The `enableOfflineQueue` option at both the cluster and redisOptions levels may cause conflicts in behavior, leading to connection issues.
Awaiting Verification
Be the first to verify this fix
- 1
Remove `enableOfflineQueue` from redisOptions
Since setting `options.redisOptions.enableOfflineQueue` to false is causing connection issues, remove this setting to allow ioredis to handle offline commands appropriately.
typescriptoptions.redisOptions = { ...options.redisOptions, enableOfflineQueue: undefined }; - 2
Keep `enableOfflineQueue` in Cluster Options
Ensure that `options.enableOfflineQueue` is set to false to prevent queuing of commands at the cluster level while allowing the library to manage connection states effectively.
typescriptoptions.enableOfflineQueue = false; - 3
Test Connection to Redis Cluster
After adjusting the options, test the connection to the Redis cluster to ensure that commands are processed correctly without queuing.
typescriptconst cluster = new Cluster(options); cluster.on('error', (err) => console.error('Cluster error:', err)); - 4
Monitor Logs for Errors
Monitor the application logs for any errors related to Redis connections after the changes. This will help identify if the issue persists or if new issues arise.
typescriptconsole.log('Monitoring Redis connection logs...'); - 5
Review Documentation for ioredis
Review the ioredis documentation for any updates or changes regarding the handling of offline queues and cluster connections to ensure best practices are followed.
markdownhttps://github.com/luin/ioredis#readme
Validation
Confirm that the Redis cluster connects successfully without throwing the 'Failed to refresh slots cache' error. Execute commands against the cluster and verify that they are processed as expected without queuing.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep