Skip to main content

Why NodeJS

In this post we're going to cover the why should we use NodeJS.

Why is it so good at creating backend apps and why is it becoming so popular with companies like Netflix, Ubre and Walmart all using node in production as you might have noticed since you're taking this course when people want to learn a new backend language more and more they're turning to node as that language. They want to learn the nodes skill set is in hot demand for both frontend developers who need to use node to day to day to do things like compile their applications to engineers who are creating applications and utilities using node. Yes all of this has made Node the prime choice as the backend language of choice.

We're learning exactly why node is so great.

"NodeJS uses an event driven non-blocking IO model that makes it lightweight and efficient."
We're going to explore all of that in just a second. The sentence which we'll explore at the end. "Node's package ecosystem NPM is the largest ecosystem of open source libraries in the world."
Now these two sentences they have a ton of information packed into them.
We're going to dive into some charts and graphs and we'll explore what makes no different and what makes it so great note is an event driven non-blocking language. Now you see non-blocking IO right here.

What is I/O? IO is something that your application does all of the time when you're reading or writing to a database that is I/O. Which is short for input output. This is the communication from your node application to other things inside of the Internet of Things. This could be a database read and write request. You may be changing some files on your file system or maybe making an HTP request to a separate web server such as a Google API for fetching a map for the users current location. All of those use I O when IO takes time. Now the non-blocking IO is great. That means while one user is requesting a URL from google other users can be requesting database file read and write access. They can be requesting all sorts of things without preventing anyone else from getting some work done. Let's go ahead and take a look at the difference between blocking and nonblocking software development right here.

I have two files that we're going to be executing in just a minute. But for the moment we're going to explore how each operates the steps that are required in order for the program to finish. This is going to show us the big differences between blocking which I have on the left which is not what node uses and non blocking on the right which is exactly how all of our node applications in the course are going to operate.

You don't have to understand the individual details like what require is in order to understand what's going on here. We're going to be breaking things down in a very general sense. The first line on each is responsible for fetching a function that gets called and this function this is going to be our simulated I/O function that is going to a database fetching some user data and printing it to the screen. Both files do the same thing they just do it in different ways. After we load in the function of both files try to fetch a user with an ID of one to three when it gets that user it prints it to the screen with the user one string before and then it goes on and it fetches the user with it. Three two one is the I.D. It prints that to the screen. And finally both files add up one plus. Storing the result which is three in the some variable and printing it to the screen. Now will they all do the same thing they do in very different ways. Let's break down the individual steps down below. We're going to go over what node executes and how long it takes you can consider these seconds it doesn't really matter it's just to show the relative operating speed between the two.

The first thing that happens inside of our blocking example right here is we fetch the user on line 3. Now this request requires us to go to a database which is an I/O operation to fetch that user by ID. This takes a little bit of time. In our case and we're going to say it takes three seconds.
 Next online for we print the user to the screen which is not an operation and it runs right away printing user 1 to the screen. You can see that takes almost no time at all.

Next up we go ahead and we wait on the fetching of a user to when user 2 comes back. As you might expect we print it to the screen which is exactly what happens on line 7 right here.
So right here we have are some printing to the screen in barely any time. This is how blocking works. It's called the blocking because well we're fetching from the database which is an IO operation. Our application cannot do anything else. This means our machine sits around idle waiting for the database to respond and can't even do something simple like adding two numbers and printing them to the screen. It's just not possible in a blocking system on the right.

We have our non blocking example this is how we're going to be building our node applications. Let's go ahead and break this down line by line first up things start much the same way. We're starting the get user function for user 1 which is exactly what we do here but we're not waiting. We're simply kicking off that event. This is all part of the event loop inside of know JS which is something we'll be exploring in detail. Notice it takes a little bit of time. We're just starting the request we're not waiting for that data. The next thing we do might surprise you. We're not printing user 1 to the screen because we're still waiting for that request to come back. Instead we start the process of fetching our user with the idea of three to one. Here we're kicking off another event which takes just a little bit of time to do. It is not an I/O operation now behind the scenes the fetching of the database is I/O but starting the event calling this function is not. So it happens really quickly. Next up we print the sum the sum doesn't care about either of the two user objects they're basically unrelated. So there's no need to wait for the users to come back before I print that some variable down below after we print the sum. What happens when we have this dotted box. This is the simulated time it takes for our event to get responded to.




Now this box it is the exact same with as the box over here using nonblocking doesn't make our IO operations any faster but what it does do is it lets us run more than one operation at the same time. Here we start two IO operations before the half second to Mark and right here between three and three and a half seconds both come back. Now the result here is that the entire application finishes much quicker. The nonblocking version finished in just over three seconds while the blocking version takes just over six. A difference of 50 percent and that 50 percent comes from the fact that here we have two requests each taking three seconds and here we have two requests each taking three seconds. But they run at the same time using the nonblocking model. We can still do stuff like printing the sum without having to wait for our database to respond. Now this is the big difference between the two. Blocking everything happens in order and a nonblocking we start events attaching callbacks and these callbacks get fired later.

Comments

Popular posts from this blog

What is NodeJS?

So let's dive into the most important question, what is nodejs? Nodejs is a javascript runtime and now what does this mean? You know javascript, it's a programming language you typically use in the browser to manipulate your dom, to manipulate the page which was loaded in the browser, for example to open a popup, a modal or add any kinds of effects because Javascript is a language that runs in the browser that allows you to interact with the page after it was loaded and it therefore is a crucial part when it comes to building interactive user interfaces in the browser, so whatever your users see. However javascript is not limited to that. Nodejs is a different version of javascript you could say, it is basically built on javascript, it adds some features to it, is not capable of doing some other things you can do with javascript in the browser, so it basically takes javascript and puts it into a different environment. It allows you to run javascript code on the server you coul...