How does a Node.js process behave in AWS Fargate?
Asked Answered
E

2

7

I have a Node app deployed on AWS Fargate on the 1 vCPU/2GB memory task config. I have been wondering about the behavior that Node.js has when running on this setup in relationship to the vCPUs that are available.

According to AWS docs, the vCPU is just a hyperthread on an intel Xeon CPU core: What vCPUs in Fargate really mean?. So, how does libuv run the threadpool (which by default runs 4 threads) if the whole program is just running on one vCPU which is already itself a hyperthread? Also, how does the Node program behave on a config running on a lower configuration with a 0.256 vCPU?

Furthermore, Fargate automatically handles scaling by running the number of tasks specified in the configuration; but, what would happen if I decided to use something like PM2 to run multiple Node processes inside each task? Would this not really work since I'm running on 1 vCPU?

Enter answered 27/7, 2020 at 1:58 Comment(0)
K
1

Same as threads has always run: on the single CPU.

Threads (software) was invented before multi-core machines were popular. All the original multithreaded Unix machines like the Sun, HP9000, Digital VAX and Alphas and Silicon Graphics IRIS were all single cpu machines that had only a single core with no hyperthreading.

How threads (software) work was the same back then as it is today: the thread takes turn to execute on the CPU. This is scheduled by the OS using a hardware feature of the CPU: timer interrupts.

The OS will set a timer to trigger an interrupt and then pass the CPU to the thread. When the timer expires it will interrupt the CPU causing the CPU to stop executing the code from the thread and execute code from the OS instead. It is during this interrupt that the OS decide which thread to execute next and set the timer interrupt again. The only difference with multicore CPUs is that the OS have more CPU cores to schedule threads on. And the only difference with hyperthreading is that the OS can schedule two threads per CPU core.

This is why you normally have around 300 threads running on a typical home user desktop Linux machine (not even a server) even though it physically has only 4 cores capable of running only 8 threads at the same time.

Kith answered 14/6, 2021 at 20:6 Comment(1)
You may notice that my description of how the OS runs threads on a single core sounds suspiciously similar to how javascript event loop works. Actually they're very closely related and in fact on a single core machine there is no difference in theory between a multithreaded software and an event-driven software. In practice the event-driven software is usually faster unless you use a fixed (non-growing) thread pool on a single core. With multiple cores a multithreaded software can run more things in parallel.Kith
M
-1

im wondering the same, gonna send logs with system resources to see what happens because in theory, nodejs only use one core if you are not using thread workers or child process but fargate seems a little bit different

Mohammedanism answered 6/10, 2022 at 22:50 Comment(1)
This isn’t an answerSilvio

© 2022 - 2024 — McMap. All rights reserved.