node.jsaws-lambdaproxymysql2connection-pool

Mysql Connection vs Connection Pool


I have 4 separate tables in the same database. Would it be better to use mysql2.createConnection() or mysql2.createPool to bulk insert into each table? I'd like to run the inserts asynchronously.

The code will be executing the inserts from an AWS Lambda and connections are done through RDS Proxy which handles connection pooling for all connections to the Aurora MySql database instance.

const mysql2 = require('mysql2');

const connection = mysql2.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

or

mysql2.createPool

const mysql2 = require('mysql2');

const pool  = mysql2.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret' 
});

Solution

  • If you would like to run the inserts asynchronously, you will want createPool.

    Because in with createConnection, there is only 1 connection and all queries executed on that connection are queued, and that is not really asynchronous. (Async from node.js perspective, but the queries are executed sequentially)