Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple hello world in Node, using Fastify and clustering for Medium post
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const fastify = require('fastify')();
const port = 8123;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', worker => {
console.log(`Worker ${worker.process.pid} died`);
});
} else {
fastify.get('*', (req, res) => {
res.send('Hello World');
});
fastify.listen(port, () => {
console.log(`Fastify "Hello World" listening on port ${port}, PID: ${process.pid}`);
});
}
@avidcoder123

This comment has been minimized.

Copy link

@avidcoder123 avidcoder123 commented Dec 25, 2020

Interesting code, is there a benchmark for this server in comparison to vanilla fastify/express?

@johannesfritsch

This comment has been minimized.

Copy link

@johannesfritsch johannesfritsch commented Feb 22, 2021

Not exactly a benchmark, but an idea from a project I'm starting:

Single threaded (vanilla fastily): 13K requests/second
In cluster mode with 8 threads: 53K requests/second

The server is a graphql instance (based on mercurius/fastify) with no I/O like database access or any significant computations. So I'm pretty sure that these numbers converge a lot as soon as the server becomes more I/O bound.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment