EMFILE error when using nodejs redis

I want to test how many connections my redis server can hold, so I call redis.createClient() in a loop, while the redis server still runs lively, I got the EMFILE error, I know that I have used out my fds.

but wait, I have just test my mqtt server before, I did the same thing to my mqtt server, I called mqtt.createClient() in a loop of 10000, of 20000... but I never got the EMFILE error .

so, does the nodejs mqtt library use a different mechanism underneath?

redis-client.js :

var redis = require('redis');

function start() {
    var client = redis.createClient();

    client.on('error', function(err) {
        console.log('Error ' + err);
    });
}

exports.start = start;

redis-test.js

var redis_client = require('./redis-client');

for(var i = 0 ; i < 10000 ; ++i) {
    redis_client.start();
    console.log('redis client ' + i + ' started');
}

mqtt-subclient.js

var mqtt = require('mqtt');
function start() {
    var client = mqtt.createClient();
    client.subscribe('message');

    //client.publish('message', 'hello me!');
    client.on('message', function(topic, message) {
        console.log('receive message: ');
        console.log(message);
    });

    client.on('connack', function(packet) {
        console.log(packet);
        if(packet.returnCode == 0) {
            console.log('connect successfully');
        }
    });

    client.on('suback', function(packet) {
        console.log(packet.messageId);
    });

    client.on('error', function(err) {
        console.log(err);
    });
}

exports.start = start;

mqtt-test.js

var subclient = require('./mqtt-subclient.js');

for(var i = 0 ; i < 10000 ; ++i) {
    subclient.start();
    console.log('client ' + i + ' started');
}

Redis cannot accept more than x simultaneous connection attempts, where x is the backlog parameter of the listen system call.

It is limited by the minimum between the somaxconn kernel parameter (128 is a common default value), and 512. So if you attempt more than min(somaxconn,512) simultaneous connections, you can have errors. If you add a small delay between your connection attempts, it should fix this problem.

Then, you need to check that you have enough resources to open 10000 file descriptors (check the output of ulimit -a), and that your TCP/IP ephemeral port range is big enough to accomodate such a number of client connections.