question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flushall in Redis Cluster

See original GitHub issue

It 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:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
luincommented, Mar 15, 2016

Implemented in 2.0. Closing this issue.

Example:

// Send `FLUSHDB` command to all slaves:
var slaves = cluster.nodes('slave');
Promise.all(slaves.map(function (node) {
  return node.flushdb();
}));
1reaction
marnixhohcommented, Sep 2, 2021

I noticed that for me it only works if I use cluster.nodes('master'). For me all the slaves 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 the master nodes. Below is the code I used:

const redisSetup = await new Promise((resolve) => redis.addListener('ready', () => {
    const nodes = redis.nodes('master')
    Promise.all(nodes.map((node) => node.flushdb())).then(resolve)
}))

Without waiting for the ready event, the nodes returend by redis.nodes() is just an empty array.

Read more comments on GitHub >

github_iconTop Results From Across the Web

redis - Is there a way to flushall on a cluster so all keys from ...
Yes. You can use the cli's --cluster switch with the call command - it will execute the provided command on each of the...
Read more >
FLUSHALL
Delete all the keys of all the existing databases, not just the currently selected one. This command never fails. By default, FLUSHALL will...
Read more >
How to Use Delete All Keys in a Redis Cluster
The Redis FLUSHALL command lets you delete all the keys stored in the databases in the Redis instance. ... The command returns ok...
Read more >
Flushall on Redis cluster
Flushall on Redis cluster. GitHub Gist: instantly share code, notes, and snippets.
Read more >
How to flush Redis cache and delete everything using the ...
FLUSHDB command – Delete all the keys of the currently selected DB. · FLUSHALL command – Remove all the keys of all the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found