title: How to optimize Postgresql max_connections and node-postgres connection pool?
body: <p>In brief, I am having trouble supporting more than 5000 read requests per minute from a data API leveraging Postgresql, Node.js, and node-postgres. The bottleneck appears to be in between the API and the DB. Here are the implmentation details.</p>
  <p>I'm using an AWS Postgresql RDS database instance (m4.4xlarge - 64 GB mem, 16 vCPUs, 350 GB SSD, no provisioned IOPS) for a Node.js powered data API.  By default the RDS's max_connections=5000.  The node API is load-balanced across two clusters with 4 processes each (2 Ec2s with 4 vCPUs running the API with PM2 in cluster-mode).  I use <a href="https://github.com/brianc/node-postgres" rel="noreferrer">node-postgres</a> to bind the API to the Postgresql RDS, and am attempting to use it's connection pooling feature.  Below is a sample of my connection pool code:</p>
  <pre><code>var pool = new Pool({
      user: settings.database.username,
      password: settings.database.password,
      host: settings.database.readServer,
      database: settings.database.database,
      max: 25, 
      idleTimeoutMillis: 1000
  });
  
  /* Example of pool usage */
  pool.query('SELECT my_column FROM my_table', function(err, result){
      
      /* Callback code here */
  });
  </code></pre>
  <p>Using this implementation and testing with a load tester, I can support about 5000 requests over the course of one minute, with an average response time of about 190ms (which is what I expect).  As soon as I fire off more than 5000 requests per minute, my response time increases to over 1200ms in the best of cases and in the worst of cases the API begins to frequently timeout. Monitoring indicates that for the EC2s running the Node.js API, CPU utilization remains below 10%. Thus my focus is on the DB and the API's binding to the DB.</p>
  <p>I have attempted to increase (and decrease for that matter) the node-postgres &quot;max&quot; connections setting, but there was no change in the API response/timeout behavior. I've also tried provisioned IOPS on the RDS, but no improvement. Also, interestingly, I scaled the RDS up to m4.10xlarge (160 GB mem, 40 vCPUs), and while the RDS CPU utilization dropped greatly, the overall performance of the API worsed considerably (couldn't even support the 5000 requests per minute that I was able to with the smaller RDS).</p>
  <p>I'm in unfamilar territory in many respects and am unsure of how to best determine which of these moving parts is bottlenecking API performance when over 5000 requests per minute. As noted I have attempted a variety of adjustments based on the review of Postgresql configuration documentation and node-postgres documentation, but to no avail.</p>
  <p>If anyone has advice on how to diagnose or optimize I would greatly appreciate it.</p>
  <h3>UPDATE</h3>
  <p><em><strong>After scaling up to m4.10xlarge, i performed a series of load-tests, varying the number of request/min and the max number of connections in each pool.  Here are some screen captures of monitoring metrics:</strong></em></p>
  <p><a href="https://i.stack.imgur.com/oTepq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oTepq.png" alt="monitoring metrics" /></a></p>
  <p><a href="https://i.stack.imgur.com/LBhEx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LBhEx.png" alt="db connections" /></a></p>