cancel
Showing results for 
Search instead for 
Did you mean: 

Single thread per App instance of a cap node.js

frankreichenbach
Explorer
0 Kudos

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?

junwu
Active Contributor
0 Kudos

you add second instance before the second call or after the second call?

java should be another story, it has multi thread. it should be able to handle multiple request at the same time.

frankreichenbach
Explorer
0 Kudos

The second instanceof the app I added at a later stage to prove to myself that it is not the coding itself. Once the second instance is there, two calls fired shortly after each other are handled in parallel. But if I fire two calls in the scenario when only once instance of the app is available, they are handled sequentially. At least this is how I interprete the behavior.

junwu
Active Contributor
0 Kudos

that should be the expected behavior.

if you fire call before the second instance is live, all your request is sent to first server and queued if the request is long running.

frankreichenbach
Explorer
0 Kudos

Thanks Jun Wu.

@dj.adams, @david.kunz2, @gregorw, @milton, @wouter.lemaire, @carlos.roggan: any comments from your side on the multi thread capabilities of CAP in the node.js and java variant? (besides the option to spawn multiple instances of the deployed app)

Accepted Solutions (1)

Accepted Solutions (1)

marcbecker
Contributor
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)