Search code examples
mysqlnode.jsnode-cluster

How can I share one resource across Node clusters


I am using node cluster module, and each worker loads a database connection.

index.js

const cluster = require('cluster');
const database = require('./db.js');
if (cluster.isMaster) {
   cluster.fork();
   cluster.fork();
} else {...}

db.js

const mysql = require('mysql');
const pool = new mysql.pool(config);
module.exports = function(query){
    return pool.query(query);
}

My understanding is that each time a worker is spawned, it will initialize db.js, which will create a new pool/connections to mysql.

Is there another way to structure this so that all workers share the same mysql pool?


Solution

  • It is not possible to share resources between Node.js workers and master. Each one of these is a new process and all you can do is to communicate via messages (docs here).

    Based on the Node.js docs:

    The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.

    And for each child_process we have:

    It is important to keep in mind that spawned Node.js child processes are independent of the parent with exception of the IPC communication channel that is established between the two. Each process has its own memory, with their own V8 instances. Because of the additional resource allocations required, spawning a large number of child Node.js processes is not recommended.

    Depending on your application architecture, you might be able to move the resource to be shared, to the master, and distribute the work to workers.