Node.js Performance Tips: Socket Pooling

Apigee Edge supports Node.js version 0.10. In this version of Node, the default connection pool for outgoing http requests to a host is 5. This can be a significant bottleneck if you have many concurrent requests to the same host. This is a very well-known issue. Fortunately, there are some very simple solutions that will make your application more performant in these scenarios:

  • Manually increase the connection pool:
    var http = require('http');    
    http.globalAgent.maxSockets = 100;
    // You could also set it to unlimited (Node v0.12 does by default):
    http.globalAgent.maxSockets = Infinity;
  • Disable HTTP global Agent:
  var http = require('http'); 
  var req = http.request({        
    agent: false,    
    url: "",            
    ...    
  }, function (res) { ... });
Comments
Not applicable

Are there any side-effects of increasing the size of the connection pool or disabling connection pooling (such as increased memory foot-print) that one needs to be aware of when making these changes and would any changes be required in those areas where side-effects could be caused?

Not applicable

If requests sent from Node.js aren't throttled, increasing the pool size can be too high, so that too many requests hit the backend and bring it down. I have used NPM modules such as Throat to manage concurrency of parallel promises https://www.npmjs.com/package/throat.

Also Bluebird module comes with some handy affordances to limit concurrency with Promises. http://bluebirdjs.com/docs/api/promise.map.html#map-option-concurrency

radhika_talluri
Explorer

where should i add this off code in node.js

Not applicable

I read somewhere that static files should be served from Nginx which sits in front of Node.js. Why is it not recommended to serve from Node.js directly? Is express.static() not good for production usage?

Jason Walsh

Version history
Last update:
‎07-01-2015 06:15 PM
Updated by: