My application receives data from external sources and groups it (matching group items and shows result as HTML table, up to 20k msg/sec).
The problem is shared memory: application works in real time, I receive messages with flags "create, update, delete" so I store all in RAM, no need for any database. But when I try clustering my application messages are missed for some clusters (I tried cluster with pm2).
So now I am trying to scale my application with WorkerThreads, but communication via parentPort.postMessage/worker.postMessage requires a lot of changes to the application. So now I am trying to share memory via SharedArrayBuffer, but I don't understand how to share an array of objects between master and workers.
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
const sab = new SharedArrayBuffer(1024);
sab[0] = {foo: 'bar'};
const worker = new Worker(__filename, {
workerData: sab
});
setInterval(() => {
console.log( sab[0] ); // always {foo: 'bar'}
}, 500);
} else {
let sab = workerData;
sab.foo = false; // changing "foo" value at worker but not in main thread
}
setInterval(() => {}, 1000);