How Node really works · Chapter 24 of 34
Worker threads
By the end of this chapter you can move heavy JavaScript off the main thread, talk to it with messages, and reuse a few workers for many jobs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The system
- 5 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
On the last evening of the month Meera clicked "monthly report" in the 7 pm rush, and for a few seconds nobody could place an order. Every UPI confirmation and rider update waited while the server added up thirty days of bills. Kabir wants the report to run without freezing anything.
In chapter 1 you split long work into chunks with setImmediate. That still runs every chunk on the one thread, and not every job splits nicely. node:worker_threads gives you a second JavaScript thread, with its own event loop, inside the same process.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
totalMonth: total a month of bills on the main thread first
Write totalMonth(days). days has one entry per day, and each day is an array of orders { price, qty }. It returns { total, count, busiestDay }: total is the sum of price times qty, count is the number of orders, and busiestDay is the day number (1 for the first day) with the biggest day total.
On a tie the earliest day wins. An empty day still counts as a day, with a total of 0. A month with no days at all gives { total: 0, count: 0, busiestDay: null }.
When an order has a negative price or qty, it throws an Error whose message names the day, like 'Bad order on day 3'.
- Task 2
month-worker.js: total the month in its own thread
month-worker.js runs in a worker thread that was started with workerData { days }. It totals the month with totalMonth and posts exactly one message to the parent: the three totals plus this thread's threadId, as { total, count, busiestDay, threadId }.
When totalMonth throws, the worker must not catch it: the error has to reach the parent's error event.
- Task 3
totalInWorker: move the month-end report off the main thread
Write totalInWorker(days). It starts a Worker running month-worker.js (MONTH_WORKER), with workerData { days }, and returns a promise of the worker's message, { total, count, busiestDay, threadId }, where threadId is the worker's, above 0.
When the worker throws (a bad order), the promise rejects with that error, whose message names the day. If the worker stops without answering, the promise rejects too.
- Task 4
createPool: share the work between two reusable workers
Write pool-worker.js: a long-lived worker that answers every message { id, days } with { id, result }, where result is the totals of days with this thread's threadId added, or with { id, error }, where error is the error's message, when totalMonth throws. It keeps running between jobs.
Write createPool(size), with size 2 when it is left out. It starts size workers running pool-worker.js (POOL_WORKER) once, up front, and returns { run, close }. run(days) returns a promise of the totals with the threadId of the worker that did the job. Each worker does one job at a time; jobs wait in a queue while every worker is busy, and go to whichever worker is free next. No new workers are started, so five jobs on a pool of two all run on the same two threads.
A bad month rejects with an Error carrying the worker's message (it names the day), and the pool keeps working on the same workers. close() terminates every worker and resolves once they have all stopped.
- Task 5 · interview question
Interview: bestTool, pick worker threads, child processes or cluster
Fill bestTool: for each of its six jobs, write exactly 'worker' (worker_threads), 'child' (child_process) or 'cluster'. Keep all six keys as they are.
What you will learn
- Worker, workerData and parentPort: start a thread with a file and some data, and hear back from it.
- Copies and shared memory: what a message can carry, and the one kind of memory two threads can share.
- Errors and exits: what the parent sees when a worker throws.
- A pool: start a few workers once and give them job after job.
More in The system
Build "Worker threads" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.