jadent
jadent

Reputation: 3894

Node.js maxing out at 1000 concurrent connections

We're benchmarking node performance using a simple Hello World node server on AWS (EC2).

No matter what size instance we use Node always appears to max out at 1000 concurrent connections (this is NOT 1000 per second, but 1000 it can handle at 1 time). Shortly after that the CPU spikes and node basically freezes.

Node v0.10.5

var http = require('http');
var server = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('loaderio-dec86f35bc8ba1b9b604db6c328864c1');
});
server.maxHeadersCount = 0;
server.listen(4000);

Node should be able to handle more than this correct? Any thoughts would be greatly appreciated.

Also the file descriptors (soft, hard, system) are set to 65096)

Upvotes: 46

Views: 52579

Answers (2)

BadCanyon
BadCanyon

Reputation: 3133

You've reached the default limit of file descriptors a process can use (1024). You can check the limit on the command line by running "ulimit -n". To change the limit, you need to edit /etc/security/limits.conf. Add the follow block:

* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535

"*" applies to all users except root. Limits for root must be added separately. There are soft and hard limit. Users are allowed to change their own limits up to the soft limit but not exceeding the hard limit.

Once the file has been edited, log out and back in again. Verify the change by running ulimit -n. Restart your Node process and you should be good to go.

There's also a system-wide file descriptor limit that can be increased with the following command:

sysctl -w fs.file-max=65535

Upvotes: 19

Daniel
Daniel

Reputation: 38771

Use the posix module to raise the limit on the number of file descriptors your process can use.

Install posix

npm install posix

Then in your code that runs when you launch your app...

var posix = require('posix');

// raise maximum number of open file descriptors to 10k,
// hard limit is left unchanged
posix.setrlimit('nofile', { soft: 10000 });

Upvotes: 51

Related Questions