Node.js Green Threads: 8 OS Threads Run 1,000 Isolated V8 Isolates, Beating Workers
Node.js adds an experimental Green Threads module (node:green_threads) that runs thousands of isolated V8 isolates on a shared pool of 8 OS threads, cutting per-instance memory from 6.2 MB to 1.4 MB and achieving higher QPS than Worker threads while providing strong fault isolation for multi-tenant scripting.
Node.js Introduces Experimental Green Threads
Node.js core contributor James M Snell submitted a 14,000-line pull request (PR #67288: https://github.com/nodejs/node/pull/67288) adding an experimental module node:green_threads. The PR has been approved by core maintainers Matteo Collina and Robert Nagy and is slated to land as a Stability 1.0 experimental feature.
What Are Green Threads?
Green Threads are not goroutines. Traditional coroutines share a single address space and swap call stacks. In contrast, each Green Thread gets its own V8 Isolate with a private heap and garbage collector; JavaScript objects are never shared between isolates. There is no dedicated OS thread per isolate and no independent event loop. Instead, all Green Threads in a process share a small OS thread pool (default maximum 8 threads). When a Green Thread receives a message or a timer fires, the scheduler places it on a free pool thread; once the task finishes, the thread is immediately returned to the pool. Idle isolates consume only memory, not threads.
Why Not Worker Threads or vm Contexts?
Developers building multi-tenant script hosting, plugin systems, or AI agent sandboxes face a dilemma:
Worker threads provide strong isolation but each worker requires a dedicated OS thread, a full Node.js environment, and an event loop. A single worker starts at ~6.2 MB memory; 1,000 workers would consume over 6 GB.
vm contexts are cheap (~0.25 MB per context) but share the same V8 Isolate and heap. The official documentation states they are not a security boundary. A microtask infinite loop or heap exhaustion in one context crashes the entire worker, taking all co-located tenants down.
Green Threads achieve isolation by stripping away the bound OS thread, the heavy Node.js Environment, and global objects, leaving only a clean V8 Isolate. They also support ES Module exports, host env injection, and per-instance CPU time and memory limits.
Code Example
import { GreenThread } from 'node:green_threads';
const plugin = new GreenThread(
`export async function transform(text) {
const prefix = await env.config('prefix');
return prefix + text.toLowerCase().replaceAll(' ', '-');
}`, {
type: 'module',
env: {
async config(key) {
return { prefix: 'post-' }[key];
},
},
resourceLimits: { maxTaskCpuTimeMs: 100, maxMemoryMb: 32 },
});
const { transform } = await plugin.exports;
console.log(await transform('Hello World')); // post-hello-world
await plugin.terminate();The maxTaskCpuTimeMs limit counts only actual CPU time; waiting for timers or host async calls does not count. If a plugin exceeds its CPU budget or memory limit, only that instance errors out; the main process and other instances continue running.
Benchmarks: Memory, Throughput, and Fault Isolation
James M Snell ran tests on an 88-core Linux machine:
Memory per instance: 1.4 MB for Green Threads vs. 6.2 MB for Workers — a 77% reduction (4.5× difference).
Warm pool launch: With setWarmPoolSize(100), pulling 100 Green Threads from the pool takes 1.6 ms.
Throughput: 1,000 tenants processing 50,000 concurrent requests: 1,000 Workers achieved ~160k QPS; Green Threads reached ~195k QPS using only 8 OS threads.
Fault isolation test: 100 tenants with one malicious tenant (microtask infinite loop or memory exhaustion). vm pool: shared heap caused the entire worker to crash; 13 innocent tenants on the same thread were terminated.
Green Threads: the offending instance was killed within 51 ms; the other 99 tenants continued responding normally.
Limitations and Known Issues
No Node.js APIs: require(), process, fs, net, and even globals like TextEncoder, URL, and structuredClone() are absent. All external capabilities must be provided by the host via env or module hooks.
No preemptive scheduling: A developer ( @filipkunc-dt) demonstrated a deadlock using SharedArrayBuffer spinlocks that occupied all 8 pool threads, starving other Green Threads. James M Snell fixed this by making Atomics.wait() throw inside Green Threads and adding setStallPolicy() to detect pool starvation and dynamically add threads or terminate the longest-running instance.
CPU-bound work: The default 8-thread pool limits parallelism; 100 concurrent Fibonacci calculations still run on only 8 cores.
Current Status
The code has not yet shipped in an official release and lacks DevTools debugger integration. To experiment, check out the PR #67288 branch, compile Node.js, and run with the --experimental-green-threads flag.
Reference
[1] PR #67288: https://github.com/nodejs/node/pull/67288
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
