Replies: 1 comment 2 replies
-
Hi @webdpro, nice question btw. In this case we need to be conscious of what Piscina does under the hood. As the name states, an instance of Piscina creates a pool (with a customizable size) of Worker Threads that will distribute the load of tasks (high-level explanation). Then, moving to back to your question, if you have an implementation where on every request you create a new Piscina instance, you’ll be creating N^T number of threads, where N is the number of incoming requests and T is the number of Threads that you told Piscina to instantiate. Which, just by looking at this we can perceive that its a huge overload to the service. The best in this case is to create a single Piscina instance that is shared across the lifetime of the Service (of course assuming your server is stateless). You can prepare and instantiate the Piscina instance before starting to receive incoming requests so your Pool is ready to handle tasks beforehand. From an architectural side, an even better approach is to totally offload this expensive tasks to a secondary service by using some sort of asynchronous communication protocol, so your front-end server can continue receive incoming requests as needed meanwhile processing async the expensive data task. Hope it solves your question 🙂 |
Beta Was this translation helpful? Give feedback.
-
Hey everyone,
Have a question in regards to the proper setup and use.
Now, the docs have a basic setup when you create a 'new' instance of piscina with the 'worker' file you want to have executed.
My question is, in a production environment, say on a 'route' would you create a 'new' instance every time or is it more performant to have this in a module, then use that in the route?
For example, a module that contains the 'new instance' and exports that, then in the route just call the 'run' OR is it better to create the 'new' instance in the actual route itself, however I'm not sure if doing that would me a new 'thread' for each new item, that could get costly I would assume.
So the use-case we have is that we have routes that make DB calls, then run some code with the results to return those objects, while the DB execution is not blocking, the manipulation of data is, again its not slow, but if multiple people are hitting at once, those cycles can add up, hence the worker thread.
Really, the question revolves around how best to set this up though :D
Thanks for any help you can provide.
Beta Was this translation helpful? Give feedback.
All reactions