‫So, remember that in the last lecture,
‫we talked about the Node architecture,
‫where two fundamental parts
‫are the thread pool and the event loop.
‫So in this lecture, you're gonna learn
‫all about threads and a thread pool.
‫And in the next one,
‫we will then dive deep into the event loop.
‫So, let's get started.
‫So, first off, when we use Node on a computer,
‫it means that there is a Node process
‫running on that computer.
‫And the process is just a program in execution.
‫And we already learned that Node.js
‫is basically a C++ program,
‫which will therefore start a process when it's running.
‫This is important because in Node,
‫we actually have access to a process variable,
‫which we're gonna use later in this course.
‫Now in that process, Node.js runs
‫in a so called, single thread.
‫And a thread is basically just a sequence of instructions.
‫But it's not important to deeply understand
‫what a thread or a process is.
‫That is more about computer science.
‫Just imagine a thread as being a box
‫where our code is executed in a computer's processor.
‫Now, what is important to understand here,
‫is the fact that Node runs in just one thread,
‫which makes it easy to block Node applications.
‫And this is something that we talked about before, actually.
‫But it's something really, really important to remember
‫because this is one of the unique features
‫that Node.js brings to the table.
‫So, again, if you run your Node application,
‫it'll run in just a single thread.
‫No matter if you have 10 users or 10 million users
‫accessing your application at the same.
‫And so you need to be very careful
‫about not blocking that thread.
‫And we will of course, take care of that
‫throughout the project in this course.
‫Moving on, let's now quickly understand
‫exactly what happens in a single thread
‫when you start your Node application.
‫So when the program is initialized,
‫all the top level code is executed,
‫which means all the code that is
‫not inside any callback function.
‫Also, all the modules that your app needs are required
‫and all the callbacks are registered,
‫just like the ones that we used
‫for our HTP server in the Node Farm App.
‫Remember that?
‫Then after all that, the event loop finally starts running.
‫And again, more about the event in loop in the next video.
‫What you need to know for now is that the event loop
‫is where most of the work is done in your app.
‫So, it's really the heart of the entire Node architecture.
‫But here is the catch, some tasks are actually too heavy.
‫They are too expensive to be executed in the event loop
‫because they would then block the single thread.
‫And so, that's where the thread pool comes in,
‫which just like the event loop,
‫is provided to Node.js by the libuv library
‫that we talked about before.
‫So, the thread pool gives us four additional threads
‫that are completely separate from the main single thread.
‫And we can actually configure it up to 128 threads.
‫But usually, these four are enough.
‫So these threads together formed a thread pool.
‫And the event loop can then automatically
‫offload heavy tasks to the thread pool.
‫And all this happens automatically behind the scenes.
‫It's not us developers who decide
‫what goes to thread pool and what doesn't.
‫Now, the expensive tasks that do get offloaded
‫are all operations dealing with files,
‫everything related to cryptography, like caching passwords,
‫then all compression stuff, and also DNS lookups,
‫which basically matches web domains
‫to their corresponding real IP addresses.
‫So this is the stuff that would
‫most easily block the main thread.
‫And so, Node takes care of automatically
‫offloading them into the thread pool,
‫where they don't block our event loop.
‫And that is the most important thing
‫that I want you to retain from this video.
‫So, let's now move on and talk about the event loop itself.