vjjj vjjj - 2 months ago 40
Node.js Question

Nodejs, dedicated server, cpu intensive task

I am doing a cpu intensive task on a dedicated server running on nodejs using the following procedure. Wondering if this is the correct approach (steps and approach as a whole) -

(Note - Time of completion of cpu intensive task is not important as long as it just gets done)

  1. Create a nodejs web server (WS) and another nodejs server for cpu intensive tasks (CPS) on different ports on the same machine

  2. WS sends cpu intensive task to CPS via a localhost http request

  3. CPS gets blocked, performs each task sequentially. WS is not directly affected by CPS being blocked (I am assuming this!)

Question1. What will happen if CPS has a huge queue which keeps on growing, due to requests sent by WS?

Question2. Is this a bad way of doing CPU intensive work? What will be a better solution?


Answer1. If CPS keeps on enqueueing requests, at some point (at a very very large request queue), heap size of application will exceed the OS limit -> CPS will be shut down losing all the enqueued requests. Think of this scenario like a DDOS attack on CPS!

Answer2. If queue is expected to become large, it is recommended that a task queue (like kue) / message queue like Redis Simple Messaging Queue (rsmq) be used.

I went with simple HTTP at this point of time as my load is not that huge. I may update this answer later (when my server load increases).

(Some sources come from this question on Stackoverflow)