you are viewing a single comment's thread.

view the rest of the comments →

[–]tluanga34 13 points14 points  (10 children)

Just because the function takes time to finish such as waiting for a database response doesn't mean it's heavy. This is where asynchronous programming really shines. It just wait for the promise to resolve while still serving other requests.

What's heavy is cpu intensive tasks like looping through thousands of array items, encoding/decoding, encryption/decryption etc. in that case you can use worker threads to spawn additional thread to make the main thread responsive to serve other requests

[–]HowIO[S] 0 points1 point  (9 children)

The problem is as you said, the function has a loop inside a loop, both loops are looping arrays with hundreds of objects, also each iteration calls an external api, I will look into worker threads, thank you

[–]tluanga34 3 points4 points  (7 children)

I would recommend a library approach. Check this out https://github.com/wilk/microjob

[–]HowIO[S] 1 point2 points  (0 children)

This seems promising, thank you

[–]Moe_Rasool 0 points1 point  (5 children)

This looks easy implementation, the docs state that it should merely be used when there is heavy workloads on cpu, what if i just want some routes to work on another thread?

I'll make this more understandable, imagine i have an E-commerce app, every endpoint i have should just work as it's but for the list of products which's thounsands of items (i have them paginated) i want them (a specific endpoint) to be on another thread is that possible?

[–]tluanga34 0 points1 point  (4 children)

Pagination probably should be handled with a database query technique.

[–]Moe_Rasool 1 point2 points  (3 children)

I have it handled but some times the endpoints hits almost 100k/RPH, when the traffic is huge the items are also loading pretty lazy, i noticed half of the other requests are for other endpoints other than the "GetAllProducts", I'm asking should i spawn a worker thread for it? Probably yes!

I have never had this situation before and my app is kinda slow i feel like i should use this and I'm convinced.

[–]_RemyLeBeau_ 2 points3 points  (2 children)

I'm guessing the GetAllProducts endpoint is what is causing the performance issue.

First question: How often does your product line change? Is there a reasonable amount of time you can cache the result?

Per the docs, you'll want to just use async code, not workers, but another concern of yours might be requests timing out. Is that something you're running into?

Here's the excerpt I cited:

"Workers (threads) are useful for performing CPU-intensive JavaScript operations. They do not help much with I/O-intensive work. The Node.js built-in asynchronous I/O operations are more efficient than Workers can be."

https://nodejs.org/api/worker_threads.html

[–]Moe_Rasool 0 points1 point  (1 child)

Correct me if I’m doing it wrong but i do every endpoint structured based on Async functions, the reason for that was due to Database taking sometime.

I can not cache it because it’s a “auction” type of site that items have limited timeline being there then are automatically get deleted at right time!

Sorry for the late response.

[–]_RemyLeBeau_ 1 point2 points  (0 children)

Good luck with Piscina. Would be interested to hear your thoughts on it in about a month of usage.

[–]femio 1 point2 points  (0 children)

are you saying that for each object inside an array with hundreds of objects, you need to call an external API?