# Node.js Performance Tips: Socket Pooling

**URL:** <https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495>\
**Category:** Knowledge Hub\
**Tags:** apigee\
**Created:** [July 2, 2015, 1:15am UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495 "2015-07-02T01:15:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [July 2, 2015, 1:15am UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495/1 "2015-07-02T01:15:59Z")

</div>

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](http://markdawson.tumblr.com/post/17525116003/node) [well](https://github.com/substack/hyperquest#rant)-[known](https://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin-mobile) [issue](http://webapplog.com/seven-things-you-should-stop-doing-with-node-js/). Fortunately, there are some very simple solutions that will make your application more performant in these scenarios:

- Manually increase the connection pool:

```auto
    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:

```auto
  var http = require('http'); 
  var req = http.request({        
    agent: false,    
    url: "",            
    ...    
  }, function (res) { ... });

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 12, 2016, 6:07am UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495/2 "2016-10-12T06:07:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 25, 2016, 5:44pm UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495/3 "2016-10-25T17:44:43Z")

</div>

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](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](http://bluebirdjs.com/docs/api/promise.map.html#map-option-concurrency)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 11, 2018, 2:07pm UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495/4 "2018-10-11T14:07:34Z")

</div>

where should i add this off code in node.js

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 13, 2018, 11:42am UTC](https://discuss.google.dev/t/node-js-performance-tips-socket-pooling/17495/5 "2018-10-13T11:42:21Z")

</div>

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](http://www.dissertation-help.co.uk/)
