Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js CPU load balancing

I created test with JMeter to test performance of Ghost blogging platform. Ghost written in Node.js and was installed in cloud server with 1Gb RAM, 1 CPU.

I noticed after 400 concurrent users JMeter getting errors. Till 400 concurrent users load is normal. I decide increase CPU and added 1 CPU.

But errors reproduced and added 2 CPUs, totally 4 CPUs. The problem is occuring after 400 concurrent users.

I don't understand why 1 CPU can handle 400 users and the same results with 4 CPUs.

During monitoring I noticed that only one CPU is busy and 3 other CPUs idle. When I check JMeter summary in console there were errors, about 5% of request. See screenshot.

CPU Utilisation

I would like to know is it possible to balance load between CPUs?

like image 271
Bobur Meliev Avatar asked Feb 18 '14 05:02

Bobur Meliev


People also ask

Is Nodejs good for CPU intensive applications?

Nodejs is good for IO intensive tasks but bad for CPU intensive tasks. The reason Nodejs is bad for CPU intensive task is that it runs on the event loop, which runs on a single thread. The event loop is responsible for everything that runs on the user-land of Nodejs. This event loop runs on a single thread.

What is load balancing in node JS?

A load balancer is a process that takes in HTTP requests and forwards these HTTP requests to one of a collection of servers.

What is CPU load balancing?

Load balancing is an important technique for improving distributed system performance by considering the group of hosts in the system to share their workloads. It results in a better utilization of host resources, a high system throughput and quick response time of user requests.

Is node js the best platform for CPU heavy applications Why?

Avoid Using Node.js: Processing CPU heavy and complex applications: Node. js uses an event-based, non-blocking I/O architecture and only has one CPU – all of that intensive CPU processing would block incoming requests. As a result of the high-end number crunching, the thread might get stuck.


2 Answers

Are you using cluster module to load-balance and Node 0.10.x?

If that's so, please update your node.js to 0.11.x.

Node 0.10.x was using balancing algorithm provided by an operating system. In 0.11.x the algorithm was changed, so it will be more evenly distributed from now on.

like image 169
alex Avatar answered Sep 17 '22 23:09

alex


Node.js is famously single-threaded (see this answer): a single node process will only use one core (see this answer for a more in-depth look), which is why you see that your program fully uses one core, and that all other cores are idle.

The usual solution is to use the cluster core module of Node, which helps you launch a cluster of Node processes to handle the load, by allowing you to create child processes that all share the same server ports.

However, you can't really use this without fixing Ghost's code. An option is to use pm2, which can wrap a node program, by using the cluster module for you. For instance, with four cores:

$ pm2 start app.js -i 4

In theory this should work, except if Ghost relies on some global variables (that can't be shared by every process).

like image 29
Paul Mougel Avatar answered Sep 17 '22 23:09

Paul Mougel