1

My node.js app currently subscribes to a number of websocket servers that is starting to push a lot of data over to the app every second. This websocket client app has several event handlers that does some work upon receiving the websocket data.

However, Node.js appears to be only using 1 CPU core at any one time, leave the remaining cores under utilized. This is expected as Node.js uses a single-threaded event loop model.

Is it possible to load balance the incoming websocket data handling over multiple CPU cores? I understand that Node Cluster and pm2 Cluster Mode are able to load balance if you are running websocket servers, but how about websocket clients?

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • @Flimzy Is there an approach to launch multiple worker instances from the main Node.js process, then somehow let the main Node.js process load balance the processing of incoming websocket data across multiple CPU cores? – Nyxynyx Feb 09 '20 at 17:40
  • You can fork as many processes as you want (and that your hardware can support). My personal opinion, which may be controversial, is that Node isn't the right tool for highly concurrent workloads. – Jonathan Hall Feb 09 '20 at 17:41
  • @Flimzy I agree with you that Node.js is not be the first choice for this situation. In the meantime, it might be quicker to load balance the existing Node app than rewriting the entire app something like Golang or Elixir/Phoenix. – Nyxynyx Feb 09 '20 at 17:47
  • yep, the quick fix will most certainly be starting multiple instances. – Jonathan Hall Feb 09 '20 at 17:54

1 Answers1

1

From the client side, I can think of the following options:

  1. Create some child processes (likely one for each CPU core you have) and then divide your webSocket connections among those child processes.

  2. Create node.js WorkerThreads (likely one for each CPU core you have) and then divide your webSocket connections among those WorkerThreads.

  3. Create node.js WorkerThreads (likely one for each CPU core you have) and create a work queue where each incoming piece of data from the various webSocket connections is put into the work queue. Then, the WorkerThreads are regular dispatched data from the queue to work on. As they finish a piece of data, they are given the next piece of data from the queue and so on...

How to best solve this issue really depends upon where the CPU time is mostly being spent. If it is the processing of the incoming data that is taking the most time, then any of these solutions will help apply multiple CPUs to that task. If it is the actual receiving of the incoming data, then you may need to move the incoming webSockets themselves to a new thread/process as in the first two options.

If it's a system bandwidth issue due to the volume of data, then you may need to increase the bandwidth of your network connection of may need multiple network adapters involved.

jfriend00
  • 683,504
  • 96
  • 985
  • 979