I am using nodejs for a web server which decodes the GET params and returns data in some encoded format. The decode/encode are done using the crypto module of nodejs, which seems to be synchronous. While the time taken to serve a single request is fast enough, blocking the event loop has made the service perform poorly with concurrency.
My requirement is simple, make the encode/decode functionality outside of the event loop.
This can either be a separate process solely for this purpose, but since the encode/decode will be blocking in the child process this will stop the child process to receive new messages i.e. there will never be a situation when two strings are encoded as child process will also be single threaded.
Create a separate thread for each request to carry out the encode/decode operation, but none of the modules seems to work as expected, i.e threads-a-gogo doesn't install through npm, fiber didn't create a separate thread on run(), node-webworker not working.
Has someone faced a similar problem or is there some way to easily create threads in nodejs with simple message passing.
Since Node. js is single-threaded and non-blocking, you can achieve higher concurrency with the same resources". And when you read about what it's bad at it usually goes like this: "Since Node. js is single-threaded, CPU-intensive tasks will block all requests from completing, until the task is completed.
Node. js provides developers a system with single-threaded event loop architecture that provides a non-blocking I/O mechanism. This works great until we get to CPU-intensive tasks.
In computer science, a computer is CPU-bound (or compute-bound) when the time for it to complete a task is determined principally by the speed of the central processor: processor utilization is high, perhaps at 100% usage for many seconds or minutes.
Sorting, search, graph traversal, matrix multiply are all CPU operations, a process is CPU-intensive or not it depends on how much and how frequent are their execution.
This is built into node's child processes. Docs here:
http://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle
You could also use cluster:
http://nodejs.org/api/cluster.html#cluster_cluster_fork_env
With cluster, it would work something like:
if (cluster.isMaster) {
var worker = cluster.fork();
worker.send('encodeThisString');
} else if (cluster.isWorker) {
process.on('message', function(msg) {
var encrypted = crypto.doSomeEncryption(msg);
process.send(encrypted);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With