# @getlarge/nestjs-tools-cluster

> Cluster and workers manager

Latest version **0.15.8** (published 2026-11-27) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @getlarge/nestjs-tools-cluster
pnpm add @getlarge/nestjs-tools-cluster
yarn add @getlarge/nestjs-tools-cluster
bun add @getlarge/nestjs-tools-cluster
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.15.8 |
| Published | 2026-11-27 |
| First published | 2024-04-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 19.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 72 |
| Author | Edouard Maleix |
| Maintainers | getlarge |
| Keywords | cluster, worker, node, scale, nestjs |

## Links

- npm: https://www.npmjs.com/package/@getlarge/nestjs-tools-cluster
- Repository: https://github.com/getlarge/nestjs-tools
- Homepage: https://github.com/getlarge/nestjs-tools/tree/main/packages/cluster
- Issues: https://github.com/getlarge/nestjs-tools/issues
- npm.io page: https://npm.io/package/@getlarge/nestjs-tools-cluster

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.0
- [@getlarge/typed-event-emitter](https://npm.io/package/@getlarge/typed-event-emitter.md) 0.2.2

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.15.8 (latest) — 2026-11-27
- 0.15.7 — 2025-01-31
- 0.15.6 — 2024-04-16

## README

# Cluster

[![npm][npm-image]][npm-url]

[npm-image]: https://img.shields.io/npm/v/@getlarge/nestjs-tools-cluster.svg?style=flat
[npm-url]: https://npmjs.org/package/@getlarge/nestjs-tools-cluster

This package provides a simple way to clusterize a NestJS application.
The `ClusterService` class is a wrapper around the native `cluster` module and provides a simple API to manage the workers' lifecycle.

## Installation

```bash
npm install --save @getlarge/nestjs-tools-cluster
```

## Example

```ts
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ClusterService, ClusterServiceConfig } from '@getlarge/cluster-service';
import { cpus } from 'node:os';

import { AppModule } from './app/app.module';

const GLOBAL_API_PREFIX = 'api';
const DEFAULT_PORT = 3000;
const CLUSTER_MODE = process.env.CLUSTER_MODE === 'true';
const MAX_WORKERS = +process.env.MAX_WORKERS || cpus().length;

async function bootstrap(
  opts: { workerId?: number } = {},
  disconnect: () => void = () => process.exit(1),
): Promise<void> {
  /**
   * This is a global variable that will be used to identify the worker id
   * in the application. This is useful for debugging purposes.
   */
  globalThis.__WORKER_ID__ = opts.workerId;

  try {
    const app = await NestFactory.create<NestFastifyApplication>(
      AppModule,
      new FastifyAdapter({
        trustProxy: true,
        bodyLimit: +process.env.MAX_PAYLOAD_SIZE || 1048576,
      }),
      { bufferLogs: true, abortOnError: false },
    );
    app.setGlobalPrefix(GLOBAL_API_PREFIX);
    const configService = app.get(ConfigService);
    const port = configService.get('PORT', { infer: true }) ?? DEFAULT_PORT;

    await app.listen(port, '0.0.0.0', () => {
      Logger.log(`Listening at http://localhost:${port}/${GLOBAL_API_PREFIX}`);
    });
  } catch (error) {
    Logger.error(error);
    disconnect();
  }
}

if (CLUSTER_MODE) {
  const clusterConfig: ClusterServiceConfig = {
    workers: MAX_WORKERS,
    delay: 2000,
    grace: 1000,
  };

  const clusterService = new ClusterService(clusterConfig);
  clusterService.clusterize(bootstrap).catch((e) => {
    clusterService.logger.error(e);
    process.exit(1);
  });
} else {
  void bootstrap({}, () => {
    process.exit(1);
  });
}
```

## Building

Run `nx build nestjs-tools-cluster` to build the library.

## Running unit tests

Run `nx test nestjs-tools-cluster` to execute the unit tests via [Jest](https://jestjs.io).

---
_Source: https://npm.io/package/@getlarge/nestjs-tools-cluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
