Flushall in Redis Cluster
See original GitHub issueIt may just be a lack of understanding but I could not find the answer in browsing the unit tests of a Redis Cluster (latest Redis version).
It seems that a flushall() command which is sent to a cluster connection does take some time to populate or is not properly handled in terms of callback / waiting.
For example, I have the following function which should flush Redis (it is used insde the AfterEach function of my Mocha unit tests) by using Promises (I love Bluebird Promises!):
function flushDBAsync(cluster) {
log.debug("flushing Redis ...");
return cluster.flushall()
.then(function(val) {
if (val === "OK") {
log.debug("flushing finished.");
log.debug("disconnecting from Redis ...");
return cluster.disconnect();
} else {
var msg = "flushing returned with " + val;
return Promise.reject(msg);
}
})
.then(function() {
log.debug("disconnecting finished.");
return true;
})
.catch(function(err) {
log.error("Error in flushDB: " + err);
return false;
});
}
The function is working always 100% perfectly if I am using a “normal” Redis connection (not a cluster). But when used with a cluster connection then the flushing is not fully completed (populated?) after the return and there are still some keys existing in the cluster.
For now my hacky solution is to wait 2 seconds after I called the function but that is really slowing down my unit tests: return flushDBAsync(redisConnection).delay(2000);
How should I properly do a flushall on a Redis Cluster consisting of 3 masters and 3 slaves (default from Redis website)? Redis Cluster is really awesome and I would love to use it with ioredis.
Thanks for looking into that surely very special case!
Sebastian
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Implemented in 2.0. Closing this issue.
Example:
I noticed that for me it only works if I use
cluster.nodes('master')
. For me all theslaves
are only in readOnly mode and thus flushing them doesn’t work.I flush Redis after each test suite and this was never really stable. Sometimes it flushed properly and sometimes it didn’t. I found the solution to this and thought I’d post it here in case it helps someone else. The trick is to wait for the
ready
event first and then (at least in my case) use themaster
nodes. Below is the code I used:Without waiting for the
ready
event, the nodes returend byredis.nodes()
is just an empty array.