1

I've heard that node.js is fast when it performs IO tasks like querying a database and because javascript is single threaded and uses event loop, it uses a lot less resources when comparing to using a separate thread whenever a concurrent database query is made. But doesn't that mean the maximum number of concurrent queries it can make is still limited by how many concurrent connections a particular database can handle? If that's the case, what are the advantages of node.js over something like vert.x, which can use multiple event loops instead of just one.

1
  • 2
    In node.js max concurrent queries that are effective is dependent upon how many concurrent queries your database can handle. That depends entirely upon your database, it's hardware and how it is deployed and configured. That number could be a couple concurrent queries or hundreds. node.js can efficiently do either. Commented May 21, 2020 at 3:36

1 Answer 1

1

Since node.js 12 (it's available since node.js 10 as "experimental feature") you can use worker threads like you're maybe already using worker verticle with vert.x. You also have the child process with node or cluster mode.

Either ways (vert.x or node) you need to keep in mind the golden rule aka "do not block the eventloop or worker pool" and think about the size of your thread pool according to your capacity.

I think it's often better to keep you process monothreaded and to scale it as new and completely isolated instances that can be hosted on multiple places in your infrastructure or even in the same place (using docker/kubernetes/whatever or even with systemd for example). And dynamically perform the scaling according to the requests increase, your infrastructure's ability to scale and of course the size of the connections pool your database can handle.

When you're designing your application as a reactive or event-driven one (the right way to use node or vert.x) and a stateless one, it's easier to handle the scaling that way.

Sign up to request clarification or add additional context in comments.

8 Comments

@ChungYang I guess like every libs/tools that allow us to handle multithreading: you need to define a pool of worker threads with a reasonable size and every thread are run on the available CPUs or waiting for an available CPU. But I think it's almost an anti-pattern to handle parallelism that way with node or vert.x.
No, I mean if javascript is single threaded. How does node get around that issue and implements multithread if that's not supported by the language construct.
@ChungYang First, it's not the javascript language that is monothreaded (a.k.a "run on a single core/CPU") by itself but node. And node is not always monothreaded. It's not working on a single core anymore when you're using thread worker or child process or cluster mode. You should read the documentations I sent to see how you can use multiples core to execute some functions or even monitor your CPUs.
@ChungYang The separate threads each have it's own js interpreter. Basically the worker threads architecture is not to make javascript multithreaded but make node.js (a software written in C) multithreaded running multiple singlethreaded javascript interpreters
ok, I think I have some misconceptions about node and js. I will do more reading but your answer is very helpful. Thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.