Three areas to take care in scaling
- Multiple instances
- Microservices
- Database Partitioning
How to fork?
const { fork } = require('child-process);
const processes = [
fork('./app', ['3001']),
fork('./app', ['3002']),
fork('./app', ['3003'])
]
Check CPUs on machine
const cpus = require('os').cpus;
console.log(cpus);
Run as many instances of node app as many CPUs are available. Write the following to index.js and run in Node
const http = require('http');const cluster = require('cluster');const cpus_len = require('os').cpus().length;if (cluster.isMaster) {console.log("FORKING", process.pid);for (let i = 0; i < cpus_len; i++) {cluster.fork();}} else {http.createServer((req, res) => {res.end(process.pid.toString());}).listen(3000);}
LoadTest
LoadTest can be used to test multiple processes running.
Installing LoadTest
npm install loadtest
loadtest -n 500 http://localhost:3000
loadtest -n 500 http://localhost:3000
Comments
Post a Comment