Most Fundmental and Frequently Asked Question in Every Node.js Interviews
Hello Everyone,
Welcome to my very first blog, so the blog is about the most asked question
in node.js interviews. If you are someone preparing for the node interviews make
sure you are well versed with the question.
Since I had a recent switch I was asked this question in almost 70% of the interviews if not more. So let's see what this question is.
The question you will be most asked is "Explain the architecture of Node.js"
or "Explain the event-driven architecture of node.js".
So why is this question so important you may ask the reasons are
understanding the architecture helps in having a good understanding of the
node.js quirks and behavior. You get to see under the hood how exactly node.js
is working. It helps in building a scalable and robust application.
When it comes to the quirky nature of asynchronous programming in node.js
and us having to work with single-threaded node.js language, knowing architecture is too fundamental to ignore.
So let us start.
So the key components of node.js architecture are the following
Event Loop
The event loop is a fundamental mechanism in node.js architecture which is
responsible for the execution of callback functions or event handlers. It continuosly looks for new events and executes corresponding callback functions associated with those events.Since
the node.js works on event-driven design patterns. Events having callback and event
handlers are executed by the event loop when that event occurs.
Callback Functions
Since node.js is a single-threaded language it uses callback functions extensively
to provide the illusion of concurrency. Making it efficient and scalable.
So callback functions are any function that are passed as an argument to another function. The main use of callback functions is to allow the rest of the program to continue executing other tasks while waiting for the asynchronous operation to finish.
When the async operation is finished, the callback function is pushed into the callback queue. The event loop which continually looks for programs to execute fetches it from the callback queue and the callback function is executed.
Non-blocking I/O
I/O operations like reading from the file, making HTTP requests, or querying the database in a traditional programming paradigm would block the execution of the programming till the i/o operation is completed.
Node.js however works on an event-driven non-blocking i/o paradigm where
whenever an i/o operation is initiated, the program registers a callback function to be executed when the operation is completed. This enables node.js to continue
execution of the rest of the program. When the I/O operation is completed, the callback function associated with it will be pushed into the callback queue.
The event loop will do its usual work of fetching the callback function from the callback queue and executing it.
Event Emmiters
The EventEmitter class in node.js implements the Observer design pattern.
I will talk more about design patterns in my other blogs.
So the eventEmitter class enables objects to emit named events and register event listeners to handle those events. This mechanism allows the objects to communicate with each other without being tightly coupled.
An analogy that comes to my mind is YouTube channel and their subscribers.
Whenever the YouTube channel posts a video (emitting an event) their subscriber will receive a notification(subscribing to the event). When notification is received the subscriber will watch the video(Invoking the event listener).
My popular libraries extensively make use of the EventEmitter library as a base to build on top of it. Some of the popular libraries are HTTP module and express.js File system module(fs).
Final Thoughts:
This is a very important question to prepare for the node.js interview and having a command over it will greatly impress the interviewee. I will post more suchblogs which can help others related to coding.