Menu

Post image 1
Post image 2
1 / 2
0

Why worker pools beat clustering for CPU-Heavy tasks on Node.js

DEV Community·Jancer Lima·28 days ago
#kK9s5awW
Reading 0:00
15s threshold

Imagine you have a Nodejs server with endpoint that performs heavy CPU operations. By default your server runs on a single thread. This means it will freeze depending on the CPU load. If your server has other asynchronous endpoints, for example, to execute database operations, those endpoints would become unresponsive while the heavy load endpoint is processing. Our first idea is to create more threads, sending the heavy tasks to be processed in parallel by another CPU core. Once finished, we send the output back to the main thread and return the answer to the client. The problem now is that we can not have more threads than available CPU cores (technically we can but it does not make much sense) so we start thinking about using worker pools where we instantiate an fixed amount of workers and reuse them to our desired tasks. Now we have a stable structure where we offload CPU intensive tasks to other threads to make the main thread free and available for new requests.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More