Skip to content

multithreading nodejs

Sᴛѧʀʟɪɴɢ edited this page Apr 23, 2019 · 8 revisions

The programming language goal is to support multiple (isolated) instances running on the same process without limiting capabilities of the framework since Google’s V8 engine already supports multiple and concurrent isolates. Every single isolate has its own heap memory and garbage collection.

JXcore official page says multiple isolates make JXcore more responsive and capable of handling expensive operations comparing to multi process models because of not having cross process communication and synchronization. Besides the performance gain, it also helps reaching beyond V8’s memory limitation. Every single thread has its own ‘separate’ heap memory space.


Threading at the JS level in node is really ineffective. V8 has a global lock on all JS objects that prevent them from being passed between threads. The only way to allow threading is to serialize and deserialize the objects into new "isolates" (V8 language). V8 does this for some pretty obvious reasons, you wouldn't really want to allow threading at the JS level in a browser, now would you.

You can use threads if you dive down into C++ land, but event then, you should have a really good reason for doing so, because if you're only manipulating objects for processing you're going to have to serialize into a C++ data-structure, process, and then serialize into a JS object. Threads in node are good for doing process intensive work that doesn't require any two way input from the main event loop, otherwise it's very unlikely to be worth your time or effort.

This is why fibers are kind of silly for node and is also why process intensive programs should most likely be avoided in node (with rare exception). Please use node for what it is good at; it's a short lived connection proxy maven, and good at ancillary front-end work that requires complex state transitions (i.e. mobile). Working it into a monolithic stack is just asking for trouble and pain. No serious architect should consider it for a large monolithic rendering stack.


In a multi-process model it's easy and cheap to expose everything.

In a multi-thread model a lot of work has to be done for seemingly trivial things. Example: the current working directory is a per-process property, not per-thread. Node needs to be taught to maintain a per-thread working directory and all file system operations must use it henceforth. That requires major surgery to the fs module, process.chdir(), the module loader, etc. Native add-ons probably cannot be made to work at all.

Another problem with the multi-thread model that the multi-process model doesn't have is that the address space on 32 bits architectures is limited to ~2 GB. A V8 isolate needs a lot of virtual memory to do anything interesting. 10 or 20 threads tops will exhaust the address space and kill the process.

I'd look into the multi-process model + shared memory. That lets you support SharedArrayBuffer without the headaches of the multi-thread model.

Cross-platform shared memory has issues of its own but they are tractable: it's mostly working around platform quirks and limitations. Drudge work but nothing that is insurmountable.


The programming language goal is to support multiple (isolated) instances running on the same process without limiting capabilities of the framework since Google’s V8 engine already supports multiple and concurrent isolates. Every single isolate has its own heap memory and garbage collection.


https://medium.com/devschacht/a-cartoon-intro-to-arraybuffers-and-sharedarraybuffers-952198b0a1c9
http://aosabook.org/en/posa/from-socialcalc-to-ethercalc.html#multi-core-scaling
https://news.ycombinator.com/item?id=6834179
https://tproger.ru/translations/guide-to-threads-in-node-js/
https://itnext.io/multi-threading-and-multi-process-in-node-js-ffa5bb5cde98
https://medium.com/lazy-engineering/node-worker-threads-b57a32d84845
https://stackoverflow.com/questions/51053222/nodejs-worker-threads-shared-object-store
https://medium.com/@jsmrcaga/experimenting-with-nodes-worker-threads-a297f722b50d
https://habr.com/ru/company/ruvds/blog/437984/
https://stackoverflow.com/questions/25519325/share-objects-in-nodejs-between-different-instances
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics
https://github.com/tc39/ecmascript_sharedmem/blob/master/TUTORIAL.md
https://lucasfcosta.com/2017/04/30/JavaScript-From-Workers-to-Shared-Memory.html
https://stackoverflow.com/questions/48346490/what-does-the-atomics-object-do-in-javascript
https://www.alibabacloud.com/blog/improving-redis-performance-through-multi-thread-processing_594150
https://github.com/audreyt/node-webworker-threads