0
0
Node.jsframework~10 mins

Worker pool pattern in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Worker pool pattern
Start main thread
Create worker pool
Assign tasks to workers
Worker executes task
Worker sends result back
Main thread collects results
Check if more tasks
Assign next
The main thread creates a pool of workers, assigns tasks to them, collects results, and repeats until all tasks are done, then shuts down the pool.
Execution Sample
Node.js
import { Worker } from 'worker_threads';

const pool = [];
for(let i=0; i<2; i++) pool.push(new Worker('./worker.js'));

pool[0].postMessage('task1');
pool[1].postMessage('task2');
This code creates two workers and sends each a task message to start processing.
Execution Table
StepActionWorker StateMain Thread StateMessage Passing
1Create Worker 0IdlePool: [Worker0]No message
2Create Worker 1IdlePool: [Worker0, Worker1]No message
3Send 'task1' to Worker 0Busy with task1Pool unchanged'task1' sent to Worker0
4Send 'task2' to Worker 1Busy with task2Pool unchanged'task2' sent to Worker1
5Worker 0 processes task1Processing task1Waiting for resultNo message
6Worker 1 processes task2Processing task2Waiting for resultNo message
7Worker 0 sends resultIdleReceived result1'result1' received from Worker0
8Worker 1 sends resultIdleReceived result2'result2' received from Worker1
9Check for more tasksIdleNo more tasks, shutdown poolNo message
10Shutdown workersTerminatedPool emptyNo message
💡 All tasks processed and results collected, workers shut down.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
pool[][Worker0, Worker1][Worker0, Worker1][Worker0, Worker1][]
Worker0 stateIdleBusy with task1Processing task1IdleTerminated
Worker1 stateIdleBusy with task2Processing task2IdleTerminated
Main thread results[][][][result1][result1, result2]
Key Moments - 3 Insights
Why does the main thread wait after sending tasks?
Because workers run tasks asynchronously, the main thread waits to receive results before proceeding, as shown in steps 5 and 6 where workers process tasks while the main thread waits.
What happens if a worker finishes a task before others?
The worker sends its result back and becomes idle, ready for new tasks. This is shown in step 7 where Worker0 finishes and sends result while Worker1 is still processing.
Why do we shut down workers at the end?
To free system resources once all tasks are done, as shown in step 10 where workers terminate after no more tasks remain.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is Worker0's state at step 6?
AProcessing task1
BIdle
CBusy with task2
DTerminated
💡 Hint
Check the 'Worker State' column at step 6 in the execution_table.
At which step does the main thread receive the first result?
AStep 5
BStep 8
CStep 7
DStep 10
💡 Hint
Look at the 'Main Thread State' and 'Message Passing' columns for when 'result1' is received.
If we add a third worker, how would the pool variable change after creation?
Apool would have 1 worker
Bpool would have 3 workers
Cpool would have 2 workers
Dpool would be empty
💡 Hint
Refer to the variable_tracker 'pool' row after worker creation steps.
Concept Snapshot
Worker Pool Pattern in Node.js:
- Create multiple Worker threads in a pool.
- Assign tasks to idle workers asynchronously.
- Workers process tasks and send results back.
- Main thread collects results and assigns new tasks.
- Shutdown workers when all tasks complete.
Full Transcript
The Worker Pool Pattern in Node.js helps run multiple tasks in parallel using worker threads. First, the main thread creates a pool of workers. Then, it sends tasks to each worker. Workers run tasks independently and send results back. The main thread waits for these results and can assign more tasks if needed. When all tasks are done, the main thread shuts down the workers to save resources. This pattern improves performance by using multiple threads efficiently.