title: What do I get this Error: EADDRINUSE, Address already in use when stress-testing Node.js with cradle and CouchDB?
body: <p>I am trying to measure the throughput of a simple Node.js program with a CouchDB backend using cradle as the DB driver. When I put load against the program I get the following error within 30 seconds:</p>
  
  <p>EADDRINUSE, Address already in use</p>
  
  <p>Here is my program:</p>
  
  <pre><code>var http = require ('http'),
      url = require('url'),
      cradle = require('cradle'),
      c = new(cradle.Connection)('127.0.0.1',5984,{cache: false, raw: false}),
      db = c.database('testdb'),
      port=8081;
  
  http.createServer(function(req,res) {
      var id = url.parse(req.url).pathname.substring(1);  
      db.get(id,function(err, doc) {
        res.writeHead(200,{'Content-Type': 'application/json'});
        res.write(JSON.stringify(doc));
        res.end();
      });
  }).listen(port);
  
  console.log("Server listening on port "+port);
  </code></pre>
  
  <p>I am using a JMeter script with 50 concurrent users. The average response time is 120ms, average size of the document returned 3KB.</p>
  
  <p>As you can see I set the caching of Cradle to false. To investigate I looked at the  number of waiting sockets: It increases up to about 4000, at which point it crashes (netstat | grep WAIT | wc -l)</p>
  
  <p>To test other options I set the caching to true. In this case the program doesn't crash, but the number of waiting sockets increases to almost 10000 over time.</p>
  
  <p>I also wrote the same program (sans the asynchronous part) as a Java Servlet, and it runs fine without the number of waiting sockets increasing much beyond 20.</p>
  
  <p>My question is: Why do I get the ' EADDRINUSE, Address already in use' error? Why is the number of waiting sockets so high?</p>
  
  <p>P.S.: This is a snippet from the output of netstat|grep WAIT:</p>
  
  <pre><code>tcp4       0      0  localhost.5984         localhost.58926        TIME_WAIT
  tcp4       0      0  localhost.5984         localhost.58925        TIME_WAIT
  tcp4       0      0  localhost.58924        localhost.5984         TIME_WAIT
  tcp4       0      0  localhost.58922        localhost.5984         TIME_WAIT
  tcp4       0      0  localhost.5984         localhost.58923        TIME_WAIT
  </code></pre>