title: Tune nodejs + expressjs to handle many concurrent requests
body: <p>How do I tune nodejs (6.1.0) and expressjs (4.13.4) to handle a large number of concurrent connections.  </p>
  
  <p>To demonstrate that express can handle many connections I wrote the following javascript:</p>
  
  <pre><code>var Q = require('q');
  var needle = require('needle');
  var logger = require('log4js').getLogger("deleteme");
  
  var batches = 2;
  var batchSize = 200;
  
  var express = require('express');
  var app = express();
  
  app.get('/', function(req, res){
    res.send('hello world');
  });
  
  var server = app.listen(8001);
  
  
  var requestBatches = [];
  for(var batchIndex = 0; batchIndex &lt; batches; ++batchIndex) {
    const thisBatch = batchIndex;
    requestBatches.push(Q.delay(batchIndex * 1000 + 1).then(function() {
      var requests = [];
      var errors = 0;
      for( var queryIndex = 0; queryIndex &lt; batchSize; ++queryIndex) {
        requests.push( Q.nfcall(needle.get, "http://localhost:8001/", {timeout: 1000}).catch(function(error) {
          logger.error("Error: ", error.message);
          ++errors;
          throw error;
        }))
  
      }
      return Q.allSettled(requests).then(function() {
        if(errors) {
          logger.error("Batch: ", thisBatch, " failed ", errors, "/", batchSize)
        } else {
          logger.info("Batch: ", thisBatch, " all success")
        }
      })
  
  
    }));
  
  
  }
  
  
  Q.allSettled(requestBatches).finally(function() {
    logger.info("all done");
    server.close();
  });
  </code></pre>
  
  <p>All this basically does is starts a server on port 8001, send several batches of connections at it, calculate the success rate for batches, and report the result.</p>
  
  <p>Right now I am pouncing on the server with 200 connections, I would expect nodejs to slow down the responses but instead I get connection disconnects.  </p>
  
  <p>It varies run to run, sometimes I have 2 connection resets, sometimes I have a ton.  Sometimes I have socket hangups.  </p>
  
  <p>What can I do to tune expressjs and nodejs to handle more concurrent connections?</p>
  
  <p>Sample result:</p>
  
  <pre><code>[2016-11-14 14:07:58.186] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:07:58.385] [ERROR] deleteme - Batch:  0  failed  1 / 200
  [2016-11-14 14:07:59.280] [INFO] deleteme - Batch:  1  all success
  [2016-11-14 14:07:59.281] [INFO] deleteme - all done
  </code></pre>
  
  <p>With a great many failures:</p>
  
  <pre><code>[2016-11-14 14:09:54.544] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.547] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.548] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.548] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.548] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.549] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.550] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.550] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.551] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.551] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.551] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.551] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.552] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.552] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.552] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.552] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.553] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.553] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.553] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.554] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.554] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.554] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:54.745] [ERROR] deleteme - Batch:  0  failed  22 / 200
  [2016-11-14 14:09:55.501] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.501] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.502] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.502] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.502] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.503] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.503] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.503] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.504] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.505] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.505] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.506] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.506] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.506] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.506] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.507] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.510] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.510] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.510] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.510] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.510] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.511] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.511] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.511] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.512] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.512] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.512] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.512] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.513] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.513] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.514] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.514] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.514] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.514] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.515] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.515] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.515] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.516] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.516] [ERROR] deleteme - Error:  connect ECONNRESET 127.0.0.1:8001
  [2016-11-14 14:09:55.627] [ERROR] deleteme - Batch:  1  failed  39 / 200
  [2016-11-14 14:09:55.627] [INFO] deleteme - all done
  </code></pre>