Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Express, can it handle parallel requests with a single cpu core?

I am creating an API based on nodejs, express and mongodb inside a Docker.

The Docker is running at the moment in a DigitalOcean droplet with 512MB of RAM and 1 cpu core.

I have read that nodejs is single thread per cpu core, and using something called 'cluster' it is possible to make it fakemultithread starting an App on each core. I do have only 1 core, so.. does it mean that I am screwed?

If I would have an endpoint in my API which hypothetically takes 5 seconds to provide a response... Does it mean that during those 5 seconds I could not attend any other request?

like image 776
StuckOverFlow Avatar asked Sep 16 '25 12:09

StuckOverFlow


1 Answers

I do have only 1 core, so.. does it mean that I am screwed?

That only means that your instance can only do one thing at a time. It still can achieve multiple threads running at the same time due to context switching. Thats how modern computers are able to run a few thousand threads in parallel even if they only got a few cores. So no, you are not screwed.

Now the cool thing of javascript is that most API calls or IO do not block the javascript main thread. Nodejs will spawn another thread for you, that happens in the background, therefore you don't have to deal with threading and that stuff, just write your code and nodejs does it job.

If I would have an endpoint in my API which hypothetically takes 5 seconds to provide a response... Does it mean that during those 5 seconds I could not attend any other request?

That depends on the things you want to do. If you are doing heavy calculations (blocking) then yes, you should spawn another thread for that. If it is readig files, requesting databases etc. thats not a problem as it is non-blocking, and nodejs can answer other requests in the meantime.

like image 168
Jonas Wilms Avatar answered Sep 18 '25 08:09

Jonas Wilms