raghav
raghav

Reputation: 451

RangeError: Maximum call stack size exceeded - nodejs, 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);
                });
            });
        });
    }
});
};

Upvotes: 4

Views: 1445

Answers (1)

raghav
raghav

Reputation: 451

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);
      });
    });
  });
});

Upvotes: 3

Related Questions