Purge data from redis cache in chunk

787 views Asked by At

I need to delete a pool from redis cache. However, this pool might have millions of keys. I am using following code to delete the keys from cache

String regex = "*." + poolname + ".*";
Set<String> rkeys = jedis.keys(regex);

for (String key : rkeys) {
    LOGGER.info("key ===>" + key);
    jedis.del(key);
}

I am afraid that redis server might crash in case, there are million rows. Is there any way I can tell redis to select only 100 rows and delete at time. Something like

while (true) {
    //sleep for 1 minute
    //get 100 rows from cache
    if (keys.isEmpty()) {
        break;
    }
    jedis.del(key);
}
1

There are 1 answers

1
Matthias Winkelmann On BEST ANSWER

Redis shouldn't ever crash, and I would test the scenario before making my code more complicated on a hunch. I just created a million keys and deleted them. It took 2 minutes and the bottleneck was the ruby client, not redid.

That said, you may want to check out https://redis.io/commands/unlink, which is a new non-blocking version of DEL.