Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RangeError: Maximum call stack size exceeded - nodejs, redis

Tags:

node.js

redis

I'm trying to delete large number of keys(~20M) from redis and I'm getting an error RangeError: Maximum call stack size exceeded due to execessive recursive calls. I tried using the process.nextTick() in the recursive call but still getting the same error.

count = "100";
cursor = "0";
function scanRedis(key, callback){

redisClient.scan(cursor, "MATCH", key, "COUNT", count, function(err, reply){
    if(err){
        throw err;
    }
    cursor = reply[0];
    if(cursor === "0" && reply[1].length === 0){
        return callback(false, true);
    }else if(cursor === "0" && reply[1].length > 0){
        redisClient.del(reply[1], function(deleteErr, deleteSuccess){
            return callback(false, true);
        });
    }else{
        if(reply[1].length > 0){
            delCount += reply[1].length;
            //console.log(reply[1]);
            redisMulti.del(reply[1]);
        }
        redisMulti.exec(function(deleteErr, deleteSuccess){
            process.nextTick(function(){
                scanRedis(key, function(err, reply){ //getting an error here
                    callback(false, true);
                });
            });
        });
    }
});
};
like image 950
raghav Avatar asked Oct 20 '25 16:10

raghav


1 Answers

I solved this by inserting another process.nextTick() in the callback of scanRedis() function and it worked for me.

redisMulti.exec(function(deleteErr, deleteSuccess){
  process.nextTick(function(){
    scanRedis(key, function(err, reply){ 
      process.nextTick(function(){
        callback(false, true);
      });
    });
  });
});
like image 199
raghav Avatar answered Oct 22 '25 07:10

raghav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!