DEV Community

Thomas Sarpong
Thomas Sarpong

Posted on

Scaling up express js performance

Nodejs runtime was built on top of the v8 chrome engine which runs on the event loop model. Nodejs has a single thread runtime which makes it relatively fast in executing and dealing with I/O operations.

Even thought the single thread nature has much advantage it has some other disadvantages.
Nodejs applications can be blocking when CPU intensive tasks are running.

Imagine an express application which has a route that checks whether a number is a prime number. When a user sends a request passing a larger number, the application will prevent a user who sends a smaller number from getting a faster response.

Nodejs exposes a lot of APIs that allows developer to manage process and make full use of the host machine's capabilities.

Imagine spinning up multiple instances of your node applications on a single host that is well load balanced.

There is an easy implementation of this using a package I found on npm. The code snipped below shows a simple way of using this package to run multiple instances of your node applications on your host machine.

import express from "express";
import AppRunner from "express-worker-manager";

const app = express();

app.get("/hello", (_, res) =>
  res.send(`Hello response from app running in process ${}`)

const runner = new AppRunner(app, { env: "production",port:4000});
runner.listen(() => {
  console.log("App running");
Enter fullscreen mode Exit fullscreen mode

The code below shows a simple usage of the express-worker-manager to spin up multiple instances of your express application based on the number of CPUs available on host machine. Each instance runs in a core of the CPU.

Link to package

Top comments (0)