Easy way to scale your Node.js Application

#Cluster Mode:

pm2 start server.js
npm install pm2 -g
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
pm2 start server.js -i 2
module.exports = {
apps : [{
name : "app1",
script : "./app.js",
max_memory_restart: '700M',
env_production: {
NODE_ENV: "production"
},
env_development: {
NODE_ENV: "development"
}
}]
}

#Network Load Balancing:

  1. Check cron jobs to ensure that you will not run multiply instances of your jobs. Maybe you need move them to single instance.
  2. Check in-memory Cache, maybe you need store cache on Redis or Memcached.
  3. Check how you run database migrations, maybe you need to run them via Jenkins etc.
PORT=3000 pm2 start server.js -f --update-env
PORT=3001 pm2 start server.js -f --update-env
PORT=3002 pm2 start server.js -f --update-env
PORT=3003 pm2 start server.js -f --update-env
curl http://localhost:8080
/opt/homebrew/etc/nginx/nginx.conf
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;

upstream my_http_servers {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://my_http_servers;
}
}
}
sudo nginx

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store