Building a non-blocking multi-processes Web Server (Node JS fork example)
Hussein Nasser Hussein Nasser
396K subscribers
49,077 views
0

 Published On Sep 27, 2019

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)
https://github.com/hnasr/javascript_p...

isprime (non-blocking)
https://github.com/hnasr/javascript_p...



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



Support me on PayPal https://bit.ly/33ENps4
Become A Patron   / hnasr  

Stay Awesome!
Hussein

show more

Share/Embed