0

My backend has a few endpoints, most of them return some json to the customer and are pretty fast, however one of them takes a very long time to process. It takes an image url from the request body, manipulates that image to get a new one, and once the image is processed it uploads it to a server in order to get back a url, and only then it can use the url to make an order.

Getting the enhanced image and uploading it to the server (to get back the url) take a long time, like a good 3 seconds each if not more. I don't want the "order" endpoint to block the other endpoints, if that is something that would happen.

Each order is independent from the previous or the next one and I don't care how long it takes to process one, if it means it doesn't distrupt and block the event loop.

For now this is my code:

app.post("/order", async (req,res) => {
    AIEnhancedImage = await enhance(req.body.image)
    url = await uploadImageToServer(AIEnhancedImage)
    order(url)
}

app.get("/A"), async (req,res) => {
    ...
}

app.get("/B"), async (req,res) => {
    ...
}

app.get("/C"), async (req,res) => {
    ...
}

My question is, if another endpoint is hit, will that endpoint be blocked by the "order" one if there is one processing? If it does, what is a better implementation to make sure the order endpoint is processed bit by bit instead all at once?

This doubt probably arises from my lack of knowledge about the event loop. what I hope is that the code from the order endpoint will be added to the event loop but be processed indipendently and at the same time as other requests from other endpoints. The blocking part would only be within that endpoint, so it wouldn;t affect significantly the performance of other endpoints.

  • Does this answer your question? [How are concurrent requests handled by Nodejs express http server?](https://stackoverflow.com/questions/56941047/how-are-concurrent-requests-handled-by-nodejs-express-http-server) – GalAbra Mar 16 '22 at 09:39
  • @GalAbra I'm not sure... they are using a while loop over there tho block the event loop. I'm not using a while loop but I think it can be applied to my problem. Is there a way to say to Node: "you handle this, just do it bit by bit while still processing other requests". Do I have to write a promise myself with the blocking code inside it and then call that promise, and since it is a promise Node knows to process it separately without blocking everything else, even if within the promise there is blocking code? – Matteo Barberis Mar 16 '22 at 10:28

1 Answers1

0

The answer is it depends.

Is the code below CPU intensive or IO intensive?

AIEnhancedImage = await enhance(req.body.image)
url = await uploadImageToServer(AIEnhancedImage)
order(url)

Only one active user action can run inside an event loop callback. So if you are doing some cpu intensive task than nothing else can run on unless that task finishes.

Think it like this.. what ever custom code you write only one thing can run at a time.

But if you are doing IO based task then node JS will use special worker pool to process and wait for IO. So while Node JS waits for IO, node JS will pick something else in event loop and try to process it.

https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

indolentdeveloper
  • 1,253
  • 1
  • 11
  • 15
  • Thank you for answering. I'm not sure what you mean by user action, but when you say "if you are doing some cpu intensive task than nothing else can run on unless that task finishes" do you mean in general or that within the order endpoint code, only one thing can run at a time? I think they are IO intensive? Both the image enhance and the upload to server send an image over to a API to get back an image, however they have to send the whole image as a buffer and get a new buffer each time, would that be IO or CPU? – Matteo Barberis Mar 16 '22 at 10:19
  • in General. The event loop has list of things to do in a queue. The event loop processor will handle one event in queue at a time. The processor goes to next item in queue only in 2 conditions. 1. If the callback successfully completely. 2. if some IO needs to be done then new queue item is created to handle IO response. – indolentdeveloper Mar 16 '22 at 10:23
  • I would say not much. This is what you would have to experiment. Like how big is image. How much time to convert to buffer etc. For now it is mostly looking fine. But you would still need some sort of performance test to setup number of NodeJs instances you need to achieve desired performance. – indolentdeveloper Mar 16 '22 at 10:25