# Use all CPU cores with the cluster mode

> Start several worker processes behind one port with the cluster option of Total.run(), and know what is shared between them and what is not.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 99d3248ff978a66dc94b11b8e9618643225d23dc06e9d6f4278b01bbb9f41e09
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

A Node.js process uses one CPU core. `Total.run({ cluster: N })` starts a **master** that forks `N` worker processes, each running your whole application, and the operating system shares the incoming connections between them (`'auto'` instead of a number lets the master add threads as the load grows). Run it in release mode (`release: true`, which the generated start script sets with `--release`); the debug watcher is not meant for a cluster. In the start script it is one option:

```js
Total.run({ cluster: 4, release: true }); // or cluster: 'auto'
```

Every worker is a separate process with its own memory, so anything kept in a variable, in `CACHE` or in `MEMORIZE` exists once **per worker** and is not shared: two requests of the same user can land on different workers. Keep shared state in a database, in a file storage or in a message service, and keep sessions in a store all workers can read. Scheduled work such as `CRON()` also runs in every worker, so guard it if it must run once.

**Check that all the workers are up.** After a deploy, request a URL like `/pid/` below repeatedly and count the distinct ids: you should see as many as the workers you asked for.

Each worker has its own process id. The example starts two workers and includes that id in the response.

```js controllers/pid.js
exports.install = function() {
	ROUTE('GET /pid/', pid);
	ROUTE('GET /dir/', dir);
};

function pid($) {
	$.json({ pid: process.pid });
}

function dir($) {
	$.json({ dir: PATH.root() });
}
```

```js cluster.js
require('total5');
Total.run({ cluster: 2, port: 8766, release: true });
```
