We are building an infrastructure which features a Node.js server and Express.
In the server, what is happening is as follow:
- The server accepts an incoming HTTP request from client.
- Server generates two files (this operation can be "relatively long", meaning also 0.1 seconds or so)
- Server uploads the generated files (~20-200 KB each) to an external CDN
- Server responds to client, and this includes the URI of the file on the CDN
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.
What I was thinking about is to have a separate part in the Node server that contains a few "workers" (2-3, but we will do tests to determine the correct number of simultaneous operations). So, the new flow would look something like:
- After accepting the request from the client, the server adds an operation to a queue.
- 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).
- 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).
What do you think of this approach? Do you believe it is the correct one?
Mostly important, HOW could this be implemented in Node/Express?
Thank you for your time