Search code examples
javascriptconnection-poolingbookshelf.jsknex.js

Connection Pools and Billed-Hourly Instances


I'm trying to better understand how connection pools work in knex.

In the future, I'd like to deploy my application to a PaaS with a billed-hourly MySQL datastore. My concern is that using a connection pool (as knex does) will cause me to keep connections open to my datastore that I no longer need, thereby costing me instance hours.

Therefore, my question is: does knex automatically remove connections in the pool after they are not used for an extended period of time? Further, if it's the case that there is no scale-down of connections, what should I do to make sure that I'm not being charged for unused instance hours when all connections in the pool are inactive?

I imagine that, if there is no automatic scale-down, I'll need to be able to access the active connections in the pool in order to ensure I know when all connections are inactive, and then destroy the pool. Unfortunately, I'm not entirely sure how to do that (I couldn't find it in the documentation).


Solution

  • Knex uses Pool2 for pooling with a default of 2 minimum connections open (see knex.js pooling). So your worries are valid if using those defaults.

    To change the behavior try something like:

    var knex = require('knex')({ 
      client: '<your driver, mysql?>', 
      connection: { <your connection parameters> }, 
      pool: { 
        min: 0 /* no connections if idle */, 
        idleTimeout: 10*1000 /* 10 secs idle connections are closed */ 
    }});