cancel
Showing results for 
Search instead for 
Did you mean: 

Single thread per App instance of a cap node.js

frankreichenbach
Explorer
0 Kudos
1,070

Sorry about this stupid question, but I just wanted to get confirmation that my understanding is correct..

When providing a cap based service that is built with node.js, is there only one thread available to handle this request? I.e. If I have a long running service handler this blocks/queues any other caller of this service until it is completed.

And thus if I want multiple requests of that cap service, I need to spawn a second instance of the app?

Was making a test with a forced loop in the on-handler of am action and while this was running, triggered a second call of this action. It shows that this is only processing the second request if the first one left the loop. If I add a second instance of the app, it does immediately process a second request.

And would a similar cap service built with the java Version support multiple threads per one instance? And of so how many and how is this controlled?

Accepted Solutions (1)

Accepted Solutions (1)

marcbecker
Product and Topic Expert
Product and Topic Expert
0 Kudos

This is a general question about differences between Node.js and Java. This is not specific to CAP. CAP just embraces the patterns of the underlying runtime.

Node.js uses the Event Loop concept, which basically processes everything in a single worker thread. This works well, for I/O bound scenarios. For CPU bound scenarios this can cause issues (your endless loop is highly CPU bound ;)). Technically it is possible to start multiple such worker threads in a single Node.js process, but I think the more common approach in Node.js is a scale out to multiple processes.

Java is multithreaded and has a thread model, where a new thread is spawned for each incoming request. In Spring Boot and Tomcat the upper bound of request threads can be configured (https://www.baeldung.com/java-web-thread-pool-config#1-embedded-tomcat). With JDK 21 and virtual threads Java will be able to go for a model that takes up some of the advantages of the event loop approach.


Here is some further reading on these topics:
- https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick
- https://snyk.io/blog/node-js-multithreading-worker-threads-pros-cons/
- https://www.infoworld.com/article/3678148/intro-to-virtual-threads-a-new-approach-to-java-concurrenc...
- https://www.baeldung.com/java-virtual-thread-vs-thread

frankreichenbach
Explorer
0 Kudos

Thanks Marc for confirmation of what my testing showed and the links you did share.

Answers (0)