1

I am running an express.js server. When I send some load the cpu usage spikes to over 140%.

I understand that since the system I am running the server on has 4 cores so it can go upto 400% as well.

My question is:

  1. How can node.js application consume more than 100% despite being single threaded?
  2. To improve the performance should I run the server in cluster mode? Currently a lot of requests are in http_request_waiting state.
avck
  • 3,535
  • 3
  • 26
  • 38
  • I love to use [pm2](https://pm2.keymetrics.io/docs/usage/cluster-mode/) to run my express APIs in cluster mode. Maybe it also fits your needs. – BraveButter Sep 28 '20 at 09:45
  • See https://stackoverflow.com/q/2387724/2304737 – mr5 Sep 28 '20 at 09:51
  • @mr5 I checked that before posting. But that does not really answer how I have >100% cpu usage. Is it because node is spawning threads internally? – avck Sep 28 '20 at 09:55
  • yeah. the javascript runs on a single thread, but node uses other threads for io, network, native modules. – Matt Sep 28 '20 at 09:58
  • There may be more than 1 thread running. You can run `ps -Tp ` to show the threads of a process. Also top has a field `nTH` that will show the number of threads. – steepestascent Dec 13 '21 at 11:53

1 Answers1

2

Although the node is a single-threaded model that efficiently works on a single thread to serve the requests. But, it's underlying IO model is multi-threaded. There are two libuv components that act during the process one is event-pool and the other one is thread-pool and this thread pool is allocated with blocking operations like file reading, database queries, and IO operations.

kavigun
  • 2,219
  • 2
  • 14
  • 33