Why is node.js only processing six requests at a time?

There are no TCP connection limits imposed by Node itself. (The whole point is that it’s highly concurrent and can handle thousands of simultaneous connections.) Your OS may limit TCP connections.

It’s more likely that you’re either hitting some kind of limitation of your backend server, or you’re hitting the builtin HTTP library’s connection limit, but it’s hard to say without more details about that server or your Node implementation.

Node’s built-in HTTP library (and obviously any libraries built on top of it, which are most) maintains a connection pool (via the Agent class) so that it can utilize HTTP keep-alives. This helps increase performance when you’re running many requests to the same server: rather than opening a TCP connection, making a HTTP request, getting a response, closing the TCP connection, and repeating; new requests can be issued on reused TCP connections.

In node 0.10 and earlier, the HTTP Agent will only open 5 simultaneous connections to a single host by default. You can change this easily: (assuming you’ve required the HTTP module as http)

http.globalAgent.maxSockets = 20; // or whatever

node 0.12 sets the default maxSockets to Infinity.

You may want to keep some kind of connection limit in place. You don’t want to completely overwhelm your backend server with hundreds of HTTP requests under a second – performance will most likely be worse than if you just let the Agent’s connection pool do its thing, throttling requests so as to not overload your server. Your best bet will be to run some experiments to see what the optimal number of concurrent requests is in your situation.

However, if you really don’t want connection pooling, you can simply bypass the pool entirely – sent agent to false in the request options:

http.get({host:'localhost', port:80, path:"https://stackoverflow.com/", agent:false}, callback);

In this case, there will be absolutely no limit on concurrent HTTP requests.

Leave a Comment