Use all CPU cores with the cluster mode
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:
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.
exports.install = function() {
ROUTE('GET /pid/', pid);
ROUTE('GET /dir/', dir);
};
function pid($) {
$.json({ pid: process.pid });
}
function dir($) {
$.json({ dir: PATH.root() });
}require('total5');
Total.run({ cluster: 2, port: 8766, release: true });