Building a non-blocking multi-processes Web Server (Node JS fork example)

preview_player
Показать описание
If you build a backend such as a web server you need to account for requests that will yield expensive processing. Expensive requests will block other requests from being served if you only have a single process or thread for execution. This is when you might want to spin another process to execute your requests leaving the main process free to receive requests. In this video we will build a multi-process non-blocking web server using node js by forking incoming requests into their own processes for execution while keeping the main process free. We also explain the pros and cons of doing so, since it might not be always a good idea to create multiple processes as it increases the complexity of your application.

Building ‘isPrime’ web service

Some testing prime numbers
2367949 (16 ms)
43686389 (200 ms)
93686687 (500 ms)
936868033(4 seconds)
29355126551 (doesn’t finish)


Source Code

isprime (blocking)

isprime (non-blocking)

Jump code

Blocking Single Process 2:40

Non-Blocking Single Process 6:10

Pros & Cons 17:00

Pros
* Non-blocking
* utilizes the multi-core CPUs
* De-coupling
Cons
* Complex
* You Won’t be able to debug
* little challenging to know how long the client has been waiting since we are not measuring that on the server
* Health Checking Messaging between processes, orphan processes, crashed processes

Cards
4:15 Query parameters

Stay Awesome!
Hussein
Рекомендации по теме
Комментарии
Автор

I was literally already using child_process for ffmpeg and didn't even know all this functionality was included! Thank you so much for sharing your infinite wisdom

dabbopabblo
Автор

You will be one of the biggest tech YouTuber soon

raftar
Автор

Can you make a video discussing the implication of child processes, cluster and worker threads in node JS ?

RmAndrei
Автор

we are using pm2 with docker instances on Kubernetes (or just either use one of them avoid too much this is one of the best ways to optimize nodeJS performance on an enterprise scale. regarding child process, this would be useful if running something like an internal job processing through the CLI

tobiowolawi
Автор

i undestood this topic with you faster and easier than with my teacher, thank you so much!!!

valentino
Автор

Very nice tutorial sir.
Keep uploading videos like this which helps building scalable application.
Thanks.

codewithsub
Автор

Thanks for the information, buddy. But going offtopic, I think you look alike Wolverine (from Wolverine movie) which is played by Hugh Jackman. Once Again Thank you for sharing your experience with us which is definitely making me a better backend developer.

pratikpawar
Автор

Thank you so much, I really understand the concept. And most important thanks for sharing code

mehtabahmed
Автор

Exactly what I was looking for, thank you for this

sr
Автор

Interesting! Quite useful if you don't know containers.

jeno
Автор

Really this guy has potential to kick off node builder

akashsaha
Автор

the for loop is a long running process, its not a high traffic IO. worker threads should be used instead of child processes. this is clearly mentioned in the docs and a fundamental difference in use case
child processes are for high traffic io
worker threads are for long running processes and worker threads are counter productive for io

karma_yogi_
Автор

Once more another very helpful video, one question is fork the only option we have for this kind of scenarios?

elmeroranchero
Автор

That was really a great video, with good use case.
Thanks. 🙏

ayikkathilkarthik
Автор

It would be helpful to include diagrams to explain concepts such as horizontal scaling and load balancer etc.

zulfiqarjunejo
Автор

Thanks for the tutorial! I've just begun learning this stuff. The level of difficulty was just right.

ReporterTorizo
Автор

Another High energetic narration, thanks man

HosseinOjvar
Автор

This really gave me great insight, thank you

rahultej
Автор

if you say there is huge complexity behind this, why did you choose this approach? There are already tools like pm2 to handle the processes

awekeningbro
Автор

Really rich content. Well explained. Thanks a lot bruh!!

rishavkumar