简体   繁体   中英

How does mysql connection pooling works with Node microservices?

I have two node microservices talking to one common mysql database. Both microservices have this below code to create a connection pool with a connectionlimit of 10 as follows:

// Initializing pool

var pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'root'
});

function addConnection(req, res) {
  pool.getConnection(function (err, connection) {
    if (err) {
      connection.release();
      res.json({ "code": 500, "status": "Error" });
      return;
    }

    connection.query("select * from user", function (err, rows) {
      connection.release();
      if (!err) {
        res.json(rows);
      }
    });

    connection.on('error', function (err) {
      res.json({ "code": 500, "status": "Error" });
      return;
    });
  });
}

For mysql database I have the max_connections set to 200(SHOW VARIABLES LIKE 'max_connections'; returns 200).

  1. With the pool connectionLimit set to 10 for each of the microservice, in which cases or scenarios will the number of connections for any of the microservice will go above 10? ie When and how the node services would be able to maintain more connections then expected?
  2. If I have 3 instances running of same microservice then how does the pool connectionLimit works? What would be the limit of connections for each instance of microservice?
  3. In one of the microservice say I have two apis which does database transactions and both connects to the database(getting connection) through two different functions having same implementation of mysql.createPool({}) as above. What would happen if both apis are called concurrently and the number of requests made for each of them per second is 100 or more? Will the number of connections made available be 10 or 20(since there are two mysql pools created with a connectionLimit of 10 each)?
  1. Ideally it would not; but it can go above 10; if suppose some connections become stale ie they are closed from client end but are still open on Server end.

  2. If you have multiple instances of same micro-service deployed in multiple VM or docker containers; they are like independent services.. and they have no connection among each other.. Hence, each of them will create its own 10 connection.

  3. Firstly if u set connection pool limit as 10; that does NOT mean that during first moment 10 connections would be created.. While creating a pool; you also specify initial connection parameter suppose 5.. so, when service starts only 5 connections would be created.. and more created only when needed.. with UPPER Limit set as defined by parameter max_connections. Coming back to your question; well if you have NOT implemented synchronization etc. properly then yes it is possible that both pools will initialize their INITIAL_CONNECTIONS..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM