In this video, learn how Node.js works by seeing Node described as two different types of restaurants. Also, learn how Apache works; how Node is different due to it being single threaded; about non-blocking, event-driven IO; and asynchronous activity in Node.js.
- I'm going to demonstrate how Node JS works and why it is so fast by taking a look at two restaurants. The first restaurant is Apache Steaks and Chops. It is a big, nice, fancy restaurant. In this restaurant, every new guest represents a new user. And making an order is like making a request. If I place an order for a salad, a manager will need to hire a new waiter to take care of me. In this restaurant, our waiter represents a thread. We are going to have our own waiter, our own thread. And they will handle all of our orders. This is similar to how Apache works. Every request is single-threaded. After placing the order, the waiter will take the order to the kitchen and give it to the chef. And now the waiter just waits. He won't do anything else until the chef is finished making the food. I would like to order a glass of water, but I can't order anything until the chef finishes making that salad. The chef is blocking me from being able to simply order a glass of water. In this analogy, the chef represents the file system, or a data store. In Apache, the single thread waits for the file system to finish reading files before it can do anything else. We refer to this as blocking. Finally, the salad is ready. My waiter brings me the food, I can order my glass of water, and my waiter also brings me that too. My request has been served, and now the manager is firing my waiter because they are not needed anymore. Now, when this restaurant gets busy for dinner service, every guest has their own waiter, which is pretty nice. That is pretty good service. But the waiters are mostly hanging around the kitchen and waiting for the chef to make the food. If this restaurant gets really popular it requires a lot of space to expand because more guests means more waiters. Now, let's take a look at this other cafe, Chez Node. At this cafe, there is only one waiter because Node JS is single-threaded. Here we can order some crepes. We can see that our waiter places the order for the food, then moves on to take an order from another new table. Hmm, this single thread services all of the restaurants guests. That is pretty cool. When my crepes are ready, the chef rings a bell, and our waiter goes and gets the crepes and delivers them to me. He then proceeds to take another order from a new table. When their food is ready, the waiter will bring it to them as soon as he can. We can say that this waiter behaves asynchronously. Everything this waiter needs to do represents a new event. A new table, placing orders, delivering orders. These are all events. And they will get handled in the order that they are raised. Our waiter does not wait. There is no blocking. Our single waiter is busy, busy, busy, but he is killing it because he can multitask. This is what it means when we say non-blocking event driven IO. We have a single thread that will respond to events in the order that they are raised. This thread behaves asynchronously because it does not have to wait for resources to finish doing what they're doing before our thread can do anything else. If this cafe gets popular, we can simply franchise it. Chez Node can easily be expanded by simply duplicating or forking the restaurant into a neighboring space. And this is precisely how we host Node JS applications in the cloud. Now remember, Node JS is single-threaded. All of the users are sharing the same thread. Events are raised and recorded in an event queue. And then handled in the order that they were raised. Node JS is asynchronous, which means that it can do more than one thing at a time. This ability to multitask is what makes Node JS so fast. And one of the reasons so many developers are building their web applications with Node JS.
- What is Node.js?
- Installing Node.js
- The global object
- Importing the core modules
- Creating custom events with the EventEmitter
- Reading, writing, and removing files
- Working with file streams
- Creating child processes with the exec and spawn functions