When NOT to choose Node

Let’s say a web client asks a NodeJS server to do get factorial of a big number. Since the factorial of a big number would take a lot of CPU time to compute, therefore the thread would be blocked until then. That’s the problem. Since NodeJS is a single-threaded application, if it gets a high CPU-intensive task, it would block the entire thread to complete the job. All the additional requests would just wait.

Examples of CPU-intensive applications: compression, encryption, mathematical calculations, algorithms

When to choose Node

Let’s say the client does a simple get-call. It would first accept the request from the client. Then it would see that a database call needs to be made. It would then give the responsibility of that to a worker and makes the main thread free. When the worker finishes the task and comes back with the response, it then brings the task to its main thread, finishes the required work and returns the response. This way when the thread is not blocked it could use the CPU for performing another task. An application with high IO operations would shine using Node.

But wait, As mentioned how do the workers do their job then? Well, NodeJS is not single-threaded after all. The workers use system kernels and ultimately use threads. But that’s only limited to the workers. Your application will always have a single thread available.

Examples of IO Intensive applications: DNS lookups, Database read/writes, File read/writes.