title: Node.js/Express and parallel queues
body: <p>We are building an infrastructure which features a Node.js server and Express.</p>
  
  <p>In the server, what is happening is as follow:</p>
  
  <ol>
  <li>The server accepts an incoming HTTP request from client.</li>
  <li>Server generates two files (this operation can be "relatively long", meaning also 0.1 seconds or so)</li>
  <li>Server uploads the generated files (~20-200 KB each) to an external CDN</li>
  <li>Server responds to client, and this includes the URI of the file on the CDN</li>
  </ol>
  
  <p>Currently the server is doing this sequentially for each request, and this works quite well (Node/Express can handle concurrent requests automatically). However, as we plan to grow, the number of concurrent requests may grow higher, and we believe it would be better for us to implement a queue for processing requests. Otherwise, we may risk having too many tasks running at the same time and too many open connections to the CDN. Responding to the client quickly is not a relevant thing.</p>
  
  <p>What I was thinking about is to have a separate part in the Node server that contains <em>a few</em> "workers" (2-3, but we will do tests to determine the correct number of simultaneous operations).
  So, the new flow would look something like:</p>
  
  <ol>
  <li>After accepting the request from the client, the server adds an operation to a queue.</li>
  <li>There are 2-3 (to be tested) workers that take elements out of the queue and perform all the operations (generate the files and upload them to the CDN).</li>
  <li>When the worker has processed the operation (doesn't matter if it stays in the queue for a relatively long time), it notifies the Node server (a callback), and the server responds to the client (which has been waiting in the meanwhile).</li>
  </ol>
  
  <p>What do you think of this approach? Do you believe it is the correct one?</p>
  
  <p>Mostly important, HOW could this be implemented in Node/Express?</p>
  
  <p>Thank you for your time</p>