A
A
Al2019-01-28 14:47:25
Node.js
Al, 2019-01-28 14:47:25

Why does nodejs in cluster mode show worse performance results than in a single instance?

So, about everything in more detail.
It all starts with NGINX, it directs requests to the backend (express.js), in one case to one port in the other to a cluster (multiple ports, but all on the same physical machine).
If express is running in standard mode (custom config), then everything happens as usual, the server starts on one port. If we run in a cluster, then we create a master process (cluster.isMaster) and create child ones starting the server in several instances (equal to the number of CPU cores) - all this is also a standard approach.
But, when we tested all this using jmeter (100 simultaneous requests), we found that in cluster mode, performance is two to three times worse than in normal mode. Although, it is obvious that in the cluster performance should be higher, because. in standard mode, one process and one core are used, and in cluster mode - 8.
I have another option to try PM2, where, as I understand it, the server starts differently, but it’s still interesting to understand why it happens in my situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2019-01-28
@Sanu0074

https://nodejs.org/dist/latest-v10.x/docs/api/clus...
The cluster itself, on all operating systems except Windows, accepts connections in the master process and passes the already established tcp connection to the client to the workers , this allows load balancing by the round-robin algorithm (each worker is given an approximately equal number of connections, since they are distributed strictly in turn), but this also imposes overhead on transferring the connection file descriptor between the master and worker processes on each connection.
There are 2 optimization options here:
1. Nginx should open connections to the backend in keep-alive mode. This minimizes the costs not only for transferring the connection to the worker, but also for opening it in general, which will give a noticeable increase in performance under load.
2. Instead of the cluster module, use the usual child_process and manage the transfer of connections manually, or rather, transfer not already open connections, but the listening socket. In this case, balancing will occur at the OS level. As they say in the cluster documentation, this can lead to uneven loading of workers, since the connection will go to the worker who has time to accept first. But it will remove the overhead for transferring each connection between processes. But even with this option, keep-alive is highly desirable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question