Configuring Worker Threads
For enable multi-threading you can use fileSystemRouter({ workers: true })
which creates
a thread pool up to the number of available logical processors or use specify the number of workers using fileSystemRouter({ workerCount: 4 })
.
Worker pool
The workers are spawn inside a WorkerPool
, you can specify the worker using the pool
option:
By default it uses the WorkerPoolType.Fixed
pool, you can also pass function that returns a WorkerPool:
How it works?
Internally we use node Worker API, and create a pool of workers. When a request comes we take an available worker and serialize the request and send it to the worker and then deserialize the response of the worker and send it as a response.
Each worker have its own router which is used to handle the request.
Is not multi-threading always faster?
No.
We recommend profiling your app before enabling workers, unless you are handling a big number of request you may not see any difference when enabling workers, it may be even slighly inneficient to using workers for a few number of request per second.
Checkout the Worker threads example.