'Does an backend endpoint with long awaits within it block other endpoints?

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.



Solution 1:[1]

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/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 indolentdeveloper