Skip to content
Go to file


npm package Dependency Status devDependency Status Build Status

Dead-simple one-liner for clustered Node.js apps.

Forks N workers and creates new ones if they go down. Correctly handles signals from the OS.

const throng = require('throng')

throng(id => console.log(`Started worker ${id}`))
$ node examples/basic
Started worker 1
Started worker 2
Started worker 3
Started worker 4


$ npm install --save throng


Fork 1 worker per CPU:


Specify the number of workers:

throng({ worker: workerStartFunction, count: 3 })

More options:

  master: masterStartFunction,
  worker: workerStartFunction,
  count: 16,
  grace: 1000

Handling signals:

(for cleaning up before disconnecting a worker on a SIGTERM, for instance)

throng({ worker })

function worker(id, disconnect) {
  console.log(`Started worker ${id}`)

  process.on('SIGTERM', () => {
    console.log(`Worker ${id} exiting (cleanup here)`)

All options (with defaults)

  master: () => {},               // Fn to call in master process (can be async)
  worker: yourWorkerFunc,         // Fn to call in cluster workers (can be async)
  count: os.cpus().length,        // Number of workers
  lifetime: Infinity,             // Min time to keep cluster alive (ms)
  grace: 5000,                    // Grace period between signal and hard shutdown (ms)
  signals: ['SIGTERM', 'SIGINT']  // Signals that trigger a shutdown (proxied to workers)

A complex example

const throng = require('throng')

throng({ master, worker, count: 4 })

// This will only be called once
function master() {
  console.log('Started master')

  process.on('beforeExit', () => {
    console.log('Master cleanup.')

// This will be called four times
function worker(id, disconnect) {
  let exited = false

  console.log(`Started worker ${ id }`)
  process.on('SIGTERM', shutdown)
  process.on('SIGINT', shutdown)

  async function shutdown() {
    if (exited) return
    exited = true

    await new Promise(r => setTimeout(r, 300))  // simulate async cleanup work
    console.log(`Worker ${ id } cleanup done.`)
$ node examples/complex
Started master
Started worker 1
Started worker 3
Started worker 2
Started worker 4
Worker 1 cleanup done.
Worker 3 cleanup done.
Worker 2 cleanup done.
Worker 4 cleanup done.
Master cleanup.

Staying alive

Throng forks replacements for workers that crash so your cluster can continue working through failures.

$ node examples/crashy


$ docker-compose run --rm dev

node@docker:/home/app$ npm test


A simple worker-manager for clustered Node.js apps



No packages published
You can’t perform that action at this time.