How to use nodeJS cluster with mySQL pool cluster?

Quick question

If I create a cluster node application with four workers (4 instances of my application), should I use the mySQL pool or the mysql pool cluster? If I use a pool, it will create one pool for each application, but if I use a pool cluster, it will create 4 pools for each application (16 in total). Is this a good implementation or will it actually slow down?

Let me make a fake example to illustrate what I ask. I am creating a nodeJS server application like this.


First, let's create a configuration file for the mysql database (the last section of this file is important in which I do the db work):

DBconfig.js

'use strict'
const mysql   = require('mysql'),
      workers = process.env.WORKERS || require('os').cpus().length,
      cluster = require('cluster');

//Local Database Settings
const local_settings = {
    user       : 'user',
    host       : '127.0.0.1',
    password   : 'pass',
    database   : 'dbname',
    debug      : false,
    dateStrings: true,
    connectionLimit     : 10,
    defaultSelector     : 'RR',
    multipleStatements  : true,
    removeNodeErrorCount: 1
};

let poolCluster = module.exports = mysql.createPoolCluster( local_settings );

//here I make one db worker for each app worker
for(let i = 0; i < workers; i++){
    poolCluster.add(`DBWORKER_${process.pid}_${i}`, local_settings);
}

,

globalLib.js

'use strict'
const path        = require('path'),
      poolCluster = require(path.join('path','to_the','DBconfig.js'));

global.db = obj => {
  return new Promise( (resolve, reject) => {
    poolCluster.getConnection(function(err, connection){
        if(err) {reject(err);return}

        connection.query(obj.query, obj.params, function(){
            connection.release();
            if(!err) resolve(rows)
            else reject(err);
        });

        connection.on('error'), function(err){
            reject(err);
        });
    }
  })
}

app.js

'use strict'
const express = require('express'),
      path    = require('path');
let   app     = module.exports = express();

//here I handle all with express.Router()
const index = require(path.join('path','to_my','index.js'));

app.use('/', index)

, ( ):

cluster.js

'use strict'
const path    = require('path'),
      cluster = require('cluster'),
      sockets = require(path.join('path_to','sockets.js')),
      app     = require(path.join('path_to','app.js')),
      pclust  = require(path.join('path_to', 'DBconfig.js')),
      workers = process.env.WORKERS || require('os').cpus().length;

if (cluster.isMaster) {

    for (var i = 0; i < workers; ++i) {
        var worker = cluster.fork();
    }

    cluster.on('disconnect', function(worker) {
        pclust.remove(`DBWORKER_${worker.process.pid}_*`);//remove all connections relative to this process pid
        var worker = cluster.fork();
    });
}
else {

    //Start Application
    var server = app.listen(8080, '127.0.0.1', function(){
        console.log('yeeeeey');
    });

    // initialize socket
    sockets.initialize(server,app);
}

, 4 cpus, 4 .js. 4 , :

cpuID    server instances    db workers

 1            1                 4

 2            1                 4

 3            1                 4

 4            1                 4

 4            4                 16 ( TOTAL )

, db... , nodeJS mySQL? ?

+4

Source: https://habr.com/ru/post/1681307/


All Articles